Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Using regex to extract keys

by perlmonkey (Hermit)
on Sep 24, 2003 at 20:59 UTC ( [id://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.

    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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://294016]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-04-25 02:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found