Hi Andy,

On 8/20/12, Andy Bach <afb...@gmail.com> wrote:
> 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

 Smart Match is introduced in Perl 5.10.1. You find more details here:
http://perldoc.perl.org/perlop.html UNDER the subheading smartmatch Operator.

>  foreach my $match_value ( sort keys %stud ) {
>      print "$match_value = $stud{$match_value}\n"
>            if grep $match_value @names;

  Smart Match is a lot faster than grep, "for-loop" and probably
"first" from List::Util Module in Perl.
While I commented on why using for loop for this kind of problem is
not effective, John's solution using map{...} is more like it and alot
better.

> }
>
> 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/
>
>
>


-- 
Tim

-- 
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