in reply to Re: Re: Re: Re: Back to Remedial Perl for Me: map{} function
in thread Back to Remedial Perl for Me: map{} function

That's what I thought, but it did the same order every time I ran it, so it seems that there is some order (i.e. it's not completely random). Was just wondering how perl decides to order it. Even Data::Dumping the hash gives the same order!
  • Comment on Re: Re: Re: Re: Re: Back to Remedial Perl for Me: map{} function

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Re: Back to Remedial Perl for Me: map{} function
by chipmunk (Parson) on Dec 15, 2000 at 22:34 UTC
    The ordering of elements in a hash is based on the hash function. The hash function processes a key and determines where in the hash that key should be placed. For any given key, the hash function must always return the same result. A good hash function will tend to keep keys spread out in the hash; if all the keys end up in the same place in the hash, then you lose the benefit of having a hash.

    So, there is an "order" to the elements in the hash, but the order is not intended to be useful to the programmer, and keys may be reordered when the size of the hash changes.

Re: Re: Re: Re: Re: Re: Back to Remedial Perl for Me: map{} function
by davorg (Chancellor) on Dec 15, 2000 at 22:29 UTC

    Oh, it is ordered in some way, just not any way that is obvious to the casual user. The sort order is indicative of how perl is modelling the hash internally. Yes you'll see the same order each time you run the script, but that may well not be true if you were to run it on another operating system or a different version of Perl.

    All in all, it's best to assume that the ordering is random.

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me