madM has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

Im trying to make a calculation inside a loop and work with the results after the loop has ended. In this example im creating an array (@matrix_numbers) with all values of the hash of hashes( $matrix->{$key}->{$key2}). As it is a problem with global and local variables i dont think is important to describe whats inside those variables.
When i try to print the array after the loop has ended im getting nothing.. the array is empty..
any ideas why is this happening and how can i avoid this?
thank you all!
foreach my $key (@aminos){ #used to create an array with all values of + the matrix foreach my $key2 (@aminos){ @matrix_numbers = $matrix->{$key}->{$key2}; } } print @matrix_numbers;

Replies are listed 'Best First'.
Re: local and global variable problem
by Athanasius (Archbishop) on Apr 30, 2014 at 15:59 UTC

    Hello madM,

    I’m assuming that each call to $matrix->{$key}->{$key2} returns a single scalar value? In any case, each time the assignment:

    @matrix_numbers = $matrix->{$key}->{$key2};

    is executed, it overwrites the array @matrix_numbers with a new value. What you need is:

    push @matrix_numbers, $matrix->{$key}->{$key2};

    See push.

    (I don’t understand why the array is empty — it should contain some value after the two foreach loops end.)

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      thank you! i forgot it ;=)
Re: local and global variable problem
by thezip (Vicar) on Apr 30, 2014 at 15:59 UTC

    Howdy PublicAccess

    Why are you looping on the same @aminos array? This is a little bizzarre.

    For troubleshooting purposes, before you get into your outer for loop, could you do something like:

    use Data::Dumper; print Dumper(\@aminos);

    ... and then show us what the output is? I suspect you'll need to change the way you're accessing the @aminos array...

    *My* tenacity goes to eleven...
Re: local and global variable problem
by Old_Gray_Bear (Bishop) on Apr 30, 2014 at 16:06 UTC
    Welcome to the world of Debugging My Code.

    At first glance I'd say that %matrix doen't contain what you think it does. Add this following the inner foreach:

    print ("DEBUG>>", $matrix->{$key}->{$key2}, "<<DEBUG\n");
    to see what is really in your matrix. Then, start working through your logic and figure out why those fields are empty.

    I Go Back to Sleep, Now.


Re: local and global variable problem
by Bloodnok (Vicar) on Apr 30, 2014 at 16:04 UTC
    As thezip says, it looks like your reference to the HoH is suspect - if matrix is defined as %matrix (some information that you've neglected to provide), the reference probably ought to be $matrix{$key}->{$key2}.

    A user level that continues to overstate my experience :-))
Re: local and global variable problem
by Laurent_R (Canon) on Apr 30, 2014 at 22:33 UTC
    Although I do not really see how this might have an impact on you program, it might be useful to see how you declare the @matrix_numbers array. Just as a matter of check. But my best guess is that you are not visiting your array data structure correctly. The Data::Dumper module might help figuring out what your data looks like.