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?

No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
Lady Aleena

In reply to I have a list expanding when I don't want it to by Lady_Aleena

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.