Dear Monks
I have the following problem: I've this array containing names. Furthere more I have a variable $name that should contain one of the names inside this array. If $name contains something else, my progrem should quit/die. Of course I can solve this with:
$test = false ;
for(0..$#array)
$test true if ( $array[$_] eq $name ) ;
}
die "bla bla\n" if $test eq false ;
Something tells me there is a more intelligent way to do this, something like:
die "bla bla\n" /$name/ for @array
But then, of course, a little bit different.....
Any suggestions ?
Thanks a lot in advance
Luca
Re: searching an array
[doc://grep]
die "bla bla\n" unless grep {$_ eq $name} @array;
Caution: Contents may have been coded under pressure.
Re: searching an array
You want grep
die "bla bla\n" unless grep { $_ eq $name } @array
Or if you want to avoid the overhead of looking at the whole array look at first() in List::Util.
s/if/unless/ - thanks frodo72
Re^2: searching an array
IIRC 'first' currently has a
memleak issue.
Ordinary morality is for ordinary people. -- Aleister Crowley
Re^3: searching an array
The mem leaks are
fixed
in v1.18 of Scalar-List-Utils. The developers just haven't resolved the tickets yet.
Remember: There's always one more bug.
Re^4: searching an array
Good to know, I just remembered these issues a few weeks or months ago were talked about in #catalyst.
Ordinary morality is for ordinary people. -- Aleister Crowley
Re: searching an array
A hash is handy for these type of jobs.
#!/usr/bin/perl
use strict;
use warnings;
my @array = (qw(peter paul john mary));
my $name = 'jim';
my %lookup = map { $_ => undef} @array;
die "$name not found\n" unless exists $lookup{$name};
print "found $name\n";
Re^2: searching an array
I prefer
my @lookup{@array} = undef;
to (*)
my %lookup = map { $_ => undef } @array;
:-)
(*) should it be "to" or "instead of" ?
Re^3: searching an array
Except that's a syntax error. You can't declare a hash slice. You'd have to declare the hash, then do the slice-assignment.
Caution: Contents may have been coded under pressure.
Re^4: searching an array
Oh, ok... I need more sleep... :-)
Re^2: searching an array
This is really up to the problem.
If your problem demands one (or few) lookups you are better off scanning through the array, esp. if the array is huge or memory is short. If The array is sorted you can optimize this using a binary search algorithm.
If you have many lookups and you can easily pay the price of memory, then the hash solution is appropriate.
Re: searching an array
Something like
die "bla bla \n" unless grep {$name eq $_} @array;
?
Re: searching an array
Thanks a lot for the grep and map solutions!!
Luca
Re: searching an array
How 'bout (untested):
$value =~ $_ && die "Check failed: $_" for @rxes;
Ordinary morality is for ordinary people. -- Aleister Crowley
Re: searching an array
You also could use one of (Quantum::Superpositions, Perl6::Junction, List::Util::Superpositions). Then it would look something like
die "blah bla\n" if $name ne any( @array );
-Bryan
Re^2: searching an array
Or, to match the OPs question w/ regexes, if I'm reading
Perl6::Junction correctly:
die "with dignity!" unless any( @rxes ) == $value;
Ordinary morality is for ordinary people. -- Aleister Crowley