Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^6: Perl list items

by aaron_baugher (Curate)
on Jul 09, 2012 at 19:24 UTC ( [id://980748]=note: print w/replies, xml ) Need Help??


in reply to Re^5: Perl list items
in thread Perl list items

You could, but that would be missing the point, and it appears that you're still confusing arrays and scalars. @A is not a scalar string; it is an array. Its first element, $A[0] is a string, which you created by assigning a string (the result of join) to it.

I don't know exactly what combine() does here, but it appears to be returning an array of references to arrays. If you just want the values from the first sub-array, there's no need to join them into a string and then split them out again. Just deference the array:

my @c = combine(5,@n); # get array of arrays my @A = @{$c[0]}; # get first sub-array

UPDATE: Thanks to Anonymous for the precedence correction.

Aaron B.
Available for small or large Perl jobs; see my home node.

Replies are listed 'Best First'.
Re^7: Perl list items
by AnomalousMonk (Archbishop) on Jul 10, 2012 at 07:51 UTC
    my @c = combine(5,@n); # get array of arrays my @A = @$c[0]; # get first sub-array

    Because the 'precedence' (if that's the proper term here) of the  @ array dereference sigil is so high, the expression  @$c[0] attempts to dereference an array reference held in the  $c scalar (not in the  @c array) and then access the element at index 0 in the referenced array. (Update: Actually, my first thought was that  @$c[0] would look like a 'degenerate' array slice, like @x[0]; not sure why the appropriate warning was not generated.) The expression  @{ $c[0] } is what is needed in the given example.

    >perl -wMstrict -MData::Dump -le "my @c = (['x', 'y'], ['a', 'b'], ['c', 'd']); my $c = ['foo', 'bar']; ;; my @A1 = @ $c[0] ; my @A2 = @{ $c[0] }; dd \@A1; dd \@A2; " ["foo"] ["x", "y"]
      To clarify  @$c[0] means the same as  @{$c}[0] meaning dereference $c, take array slice

      So

      $ perl -e " use Data::Dump; my $c = [qw/ a b c/]; dd @$c[1,2]; " ("b", "c")

      While References quick reference lists this item explicitly (item 2), its somewhat documented in perlreftut/perlref but its not recommended

        To clarify @$c[0] means the same as @{$c}[0] meaning dereference $c, take array slice

        Well... I'm not sure it's that much more clear. After all, "I know what I mean, ..."

        I'm still puzzled by the absence of a "... better written as ..." warning. Maybe the usage is just too far off the beaten track for warnings to stumble over.

        >perl -wMstrict -le "my @c = ('foo'); print @c[0]; ;; my $c = ['bar']; print @{$c}[0]; print @ $c [0]; " Scalar value @c[0] better written as $c[0] at -e line 1. foo bar bar

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-24 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found