[plug] Perl Question: Searching an array
Trevor Phillips
T.Phillips at murdoch.edu.au
Wed Feb 26 17:30:23 WST 2003
On Wednesday 26 February 2003 16:50, Brad Hill wrote:
> convert the array to a hash and then it's trivial:
>
> #!/usr/bin/perl
>
> @myarray = ( "fish", "chicken", "beef" );
> %myhash = @myarray;
> print "found fash" if $myhash{"fash"};
> print "found fish" if $myhash{"fash"};
Uuh, won't that only work for fish? (Since fish would have a value of beef,
but chicken will have no key, and beef will have no value).
I'd use grep:
if (grep { $_ eq "fish"} @myarray)
{
print "Found!";
}
--
. Trevor Phillips - http://jurai.murdoch.edu.au/ .
: Web Technical Administrator - T.Phillips at murdoch.edu.au :
| IT Services - Murdoch University |
>--------------------------------------------------------------------<
| On nights such as this, evil deeds are done. And good deeds, of /
| course. But mostly evil, on the whole. /
\ -- (Terry Pratchett, Wyrd Sisters) /
More information about the plug
mailing list