[plug] Perl Question: Searching an array

Brad Hill brad at marketcast.com.au
Wed Feb 26 16:50:37 WST 2003


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"};

----- Original Message -----
From: "David Buddrige" <buddrige at wasp.net.au>
To: <plug at plug.linux.org.au>
Sent: Wednesday, February 26, 2003 4:39 PM
Subject: [plug] Perl Question: Searching an array


> Hi all,
>
> I have a perl program that contains a very large array.  I want to search
> this array to determine if it contains a particular value.  Is there a
> faster way to search the array than this?
>
>
> $search_val = "text-to-look-for";
> $found = "false";
> foreach $line ( @myarray )
> {
>    if ( $search_val == $line )
>    {
>        $found = "true";
>    }
> }
>
> I've beeen reading through "Learning Perl" but can't find any answer on
this
> one. 8-)
>
>
> thanks heaps guys
>
> David Buddrige



More information about the plug mailing list