Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Re: split to hash, problem with random keys

by december (Pilgrim)
on Jul 03, 2003 at 17:27 UTC ( [id://271239]=note: print w/replies, xml ) Need Help??


in reply to Re: split to hash, problem with random keys
in thread split to hash, problem with random keys

It's not by size or alphabetical. Just out of curiosity, do you know more about the logic in the order those hashing routines sort the data?

Thanks for your insight.

  • Comment on Re: Re: split to hash, problem with random keys

Replies are listed 'Best First'.
Re: Re: Re: split to hash, problem with random keys
by demerphq (Chancellor) on Jul 03, 2003 at 19:11 UTC

    do you know more about the logic in the order those hashing routines sort the data

    There is no ordering principle in Perls hash implementation. In fact the hashes are statically initialized with a pseudorandom sequence at launch. The order that follows from there is strictly incidental to anything else. The only thing that you are currently promised is that you will get the same sequence from the same set of keys that were inserted into the structure. (This is order dependent. Change the order in which you insert keys into the hash, and the output sequence will differ as well.)

    And get this: Its going to get worse. There is a DOS attack that is based around targetting vulnerabilities in the hash algorithm causing perl to slow to a crawl while populating the hash. In order to resolve this the hash will no longer be initialized with a static pseudorandom sequence, instead it will be initialized with dynamic pseudo random sequence. Thus there will be no guaranty that the same set of inserts will produce the same order of keys on two different runs.

    The moral of this story is that if you want your code to be forward compatible with later versions of perl then you had better not depend on native key order in any way at all. If you need to do this then sort the keys.

    It should come to no suprise to people that there is lots of code out there that will be subtly broken by this change. People depend on the repeatability of hash keys more often than they think they do.


    ---
    demerphq

    <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
      Just a question on this, because I can't be bothered to dig it out myself. Will something the following still work as expected?
      my %new_hash; @new_hash{map munge($_), keys %old_hash} = value %old_hash;
      This depends on the sequence of elements returned being identical and is the only way I ever depend on order in a hash.

      Makeshifts last the longest.

        From perlfunc:keys

        The actual random order is subject to change in future versions of perl, but it is guaranteed to be the same order as either the values or each function produces (given that the hash has not been modified).

        As far as I'm aware, the only way to iterate the values of a hash (internally), is to iterate the keys and use them to retrieve the values, so unless an extra step was added to randomise them after retrieval, the returns from keys and values (and each) would have to come back in the same order?


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

Re: Re: Re: split to hash, problem with random keys
by MrCromeDome (Deacon) on Jul 03, 2003 at 17:32 UTC
    My knowledge of data structures is spotty at best. I imagine that consulting
    perldoc perlguts
    would shed some light on this matter though.

    MrCromeDome

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://271239]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 04:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found