in reply to Re: Array indices
in thread Array indices
sub indices { my $match = shift; my $i = 0; map { $i++; $_ eq $match ? $i : () } @_ }
Returning an empty list in the map callback causes that iteration to disappear from the result list. This is useful because you can use it as a surrogate grep that allows you to return something other than what your input was.
Update: removed ->[0] copy paste remainder from code.
Update 2: the following is more idiomatic and pretty much makes having a separate sub useless. We're back to grep too:
sub indices { my $match = shift; grep $_[$_] eq $match, 0 .. $#_ }
Makeshifts last the longest.
|
|---|