in reply to random behaviour of perl hashes

I'm a firm believer in doing empirical testing so I wrote a little sniglet of code:

#!/usr/bin/perl -w use strict; use Data::Dumper; my @ingredients = ( [ qw / onions diced / ], [ qw / potatos diced / ], [ qw / paprika spicy / ], [ qw / salt shaken / ], [ qw / pepper fresh / ] ); my $hash_browns={}; foreach my $ingredient (@ingredients){ $hash_browns -> { $ingredient -> [0] } = $ingredient -> [1]; } foreach my $key ( keys %$hash_browns ){ printf "%s\t%s\n",$key,$hash_browns->{$key}; }

When I run this code three times I get something like this:

[pberghol@cowdawg hash]$ perl hash.pl salt shaken onions diced pepper fresh paprika spicy potatos diced [pberghol@cowdawg hash]$ perl hash.pl salt shaken onions diced pepper fresh paprika spicy potatos diced [pberghol@cowdawg hash]$ perl hash.pl salt shaken onions diced pepper fresh paprika spicy potatos diced
Looks pretty much the same to me.

Now, I'm not sure why this is all important, but if I were looking for random behavior, I'd program for it and not count on a "freak of nature" in my programming language of choice to give it to me. On the other hand if predictable behavior is what I want, again I'll program for that.


Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

Replies are listed 'Best First'.
Re^2: random behaviour of perl hashes
by gellyfish (Monsignor) on Aug 28, 2006 at 08:00 UTC

    As lima1 pointed out this behaviour has been deliberately changed as of the perl 5.8.*, I get this with 5.8.1:

    [jonathan@orpheus jonathan]$ perl foo.pl potatos diced pepper fresh salt shaken paprika spicy onions diced [jonathan@orpheus jonathan]$ perl foo.pl pepper fresh potatos diced onions diced paprika spicy salt shaken [jonathan@orpheus jonathan]$ perl foo.pl salt shaken paprika spicy onions diced potatos diced pepper fresh
    but yes it would be a mistake to rely on an any hash ordering behaviour in a programme.

    /J\

      I get this running ActiveState's 5.8.8 (build 817):

      Case 1 salt shaken onions diced pepper fresh paprika spicy potatos diced Case 2 salt shaken onions diced pepper fresh paprika spicy potatos diced Case 3 salt shaken onions diced pepper fresh paprika spicy potatos diced

      I suspect that it has to do with the details of the implementation of the hashing algorithm, which may differ from system to system.

      My interpretation of the "random ordering" in the perldocs is simply that "we refuse to guarantee the order of the keys for a hash".

      emc

      Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.

      Albert Einstein
        According to "perlrun":

        The default behaviour is to randomise unless the PERL_HASH_SEED is set. If Perl has been compiled with "-DUSE_HASH_SEED_EXPLICIT", the default behaviour is not to randomise unless the PERL_HASH_SEED is set.

        perhaps ActiveState Perl is compiled with -DUSE_HASH_SEED_EXPLICIT ?

        perl -V
        might tell you whether that is the case.

        Liz