in reply to check if an element exists in array
If all you want to do is check if the name is in the list then a hash is the way to go. But, to search an AoA, you could use grep:
my @names = ( [ qw/Jim 12/ ], [ qw/John 15/ ], [ qw/Peter 08/ ], [ qw/Andrew 34/ ], [ qw/Jim 57/ ], [ qw/Andreas 27/ ], ); my $name = 'Jim'; #simple searching if ( grep { $_->[0] eq $name } @names ) { print "$name\'s in the list\n"; } # or to get all of $name's entries for ( grep { $_->[0] eq $name } @names ) { print "@$_\n"; }
Output:
Jim's in the list Jim 12 Jim 57
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: check if an element exists in array
by Anonymous Monk on Apr 19, 2008 at 09:13 UTC | |
by FunkyMonk (Bishop) on Apr 19, 2008 at 09:42 UTC | |
by Anonymous Monk on Apr 19, 2008 at 09:47 UTC | |
by FunkyMonk (Bishop) on Apr 19, 2008 at 09:58 UTC | |
by Anonymous Monk on Apr 19, 2008 at 10:11 UTC | |
|