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

push(@factored_rows,%coefficient_array);
should be
push(@factored_rows, \%coefficient_array);
otherwise a hash gets flattened in a list of keys and values. Without '\' it's as if you wrote if like that:
push(@factored_rows, 'hash_key1', $value1, 'hash_key2', $value2);

(hash is not an array, btw, they are totally different things)

Also, your program is hard to read. Some advice:
@factored_eqns = &factorize(\@eqn_set,\@variables);
'&' in factorize is completely unnecessary.
my @factored_eqns; ... @factored_eqns = factorize(\@eqn_set,\@variables);
Don't declare a bunch of variables all at the top of the program/sub. They're not much better then global variables when used that way (and it's very write-only). Perl is not C89. Instead of:
my @factored_eqns; ... @factored_eqns = factorize(\@eqn_set,\@variables); ... my $term; ... foreach $term(@{$equation}) { ...
do
my @factored_eqns = factorize(\@eqn_set,\@variables); ... foreach my $term (@{ $equation }) { ...
Indent your code properly. Rather then
foreach $equation (@this_eqn_set){ #then for each variable in the eqution foreach $variable (@this_variable_set){ ...
Write it like so:
for my $equation (@this_eqn_set) { for my $variable (@this_variable_set) { ...
(for and foreach are the same thing)

Uncomment strict. And add use warnings;

Replies are listed 'Best First'.
Re^2: Help !!! Symbolic Math Factroize Subroutine is clobbering my hash
by ipreferperl (Novice) on Dec 25, 2014 at 20:40 UTC
    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 ??
      #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....
      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.