searching an array
jeanluca
created: 2006-01-03 08:32:05
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
created: 2006-01-03 08:37:26
[doc://grep]
die "bla bla\n" unless grep {$_ eq $name} @array;

Caution: Contents may have been coded under pressure.
Re: searching an array
created: 2006-01-03 08:37:28

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
created: 2006-01-03 10:17:33
IIRC 'first' currently has a memleak issue.

Ordinary morality is for ordinary people. -- Aleister Crowley
Re^3: searching an array
created: 2006-01-03 10:43:41
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
created: 2006-01-03 10:49:28
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
created: 2006-01-03 08:39:03

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
created: 2006-01-03 10:45:19
I prefer
my @lookup{@array} = undef;
to (*)
my %lookup = map { $_ => undef } @array;
:-)

(*) should it be "to" or "instead of" ?

-- 6x9=42
Re^3: searching an array
created: 2006-01-03 10:51:44
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
created: 2006-01-03 11:09:26
Oh, ok... I need more sleep... :-)

-- 6x9=42
Re^2: searching an array
created: 2006-01-03 13:26:08
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.


holli, /regexed monk/
Re: searching an array
created: 2006-01-03 08:41:05
Something like
die "bla bla \n" unless grep {$name eq $_} @array;
?
Re: searching an array
created: 2006-01-03 08:59:43
Thanks a lot for the grep and map solutions!!

Luca
Re: searching an array
created: 2006-01-03 10:20:50
How 'bout (untested): $value =~ $_ && die "Check failed: $_" for @rxes;

Ordinary morality is for ordinary people. -- Aleister Crowley
Re: searching an array
created: 2006-01-03 11:35:45

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
created: 2006-01-03 11:55:48
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

perlmonks.org content © perlmonks.org and adrianh, Articuno, holli, jdhedden, jeanluca, mrborisguy, phaylon, Roy Johnson, si_lence, wfsp

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03