Perl: Any quick way to use regex on hash keys?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: Any quick way to use regex on hash keys?
# 1  
Old 06-10-2010
Perl: Any quick way to use regex on hash keys?

Hi,

Is there any quick way to use pull out keys that match a specific regex pattern?

eg
Code:
%hash ;
$hash(123,456) = xxx;
$hash(123,457) = xxx;
$hash(123,458) = xxx;
$hash(223,459) = xxx;

I need a fast way to get all the keys that start with 123..
Meaning I should get
Code:
$hash(123,456) = xxx;
$hash(123,457) = xxx;
$hash(123,458) = xxx;

only....

Currently I am looking through all the keys and using regex on them. I have over 2k such keys and it is making my program very slow. I am trying to tune it. Any suggestions are welcomed. Thank you.

Last edited by Leion; 06-10-2010 at 02:44 AM..
# 2  
Old 06-10-2010
Quote:
Originally Posted by Leion
...
Currently I am looking through all the keys and using regex on them. I have over 2k such keys and it is making my program very slow. ...
Can you post your code that does this ?

tyler_durden
# 3  
Old 06-10-2010
Here is the code...

Code:
 foreach $errBillkey(keys %hash)
 {
         if($errBillkey =~ m/^$account_no,.*/ )
        {
              // do something here
       }
}

# 4  
Old 06-10-2010
That looks quite okay. I tested out a similar logic with 7000 keys, and the time taken to create the hash + match + print is less than 1 second.

Code:
$ time perl -le 'BEGIN{$p="123"} foreach (120000..127000){$x{$_} = "xyz"} foreach $k(keys %x){if ($k =~ m/^$p.*/){print $k}}'
123223
123696
123691
...
...
123322
123712
123719

real    0m0.106s
user    0m0.048s
sys    0m0.019s
$

Maybe the bulk of the time is spent in the "do something" part ?

tyler_durden
# 5  
Old 06-10-2010
This could be a bit faster:
Code:
foreach $errBillkey(grep { /^$account_no/} keys %hash)

Unless you have to, don't try to match beyond the criteria. If it's still slow, you might want break down the hash a bit more, eg by creating a hash of hashes, where the keys of the first hash are the digits left of the comma.
# 6  
Old 06-10-2010
Thanks pludi and durden_tyler for helping to look at my issue.
I think the issue has been settled with pludi's suggestion. I will look into changing the code with hash of hash when i have time Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need regex for line not starting with hash #

I need to search for lines starting with "Include" and later has string "httpd-ssl.conf" like the regex should return match for "Include conf/extra/httpd-ssl.conf" I tried the following: ^]*#;].+Include.*httpd-ssl.conf Below is my current file: # Secure (SSL/TLS) connections... (7 Replies)
Discussion started by: mohtashims
7 Replies

2. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

3. Programming

store information in hash and display number of keys.

I am trying to store this information (info and number) in hash. number is the key and info is value in a hash.i shown my code below. #!/usr/bin/perl use warnings; use strict; use XML::LibXML::Reader; my $file;open $file, 'formal.xml'); my $reader =... (0 Replies)
Discussion started by: veerubiji
0 Replies

4. Shell Programming and Scripting

Sorting keys of a hash in perl

hi all, i have a small problem regarding sorting the keys in a hash. my %hash; for($i=0;$i<19;$i++) { $hash{$i}=$i; } foreach $c (sort keys %hash) { print "\n $hash{$c}"; } (1 Reply)
Discussion started by: niteesh_!7
1 Replies

5. Shell Programming and Scripting

perl hash - using a range as a hash key.

Hi, In Perl, is it possible to use a range of numbers with '..' as a key in a hash? Something in like: %hash = ( '768..1536' => '1G', '1537..2560' => '2G' ); That is, the range operation is evaluated, and all members of the range are... (3 Replies)
Discussion started by: dsw
3 Replies

6. Shell Programming and Scripting

Perl Hash:Can not keep hash data in the same order that it was inserted

Can Someone explain me why even using Tie::IxHash I can not get the output data in the same order that it was inserted? See code below. #!/usr/bin/perl use warnings; use Tie::IxHash; use strict; tie (my %programs, "Tie::IxHash"); while (my $line = <DATA>) { chomp $line; my(... (1 Reply)
Discussion started by: jgfcoimbra
1 Replies

7. Shell Programming and Scripting

PERL - another quick hash of hashes question

Hi, sorry, two hash related questions in one day .. but this has got me a bit stuck. I have a mysql database table that kind of looks like this, the table is called "view1" and a snippet of that table (SELECT'ing just rows with serial number 0629AN1200) is below serial nic_name ... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

8. Shell Programming and Scripting

Quick regex question

Say that I want to match any of the following: abc def ghi The letters will either be "abc", "def", or "ghi", only those three patterns. The numbers will vary, but there will only be numbers between the brackets. I've only been able to match abc, using the following: abc.*. I'm... (1 Reply)
Discussion started by: retrovertigo
1 Replies

9. Shell Programming and Scripting

How to set dynamically keys names in perl %hash

hello I have loop , in this loop im picking names , this names I want to be keys in %hash but I don't know how to set in every loop entertain different key in the %hash (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question