![]() |
|
Keep It Simple, Stupid | |
PerlMonks |
Re^3: foreach doesn't select array elementby ELISHEVA (Prior) |
on May 04, 2009 at 14:37 UTC ( #761740=note: print w/replies, xml ) | Need Help?? |
Your confusion may be coming from the order in which @ and [3] are being handled. @ first converts $fldref from an array reference into an array. Then it lets you use normal array element syntax, @$fldref[3], to access its elements. In other words, @$fldref[3] is equivalent to $fldref->[3]. You might be able to see this more clearly if we replace $fldref->[3] with a variable, like this:
$fldref->[3] is $aVars, a reference to an array and not the dereferenced array, @$aVars. So when you loop on foreach (@$fldref[3]) you are really looping on foreach($fldref->[3]) which is just a loop over the single element $aVars. That is why you dump only once inside the foreach loop and see the $aVars array as a whole when you dump it rather than the individual elements of $aVars, each dumped separately. If you want to loop on the contents of $fldref->[3] you have to dereference it yet again and do @{$fldref->[3]} or @{@fldref[3]}. This works as expected:
Hope that helps, beth
In Section
Seekers of Perl Wisdom
|
|