in reply to Re: array index
in thread array index

Thanks a lot, but what if @question = "foo doo doo"? Is there a way that I can return more than one array index?

Replies are listed 'Best First'.
Re^3: array index
by wfsp (Abbot) on May 20, 2007 at 07:28 UTC
    You could extend bart's hash lookup by building a hash of arrays.
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @list = qw{one two three four one}; my %lookup; for my $i (0..$#list){ push @{$lookup{$list[$i]}}, $i; } print "@{$lookup{one}}\n"; #print Dumper \%lookup;
Re^3: array index
by bart (Canon) on May 20, 2007 at 08:52 UTC
    grep returns all matching indexes, I just ignored all but the first one. That's easily fixed:
    my @where = grep { $question[$_] eq $search } 0 .. $#question;

    Using a hash for lookup is a not so dead simple adaptation, but still feasible. In the meantime, wfsp has shown how it can be done.