Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Using regex to extract keys

by perlmonkey (Hermit)
on Sep 24, 2003 at 20:59 UTC ( #294016=note: print w/ replies, xml ) Need Help??


in reply to Using regex to extract keys

use grep:

my $r = 44; my $hash = $self->{'residues'}{$modelCount}; for my $key ( grep { /^\w$r$/ } keys %$hash ) { print $key."\n"; }
Update:Yup, as abigail suggested use each for large hashes so you dont duplicate the memory needed for the keys:
my $r = 44; my $hash = $self->{'residues'}{$modelCount}; while( my ($key, $value) = each %$hash ) { next if $key =~ /^\w$r$/; print $key."\n"; }


Comment on Re: Using regex to extract keys
Select or Download Code
Re: Using regex to extract keys
by Abigail-II (Bishop) on Sep 24, 2003 at 21:17 UTC
    Since the hash is given to be large, one might prefer iterating over the hash using each in a while loop. In the loop, test whether the key matches /^\w$r$/. If not, do a next.

    Abigail

Re: Re: Using regex to extract keys
by seaver (Pilgrim) on Sep 25, 2003 at 13:59 UTC
    Fantastic, Thanks!

    I was disappointed because I was hoping that there was some inherent regex in calling a hash key, but obviously I cant do it that way. The while loop with each does the job just grand.

    Cheers
    Sam

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://294016]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2014-09-01 11:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (6 votes), past polls