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

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.

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

Replies are listed 'Best First'.
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
      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).
Re^5: Problem with traversing a two dimensional array (to create an arrayref use [ ] )
by Anonymous Monk on Jan 17, 2014 at 01:32 UTC

    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.

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

        Fuck you too