Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: foreach doesn't select array element

by Anonymous Monk
on May 04, 2009 at 08:52 UTC ( #761675=note: print w/replies, xml ) Need Help??


in reply to foreach doesn't select array element

Why would the foreach change anything, its the same reference?
# ALL SAME dump(@$fldref[TXT]); foreach my $tmp (@$fldref[TXT]) { dump($tmp); } my ( $foobar ) = @$fldref[TXT]; dump($foobar);
You probably want
foreach my $tmp (@{ $fldref[TXT][0] }) { dump($tmp); }
When in doubt, show Data::Dumper output

Replies are listed 'Best First'.
Re^2: foreach doesn't select array element
by december (Pilgrim) on May 04, 2009 at 09:52 UTC

    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...

      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.

      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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://761675]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2022-05-29 09:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (101 votes). Check out past polls.

    Notices?