jeanluca has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: searching an array
by Roy Johnson (Monsignor) on Jan 03, 2006 at 13:37 UTC
    grep
    die "bla bla\n" unless grep {$_ eq $name} @array;

    Caution: Contents may have been coded under pressure.
Re: searching an array
by adrianh (Chancellor) on Jan 03, 2006 at 13:37 UTC

    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

      IIRC 'first' currently has a memleak issue.

      Ordinary morality is for ordinary people. -- Aleister Crowley
        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: searching an array
by wfsp (Abbot) on Jan 03, 2006 at 13:39 UTC

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

      I prefer
      my @lookup{@array} = undef;
      to (*)
      my %lookup = map { $_ => undef } @array;
      :-)

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

      -- 6x9=42
        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.
      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
by si_lence (Deacon) on Jan 03, 2006 at 13:41 UTC
    Something like
    die "bla bla \n" unless grep {$name eq $_} @array;
    ?
Re: searching an array
by jeanluca (Deacon) on Jan 03, 2006 at 13:59 UTC
    Thanks a lot for the grep and map solutions!!

    Luca
Re: searching an array
by mrborisguy (Hermit) on Jan 03, 2006 at 16:35 UTC

    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

      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
Re: searching an array
by phaylon (Curate) on Jan 03, 2006 at 15:20 UTC
    How 'bout (untested): $value =~ $_ && die "Check failed: $_" for @rxes;

    Ordinary morality is for ordinary people. -- Aleister Crowley