in reply to non-scalar hash key

pack is a bit faster than join:
use strict; use warnings; use Benchmark qw(:all) ; my @foo = qw/1 2 3 4 5 6/; my %bar = (pack('b', @foo) => 'elephants'); my @baz = qw/1 2 3 4 5 6/; print $bar{pack('b', @baz)} . "\n"; cmpthese(-5, { 'join' => sub { join('', qw/1 2 3 4 5 6/) }, 'pack' => sub { pack('b', qw/1 2 3 4 5 6/) }, });
prints:
% perl test.pl elephants Rate join pack join 1002841/s -- -53% pack 2119814/s 111% -- %

Replies are listed 'Best First'.
Re^2: non-scalar hash key
by tweetiepooh (Hermit) on Jun 17, 2009 at 16:14 UTC
    Nice but not in the gold
    use strict; use warnings; use Benchmark qw(:all) ; my @foo = qw/1 2 3 4 5 6/; my %bar = (pack('b', @foo) => 'elephants'); my @baz = qw/1 a 4 6 7/; print $bar{pack('b', @baz)} . "\n";
    Also outputs "elephants".
Re^2: non-scalar hash key
by kdejonghe (Novice) on Jun 17, 2009 at 13:06 UTC
    It is indeed. Thank you very much. I pack as follows though:
    my $p = pack('L*', @$pa);
    because it's an integer array.
    I also think it uses less memory. But that I still have test.
    Thanks again. Seems like this is the only thing to do apart from implementing my own hash algorithm.