On Sun, Aug 19, 2012 at 4:48 PM, timothy adigun <2teezp...@gmail.com> wrote:
> foreach my $match_value ( sort keys %stud ) {
>     print $match_value, "=", $stud{$match_value},
>           $/ if $match_value ~~ @names;
> }

"smart match" is a Perl 6 (though it probably back ported to a Perl 5
module?) invention allowing DWIM here - look through the whole array
for this value.  In normal P5
 foreach my $match_value ( sort keys %stud ) {
     print "$match_value = $stud{$match_value}\n"
           if grep $match_value @names;
}

but that means you're going through @names once for each element of
stud.  Sounds like a job for a Schwartzian Transform (google it)  -
create another hash w/ @names as the keys:
my %match_keys;
# use a hash slice to turn elements into keys
@match_keys{@names} =  @names;

and then
 foreach my $match_value ( sort keys %stud ) {
        print "$match_value = $stud{$match_value}\n"
            if defined $match_keys{$match_value};
}

I used 'defined' in case one of your names was a zero or something.
May be a 'better' route depending upon the size of the data and/or how
often you need to check for that name as a key.
-- 

a

Andy Bach,
afb...@gmail.com
608 658-1890 cell
608 261-5738 wk

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to