in reply to Re^4: Using Array of Arrays
in thread Using Array of Arrays

The key is "as late as possible". If you need to retain a value determined in a loop then the variable has to be declared outside the loop:

my $result; for my $loopVar (...) { ... $result = ...; ... } if (defined $result) { # use the result } else { # result not determined (or undef) }

Note that $loopVar is magical. It gets aliased to each value in the list that the loop iterates over. In particular It does not retain the value from the last iteration of the loop even if declared outside the loop! Always either use the default variable($_) or explicitly declare the loop variable as shown in the code sample.


Perl is environmentally friendly - it saves trees

Replies are listed 'Best First'.
Re^6: Using Array of Arrays
by Aim9b (Monk) on Sep 24, 2007 at 15:21 UTC
    So if I declare a field outside of a loop (as in the my $result above), I can modify it inside the loop, and still have access to it outside, when the loop is done, but, I can't depend on the actual LOOP variable ($loopVar)to be set to any specific value, anywhere but INside the loop, correct? Thanks for your help and patience, Grandfather, this is getting more fun by the minute.