Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^5: Problem with traversing a two dimensional array (to create an arrayref use [ ] )

by no_slogan (Deacon)
on Jan 16, 2014 at 18:09 UTC ( [id://1070841]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Problem with traversing a two dimensional array (to create an arrayref use [ ] )
in thread Problem with traversing a two dimensional array

Actually, there is a subtle difference between lists and arrays in Perl. Functions like split return lists, not arrays. See What is the difference between a list and an array?
  • Comment on Re^5: Problem with traversing a two dimensional array (to create an arrayref use [ ] )

Replies are listed 'Best First'.
Re^6: Problem with traversing a two dimensional array (to create an arrayref use [ ] )
by Laurent_R (Canon) on Jan 16, 2014 at 19:05 UTC
    Yeah, I know the differences between a list and an array. And I know that split returns a list, but it is slightly more tricky than that. If you do this:
    my $c = qw / 4 6 8 90/;
    $c's value becomes the last element of the list (90). But if you do this:
    $c = split / /, "the quick brown fox";
    $c is now 4, the number of elements of the array (I think this would also trigger a warning that @_ may be overwritten). But if you do this:
    $c = (split / /, "the quick brown fox")[2];
    now this behaves as an anonymous array et $c is assigned to "brown" (and there no longer any warning, because Perl sort of built an anonymous array). And if you do this:
    $c = [split / /, "the quick brown fox"];
    $c's value is now something like "ARRAY(0x80359d10)", i.e. an array reference. and you need something like this:
    print $c->[2];
    to print the third element of the array. This is really an array ref, not an array.
      Your terminology is a bit off. The construction (split...)[2] picks out one element of the list returned by split (it does not create an anonymous array). OTOH, [split...] creates an anonymous array (initialized by the list returned by split) and returns a reference to it. Saying [split...]->[2] would create an anonymous array, then return one element of it (and then discard the anonymous array).
        Oh, well, my terminology might be off and, although I think that I can claim to have a relatively good command of the English language, it is not my mother tongue and I may sometimes goof on some fine subtleties, but I was careful enough not to say that (split...) creates an anonymous array, but only that the thing does behave (at least in a number of respects) as if it were an anonymous array. Accessing elements with an index, manufacturing array slices, getting an element count in a scalar context, just to give a few examples, are things that you can do with an array and not with a list. But, OK, I have no desire to argue endlessly on this (especially not with an Anonymous Monk, where I don't even know if the person talking to me is always the same person), let's consider that you were right and forget it. I have no intention to argue any further on this.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-18 01:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found