Lady_Aleena has asked for the wisdom of the Perl Monks concerning the following question:
Hello everyone.
In the following function, I create two new keys for a hash which is entered into the function.
use List::Util qw(shuffle); sub random { my ($user_input, $list) = @_; $list->{'all'} = [ map { @$_ } values %{$list} ]; $list->{'keys'} = [ grep {$_ !~ /(?:all|keys)/} keys %{$list} ]; if ($user_input && $user_input =~ /(?:help|options)/) { return 'Your options are: '.join(', ',sort @{$$list{'keys'}}).', o +r all.'; } elsif ($user_input && $user_input =~ /dump/) { use Data::Dumper; return Dumper($list); } else { my $input = $user_input ? $user_input : 'all'; my @random_list = shuffle(@{$$list{$input}}); return $random_list[rand @random_list]; } }
The first time I run a list through random, the list for $list->{'all'} is just what I want it to be. However, upon subsequent runs of random, 'all' grows. I can't figure out why it is doing it. Here is an example...
my %waters = ( running => [qw(spring streamlet rivulet run brook creek stream rive +r)], standing => [qw(drop puddle pool pond lake sea ocean)], precipitation => [qw(rain snow sleet hail)] ); sub random_water { my ($user_water) = @_; random($user_water, \%waters); }
After I run random_waters('dump'), I get...
print random_water('dump'); $VAR1 = { 'keys' => [ 'standing', 'precipitation', 'running' ], 'standing' => [ 'drop', 'puddle', 'pool', 'pond', 'lake', 'sea', 'ocean' ], 'precipitation' => [ 'rain', 'snow', 'sleet', 'hail' ], 'running' => [ 'spring', 'streamlet', 'rivulet', 'run', 'brook', 'creek', 'stream', 'river' ], 'all' => [ 'drop', 'puddle', 'pool', 'pond', 'lake', 'sea', 'ocean', 'rain', 'snow', 'sleet', 'hail', 'spring', 'streamlet', 'rivulet', 'run', 'brook', 'creek', 'stream', 'river' ] };
The hash looks good here, however, when I run it twice...
print random_water('dump'); print random_water('dump'); $VAR1 = { 'keys' => [ 'standing', 'precipitation', 'running' ], 'standing' => [ 'drop', 'puddle', 'pool', 'pond', 'lake', 'sea', 'ocean' ], 'precipitation' => [ 'rain', 'snow', 'sleet', 'hail' ], 'running' => [ 'spring', 'streamlet', 'rivulet', 'run', 'brook', 'creek', 'stream', 'river' ], 'all' => [ 'standing', 'precipitation', 'running', 'drop', 'puddle', 'pool', 'pond', 'lake', 'sea', 'ocean', 'rain', 'snow', 'sleet', 'hail', 'spring', 'streamlet', 'rivulet', 'run', 'brook', 'creek', 'stream', 'river', 'drop', 'puddle', 'pool', 'pond', 'lake', 'sea', 'ocean', 'rain', 'snow', 'sleet', 'hail', 'spring', 'streamlet', 'rivulet', 'run', 'brook', 'creek', 'stream', 'river' ] };
'all' has grown, it even includes the keys, which shouldn't be possible as I create 'keys' after I create 'all'. I can't figure out why. %waters should grow between runs, right?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: I have a list expanding when I don't want it to
by BrowserUk (Patriarch) on Nov 19, 2014 at 21:41 UTC | |
by Lady_Aleena (Priest) on Nov 19, 2014 at 21:51 UTC | |
by BrowserUk (Patriarch) on Nov 19, 2014 at 23:00 UTC | |
by Lady_Aleena (Priest) on Nov 19, 2014 at 23:11 UTC | |
by GrandFather (Saint) on Nov 20, 2014 at 00:20 UTC | |
| |
by poj (Abbot) on Nov 20, 2014 at 09:02 UTC | |
by Lady_Aleena (Priest) on Nov 23, 2014 at 08:28 UTC | |
by Lady_Aleena (Priest) on Nov 20, 2014 at 09:48 UTC |