Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: Help !!! Symbolic Math Factroize Subroutine is clobbering my hash

by ipreferperl (Novice)
on Dec 25, 2014 at 20:40 UTC ( [id://1111363]=note: print w/replies, xml ) Need Help??


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

Hi,

Thanks for the advice... I restructured the code base on your observations... A question would be what happens to an array that is being used as a temporary container within a subroutine...

sub foo{ my @this_array; my %this_hash; foreach....{ push(@this_array, $something); $this_hash{$key} = \@this_array; #reset this_array for some other operation @this_array=(); $key = $next_key; $something = $some_other_thing; } return(\%this_hash); }
I was wondering if the hash that is returned by this subroutine will map all keys but the last one to NULL objects. This is because each time the array reference is stored in the hash, it is null-ed thereafter, and therefore the stored array references are now pointing to a NULL objects.. What would be the proper way to use an array as a temporary container ??
  • Comment on Re^2: Help !!! Symbolic Math Factroize Subroutine is clobbering my hash
  • Download Code

Replies are listed 'Best First'.
Re^3: Help !!! Symbolic Math Factroize Subroutine is clobbering my hash
by BrowserUk (Patriarch) on Dec 25, 2014 at 22:18 UTC
    #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.
      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.
        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.

Re^3: Help !!! Symbolic Math Factroize Subroutine is clobbering my hash
by Anonymous Monk on Dec 25, 2014 at 21:27 UTC
    push @{ $this_hash{$key} }, $something;
    Perl will automagically create a new anonymous array and associate it with $this_hash{$key}, even if the hash didn't even have this key before (it's called 'autovivification'). OTOH, if the key is already pointing to an array, Perl will reuse it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1111363]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-24 22:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found