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

I have a hash initialized from a DBM file, for a flash card program. I want to shuffle the hash before it is displayed. I am familiar with using the shuffle() function on arrays, but can't find anything for hashes. Any help appreciated.

use strict; use CGI;

Replies are listed 'Best First'.
Re: Shuffling a hash
by Fletch (Bishop) on Jan 11, 2008 at 05:29 UTC

    Because hashes are by nature unordered (there is a natural order but it's not necessarily consistent and you shouldn't assume anything about it). Just shuffle the list of keys instead.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Shuffling a hash (example)
by tye (Sage) on Jan 11, 2008 at 05:31 UTC
    for my $key ( shuffle( keys %hash ) ) { print "$key => $hash{$key}\n"; }

    for example.

    - tye