in reply to grep surprise

Refer to the function indexes in the module List::MoreUtils.
$type morgon2.pl use strict; use warnings; use List::MoreUtils 'indexes'; my @array=(1,2); my $i1 = indexes { $array[$_] == 1 } (0..$#array); my $i2 = indexes { $array[$_] == 2 } (0..$#array); my $i3 = indexes { $array[$_] == 3 } (0..$#array); print "<$i1> <$i2> <$i3>\n"; $perl morgon2.pl Use of uninitialized value $i3 in concatenation (.) or string at morgo +n2.pl line 10. <0> <1> <>
Bill

Replies are listed 'Best First'.
Re^2: grep surprise
by Athanasius (Archbishop) on Nov 12, 2018 at 13:21 UTC
      Since you are testing the case where all entries are unique, you may want to add benchmarks for hash lookups.

      Actually two: there is no doubt that a lookup is way faster, the essential question is if the initialization of the hash needs to be included.

      In the later case I'd prefer a hash slice then.

      @lookup{@array} = (0.. $#array);

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice