Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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
Replies are listed 'Best First'.
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.


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.


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2015-12-01 20:42 GMT
Find Nodes?
    Voting Booth?

    My keyboard shows this many letters:

    Results (26 votes), past polls