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

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.
  • Comment on Re^6: Problem with traversing a two dimensional array (to create an arrayref use [ ] )
  • Select or Download Code

Replies are listed 'Best First'.
Re^7: Problem with traversing a two dimensional array (to create an arrayref use [ ] )
by Anonymous Monk on Jan 16, 2014 at 19:50 UTC
    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.
        Accessing elements with an index ... you can do with an array and not with a list.

        Why do you say that? Are you assuming that "list" means "linked list"? That's not what we mean by "list" in Perl.

        I think you're right about the element count in scalar context. I seem to remember reading something like "there's no standard way of converting a list to a scalar," but I can't remember where.

        Anyway, I'm not trying to be argumentative, I'm just trying to clear up misunderstandings people might have with Perl. And I sometimes post anonymously when I'm on public wireless access points. Happy perling to you.