in reply to Mixing Up a Text File

Dear Monks; Another idea on this topic came suddenly to my mind! I fear it is not a real solution, and when I tested it, it didn't work!
But wouldn't it be nice, to just store it into a hash, and then print out the values of the hash, while hoping perl disorders the elements, like it does sometimes!
(I know it does not help if it is absolutely necerserry to mix them up each time in a different way)
I'm rather sure that this is the shortest solution.. Could someone more skilled please tell if this was possible or if it is just one of my silly ideas.

Replies are listed 'Best First'.
Re^2: Mixing Up a Text File
by Aristotle (Chancellor) on Jun 02, 2002 at 18:58 UTC
    #!/usr/bin/perl -w use strict; my(%hash,$i); @hash{map($i++.":$_", <>)} = (); print "$_\n" for map /^\d+:(.*)/, keys %hash;
    NB: the $i++ and regex trickery is necessary to preserve identical lines.

    Update: Since the above code will produce the same "randomized" file every time you run it with the same input data:
    #!/usr/bin/perl -w use strict; my(%hash,$i); @hash{map join(":", $i++, int rand $i, $_), <>} = (); print "$_\n" for map /^\d+:\d+:(.*)/, keys %hash;
    Which I guess was not the point, since we're back to using rand()
    ____________
    Makeshifts last the longest.