SPUG: IndexOf in Perl?
Tanwir Habib
tanwirh at hotmail.com
Sat Jan 28 16:25:31 PST 2006
Many thanks for the reply.
I'm not sure where I am wrong but its not working somehow
#!/usr/bin/perl -w
use strict;
use List::Util qw(first);
my @array = qw/AA AC AE AF AG CA CC CD DC CB VV YY/;
My $index = first {$_ eq 'AF'} @array;
print $index;
It should print 3 but its printing AF.
On 28/1/06 23:35, "David Dyck" <david.dyck at fluke.com> wrote:
> On Sat, 28 Jan 2006 at 15:16 -0800, Brian Wisti <brianwisti at yahoo.com> wrote:
>
>> You might try the List::Util module. It's core in 5.8.
>>
>> use List::Util qw(first);
>> ...
>> my $index = first { $_ == $test_value } @array;
>
> $index is the element, not the index they were looking for.
>
> perl -le ' use List::Util qw(first); print first { /c/ } ('a' .. 'z'); '
> c
>
>
More information about the spug-list
mailing list