Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re^5: Perl list items

by robertw (Sexton)
on Jul 09, 2012 at 17:57 UTC ( #980733=note: print w/ replies, xml ) Need Help??

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

Thank you for your help, but how would you guys take the first five items(numbers and letters) from this Scalar string that is @A? Can i use split for that?

Comment on Re^5: Perl list items
Re^6: Perl list items
by aaron_baugher (Deacon) on Jul 09, 2012 at 19:24 UTC

    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.

      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


        $ 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

Re^6: Perl list items
by davido (Archbishop) on Jul 10, 2012 at 00:34 UTC

























Log In?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2014-12-25 21:27 GMT
Find Nodes?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?

    Results (163 votes), past polls