in reply to Re^2: Help !!! Symbolic Math Factroize Subroutine is clobbering my hash
in thread Help !!! Symbolic Math Factroize Subroutine is clobbering my hash

#reset this_array for some other operation @this_array=();

Why would you reset @this_array; Why not just use @that_array for the other operation?


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^4: Help !!! Symbolic Math Factroize Subroutine is clobbering my hash
by ipreferperl (Novice) on Dec 25, 2014 at 23:42 UTC
    The array needs to be reset. This is because subsequently the array needs to be populated with other items; the next hash will be an array of these other items. It the array is not reset, items from the previous operation will be appended to the items in the current operation....
      The array needs to be reset. This is because subsequently the array needs to be populated with other items; the next hash will be an array of these other items. It the array is not reset, items from the previous operation will be appended to the items in the current operation....

      No. It doesn't. Use a different array.

      You've stored a reference to @this_array in the hash; if you modify it, then you are modifying (deleting) the data that reference points to.

      You might just as well not have stored it in the first place, if as soon as you do, you destroy the data the reference points to.

      Update:Maybe this will clarify things?:

      @this_array = 0..9;; $hash{ akey } = \@this_array;; pp \%hash;; { akey => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] } @this_array = ();; pp \%hash;; { akey => [] }

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Yes... I see the problem... Thanks for the suggestion... Merry Christmas ;-)
      The array needs to be reset.

      If the array is going to be "populated with other items", then a new array needs to be created. The  foo() function above creates a single array  @this_array and then puts things into and takes things out of it, but the location (and thus the reference address) of the array never changes; in the end, you wind up with a bunch of references to the same empty array (because the last thing you did to it was empty it).

      c:\@Work\Perl\monks>perl -wMstrict -MData::Dumper -le "sub foo { ;; my @this_array; my %this_hash; ;; foreach my $something (0 .. 4) { ;; push(@this_array, $something); ;; my $key = $something + 1000; $this_hash{$key} = \@this_array; ;; @this_array = (); ;; } ;; return(\%this_hash); } ;; my $hash_ref = foo(); print Dumper $hash_ref; " $VAR1 = { '1002' => [], '1001' => $VAR1->{'1002'}, '1004' => $VAR1->{'1002'}, '1000' => $VAR1->{'1002'}, '1003' => $VAR1->{'1002'} };
      (The repeated appearance of the expression  $VAR1->{'1002'} just means that the values of those keys all use the value of the previous key; all hash values are references to the same empty array.)

      If you want references to a bunch of different arrays (possibly containing different things), you must repeatedly create a new lexical array and reference it. Maybe something like

      c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "sub foo { ;; my %this_hash; ;; foreach my $something (0 .. 4) { ;; my @this_array; push(@this_array, $something); ;; my $key = $something + 1000; $this_hash{$key} = \@this_array; ;; } ;; return(\%this_hash); } ;; my $hash_ref = foo(); dd $hash_ref; " { 1000 => [0], 1001 => [1], 1002 => [2], 1003 => [3], 1004 => [4] }
      Note that because  @this_array is created anew each time through the loop, there is no need ever to empty it. Note also that there are neater and IMHO better ways to create references to anonymous arrays, but that's an implementation and style discussion.

        Oh.. I see the error in my ways...you suggestion worked wonderfully... I should not have made this_array static. Did this by declaring this in the subroutine variable declaration ... Many thanks and a Merry Christmas !!!