in reply to Re^3: Convert a string into a hash
in thread Convert a string into a hash

There is no information in a hash with index values that isn't in an array.

Yes and no. If you want to know "the position of a given number", the information is there in the array, but finding it is hard (O(N)). It's O(1) with a hash.

my ($index) = grep $code[$_] == $x, 0..$#code; -vs- my $index = $code{$x};

Replies are listed 'Best First'.
Re^5: Convert a string into a hash
by Your Mother (Archbishop) on Aug 15, 2009 at 04:27 UTC

    NIce comparison/example of a possible why. With List::Util::first, or equivalent code, you could at least move the array more to the middle ground (on average); I know you know that already and it's good (for me at least) to be confronted with the algorithm costs. That alone suggests a reason for doing it with the hash.

      you could at least move the array more to the middle ground (on average)

      Pretending for a second that first doesn't require putting the contents of entire array on the stack, the performance would still be O(N).

      Mind you, that's rarely a problem since most arrays are quite small, but the code is also much less readable.