Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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"; }

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
Domain Nodelet?
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 lurking in the Monastery: (4)
As of 2022-08-10 02:33 GMT
Find Nodes?
    Voting Booth?

    No recent polls found