in reply to find position of element of array
Can't believe this hasn't been pointed out yet, but @array = {"a", "b", "c", "d"} leaves @array with one element, that element being a hashref. You want parentheses instead of braces.
Instead of the grep solution offered above, you can use List::Util's first.
#!/usr/bin/perl -l use List::Util "first"; my @array = qw(a b c d); my $index = first { $array[$_] eq "c" } 0..$#array; die "c was not found in \@array" if ! defined $index; print $array[$index-1];
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: find position of element of array
by perlbeginner10 (Acolyte) on Nov 13, 2005 at 11:38 UTC | |
by tirwhan (Abbot) on Nov 13, 2005 at 11:55 UTC | |
by perlbeginner10 (Acolyte) on Nov 13, 2005 at 12:14 UTC |