[plug] Perl Question: Searching an array
Matthew Robinson
matt at zensunni.org
Wed Feb 26 17:00:44 WST 2003
David Buddrige wrote:
> 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-)
>
You probably want to use the perl grep function. You can get
information about the grep function using perldoc as follows:
perldoc -f grep
This could be used (to duplicate your existing functionality) as follows:
#!/usr/bin/perl
use strict;
my @myarray = ('foo', 'bar');
my $search_val = 'text_to_find';
my $found = 'false';
$found = 'true' if grep /$search_val/, @myarray;
print $found, "\n";
__END__
Hope this helps,
Matt
--
print map{s&^(.)&\U\1&&&$_}split$,=$",(`$^Xdoc$,-qj`=~m&"(.*)"&)[0].$/
More information about the plug
mailing list