Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
from the below code,
%hash = ("one", 1 , "Two", 2,"three", 3, "four", 4); @value = values(%hash); print("$value[1]");
The output will be "3" not "2"
Can anyone tell me the reason for that?
Thanks

Replies are listed 'Best First'.
Re: The strange ordering of hash values
by Corion (Patriarch) on Apr 19, 2007 at 16:58 UTC

    Hashes have no intrinsic order of their elements. See perlfaq4, or type perldoc -q order on your command prompt to see how to get a hash that remembers the order you put the elements into it. Maybe you want an array instead?

      Thanks a lot for all comments posted here.
      looking into the above code, How can we say the output will be "3" .... Is that possible?
      Thanks

        If you want to print three, here you go,

        print "3";

        I think a better question would be, WHY do you want the answer to be 3. We cannot help if you cannot explain what you attempting to do.

Re: The strange ordering of hash values
by jdporter (Paladin) on Apr 19, 2007 at 17:00 UTC

    Hashes are unordered. The order in which entries are added is not preserved. The order in which you get them out via keys or values is not dependable.

    Note, though, that you can rely on the order being the same for keys and values, whatever that order is. For example, you can copy one hash into another by doing this:

    @b{ keys %a } = values %a;

    Furthermore, the above is only the default. It is possible to override this behavior on a hash-by-hash basis using a tie-based module from CPAN, such as Tie::IxHash.

    A word spoken in Mind will reach its own level, in the objective world, by its own weight
Re: The strange ordering of hash values
by thezip (Vicar) on Apr 19, 2007 at 17:04 UTC

    This happens because you are storing your values in a hash, and sorted position is not maintained in a hash. If you want to maintain the ordering, use an array instead.

    BTW, hashes are usually populated in this manner:

    %hash = ("one" => 1 , "Two" => 2, "three" => 3, "four" => 4);

    Where do you want *them* to go today?

      Actually, you would rather write this as :

      %hash = (one => 1 , two => 2, three => 3, four => 4);

      since one of the nice point of "=>" is that it allows to use a bareword on it's left side without strict protesting about it (well it's obvious here that you want a string on this side so it's nice not having to be explicit about it in my opinion, => is a nice syntaxic sugared comma).

      --
      Jedaļ
      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: The strange ordering of hash values
by McDarren (Abbot) on Apr 19, 2007 at 17:00 UTC
    Because hashes are not ordered.
    If you were to change the second line to:
    @values = sort values(%hash);
    ..then you might get the output that you expect.
Re: The strange ordering of hash values
by derby (Abbot) on Apr 19, 2007 at 17:00 UTC

    Hashes are unordered so the output order of values (and keys) has no relation to their creation order. You can use Tie::IxHash if you wish to preserve the order.

    -derby