in reply to Re: foreach doesn't select array element
in thread foreach doesn't select array element

A lot better, with that @{}...

... but I don't understand why Data::Dumper seems to suggest that @$fldref[3] points to an array, see original post – the fourth field is an array, as there's no slash in front of it. That's probably what confuses me.

Shouldn't foreach (@$fldref[3]) {} automatically use the referenced array or spit out a warning about passing a single ref?

Thanks for the help, though. I think I've been staring at the same code for too long...

Replies are listed 'Best First'.
Re^3: foreach doesn't select array element
by Marshall (Canon) on May 04, 2009 at 10:08 UTC
    Shouldn't foreach (@$fldref3) {} automatically use the referenced array or spit out a warning about passing a single ref?

    I didn't read your code in detail, but when de-referencing a subscript'ed thing, you need to use {} to show "@" what it is operating upon, eg.. @{$fldref[3]}. If $fldref is just a single ref, then @$fldref works. But this doesn't work if $fldref has a subscript.

      Aha. So perhaps I expected the @ in @$fldref[] to operate on the whole thing, whereas I need the @{} to point out the subscript should be on the inside to (de)refer to the right array...

      I admit I don't always immediately understand in complex variable statements which @{(@$var[3])->[0]}[3] subscript applies to which part... At one point 2 3 4 levels deep with one single ref, the whole operator precedence becomes a bit confusing.

      Thanks for the help.

Re^3: foreach doesn't select array element
by AnomalousMonk (Archbishop) on May 04, 2009 at 10:31 UTC
    Shouldn't  foreach (@$fldref[3]) {} automatically use the referenced array or spit out a warning about passing a single ref?
    foreach (see perlsyn) will iterate over the list that you build for it. If you build a list by de-referencing a reference to an array (an array which may have zero or one or more elements) or by simply specifying a single scalar (which may be a reference to an array of zero or more elements), that's your business.

      But why does Dump show "a", "b", "c", "d" and not \"a", "b", "c", "d"? It seems to show an ordinary array. You don't need to dereference anything in the following basic array:

      [ "anna", "beth", "christie", "denise", ] foreach my $girl (@arr) { print "$girl\n"; }

      ... and if it's a ref to an array instead of an array itself, shouldn't Dump show \... in my original post? It looks as if it's an ordinary by-value array because there's no slash in front of it to point out it's a reference.

      Thanks!