Phoenix.pm: map + grep problem

Shay Harding kellewic at yahoo.com
Mon May 5 11:46:14 CDT 2003


> However, the Cookbook will probably tell you to use
> hashes - then you can do a hashed
> (very quick access to a given element) rather than
> looping through each and every element
> of an array:
> 
>   # (1, 2, 3, 4) -> ( 1=>1, 2=>1, 3=>1, 4=>1 ) -
> just make keys exist for each element
>   %a2 = map { $_ => 1 } @a2;

This could be written as:

my %h_a2;
@h_a2{@a2} = undef;

which would be quicker than using map. The above
really only assigns to the first key, then Perl
assigns "undef" to the rest.

This code still works for the above assignment as
well.

>   foreach my $i (@a1) {
>     if(exists $a2{$i}) {
>       print "a1 and a2 both have the element
> '$i'\n";
>     }
>   }
> 
> -scott


__________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
http://search.yahoo.com



More information about the Phoenix-pm mailing list