The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
#1
[Originally posted by knalley]

Hi Dave,

I haven't seen you on the other board you used to answer about a year ago so I
had to come and track you down here (smile). I've searched google and all my
Perl books but couldn't find an answer to my question...hope you can help.

I have been searching for some information on how to extract random hash keys
and elements but haven't had any luck.

An example of what I'm wanting to do is this...

%hash = ( yahoo => 'http://www.yahoo.com',
google => 'http://www.google.com',
lycos => 'http://www.lycos.com'
);

# from here I need a routine that would allow me to pull random keys and
values from the list above while looping through the hash. I want a different
starting point everytime the loop is called so I need a random sorting order.
I realize that a hash has it's own internal sorting order that's not
ascending, but even though, it's the same everytime the below loop is called.
I need it to be different each time.

foreach (sort (keys %hash)) {
my $name = $_;
my $value = $hash{$_};
# Do other stuff....
}

Any help would be greatly appreciated...

Regards, Wayne
import-bot (20211) [Avatar] Offline
#2
Re: Random Hash Order
[Originally posted by dave]

If you're talking about the board I think you are, then the owner asked me to
stop posting there.

Here are a few possible solutions to your question:

#!/usr/bin/perl

use warnings;
use strict;

my %hash = (one => 1, two =>2, three => 3, four => 4, five => 5);

my @keys = keys %hash;

# pick one
my $key = $keys[rand @keys];

print "$key -> $hash{$key}
";

print '-' x 40, "
";

# randomise keys
@keys = randomise(@keys);

foreach (@keys) {
print "$_ -> $hash{$_}
";
}

# randomise is adapted from perlfaq4
sub randomise {
my @arr = @_;

my $i = @arr;;
while ($i--) {
my $j = int rand ($i+1);
@arr[$i,$j] = @arr[$j,$i];
}

return @arr;
}

print '-' x 40, "
";

# destroy @keys
while (@keys) {
my $key = rand @keys;
$key = splice(@keys, $key, 1);

print "$key -> $hash{$key}
";
}


hth,

Dave...
import-bot (20211) [Avatar] Offline
#3
Re: Random Hash Order
[Originally posted by knalley]

Wow, well that's sad that she requested you not to post there. I really
thought you brought A LOT to the board. I could always count on you when I
was stuck on a problem. I believe many of the others felt the same way.

It will take me a little bit to decipher you solution (I'm slow <smile>smiliebut it
looks like something I can implement into my script.

Thanks again Dave and I will probably stop in from time to time. Are there
any other message boards you like to hang out on?

Regards, Wayne
import-bot (20211) [Avatar] Offline
#4
Re: Random Hash Order
[Originally posted by dave]

> Wow, well that's sad that she requested you not to post there. I really
> thought you brought A LOT to the board. I could always count on you when I
> was stuck on a problem. I believe many of the others felt the same way.

Apparently I commited the heinous crime of recommending that people read
_other_ Perl books smilie

> It will take me a little bit to decipher you solution (I'm slow <smile>smiliebut it
> looks like something I can implement into my script.

If you need any more explaination, just shout.

> Thanks again Dave and I will probably stop in from time to time. Are there
> any other message boards you like to hang out on?

You should check out Perlmonks at http://perlmonks.org.

Dave...