in reply to Problem with traversing a two dimensional array

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

for exampe  push @sandGrid, [ split '', $line ];

Say it with me:

{ 'hashes', 'are', 'curly', 'ones' }

[ 'arrays', 'are', 'square' ]

( 'lists are round' )

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

Replies are listed 'Best First'.
Re^2: Problem with traversing a two dimensional array (to create an arrayref use [ ] )
by Laurent_R (Canon) on Jan 16, 2014 at 07:15 UTC
    Hmm, the solution is correct but, in my view, the explanation seems to miss the main point: the main point is that, to construct an AoA, what needs to be pushed onto the larger array is not a list nor an array, but an array reference, and the sqare bruckets acts in this case as an array reference constructor.

      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

        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.