http://www.perlmonks.org?node_id=294016


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