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

Hmm, the solution is correct but, in my view, the explanation seems to miss the main point:

I don't think so Laurent_R, that is impossible
The error message says Can't use string(".") as an ARRAY ref
My answer title says (to create an arrayref use [ ] )
My answer says split returns a list, to make an array you need [ 'square' , 'brackets' ]

your view is tiny

  • Comment on Re^3: Problem with traversing a two dimensional array (to create an arrayref use [ ] )
  • Download Code

Replies are listed 'Best First'.
Re^4: Problem with traversing a two dimensional array (to create an arrayref use [ ] )
by Laurent_R (Canon) on Jan 16, 2014 at 18:01 UTC

    split returns a list, to make an array you need 'square' , 'brackets'

    Well, this:

    (split /$separator/, $line)
    gives you an array, on which you can use an index to find one element, for example:
    my $third_elmnt = (split /$separator/, $line)[2];
    On the other hand, this:
    [split /$separator/, $line];
    does not give you an array, but an arrayref i.e. a single scalar. And an arrayref is what you need to oush onto an array if you want to build an AoA.

    You probably know all that, I was only objecting to the fact that this was not made very clear in my humble opinion.

        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.

      gives you an array, on which you can use an index to find one element, for example:

      No, a list is a list and not an array, perl lets you index a list, it doesn't make a list an array

      On the other hand, this: does not give you an array, but an arrayref i.e. a single scalar. And an arrayref is what you need to oush onto an array if you want to build an AoA.

      When you make an arrayref, what is that reference pointing to?
      An array right?
      so creating an arrayref creates an array (it make an array )

      You probably know all that, I was only objecting to the fact that this was not made very clear in my humble opinion.

      Well, if you wrote something like:
      to clarify
      to elaborate
      to make more clear
      to make more clear some details
      to make more clear some subtle details
      ... some kind of words other than ... miss the main point

      I probably wouldn't have responded

      Correct corrections are welcome ; elaborations are usually welcome (if they don't go off topic from main thread too much)

      wrong corrections not so much; they call the duty

        This anonymonk is not me. He's right, but frankly he's kind of a jerk.