Perl/Shell Knowledge Sharing

Perl Interview Questions and Answers

Thursday, August 4, 2011

How to shuffle the values in a hash?

use List::Util qw(shuffle);

%hash = (this => 0,
is => 1,
a => 2,
test => 3);

my @values = shuffle(values %hash);
map { $hash{$_} = shift(@values) } (keys %hash);

print "$_\t$hash{$_}\n" foreach keys %hash;
Posted by perlknowledge at 10:37 PM
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels: Interview question

No comments:

Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments (Atom)

Search This Blog

Blog Archive

  • ►  2017 (1)
    • ►  March (1)
  • ►  2014 (23)
    • ►  November (1)
    • ►  July (1)
    • ►  June (5)
    • ►  April (4)
    • ►  March (6)
    • ►  January (6)
  • ►  2013 (9)
    • ►  December (4)
    • ►  June (4)
    • ►  March (1)
  • ►  2012 (5)
    • ►  November (1)
    • ►  January (4)
  • ▼  2011 (215)
    • ►  November (5)
    • ►  September (1)
    • ▼  August (5)
      • Initialize (clear) an array
      • How to shuffle the values in a hash?
      • Perl : Numeric Range Pattern Matching (IP)
      • perl - Objects, Modules and Packages
      • Perl References
    • ►  July (11)
    • ►  June (6)
    • ►  May (17)
    • ►  April (16)
    • ►  March (14)
    • ►  February (9)
    • ►  January (131)
  • ►  2010 (42)
    • ►  December (42)
Simple theme. Powered by Blogger.