Re: Arrow dereference operator question



I did suspected it would have something to do with the difference between
array and list contexts.
Your analysis seems to be correct.

Thank you for solving this puzzle :)

Regards

On 5/31/07, Chas Owens <chas.owens@xxxxxxxxx> wrote:

On 5/31/07, David Unric <dunric29a@xxxxxxxxx> wrote:
> Based on perlref documentation arrow operator between brackets
subscripts
> may be omitted so the following code is valid:
>
> @array = ( [1, 2], [3, 4] );
> $element = $array[0][0]; # shorthand for $element = $array[0]->[0]
>
>
> Could somebody explain why it causes syntax error when the above rule is
> applied to returned value of a subroutine ?
>
> sub mysub {
> @array = ( [1, 2], [3, 4] );
>
> return @array;
> }
>
> $element = (&mysub)[0][0]; # $elem = (&mysub)[0]->[0] is valid
> ------------------------------
> syntax error at testarr.pl line 7, near "]["
>

My best guess is that the problem here is that (mysub())* is a list
not an array. Lists are not multidimensional. Try

my $element = ([1, 2], [3, 4])[0][0];

Notice how it gets the same error?

The simple solution is to use the arrow, or have the sub return an
arrayref. Here are some ways to do it:

#!/usr/bin/perl

use strict;
use warnings;

sub list {
my @array = ( [1, 2], [3, 4] );
return @array;
}

sub aref {
my @array = ( [1, 2], [3, 4] );
return \@array;
}

print (
(list())[0]->[0], "\n",
"${[list()]}[0][1]\n",
"${aref()}[1][0]\n",
(aref())->[1][1], "\n"
);



* don't use &mysub unless you know why you are doing it, use mysub()
instead



Relevant Pages

  • Re: Arrow dereference operator question
    ... Could somebody explain why it causes syntax error when the above rule is ... sub mysub { ... not an array. ... don't use &mysub unless you know why you are doing it, ...
    (perl.beginners)
  • Using scalar() on function return values
    ... the scalar() function returns when called with functions that return ... lists and/or arrays. ... elements in the array, but what if scalaris called on a list that ...
    (comp.lang.perl.misc)
  • Re: garbage collection problem in large linked lists
    ... Instead all buckets are allocated at once in an array. ... VG.net, which must be very scalable, but only for lists which would normally ... Linked lists require pointer dereferencing in order to traverse. ... When you run out of space, you allocate a new smaller array. ...
    (microsoft.public.dotnet.framework.performance)
  • Re: Dict sharing vs. duplication
    ... array to enforce unique keys and I use lists to enforce order. ... API in a page or two of Tcl code, it isn't so much a missing feature ... OpenACS database API which creates a new specialized data structure, ...
    (comp.lang.tcl)
  • Re: Multiple OUs?
    ... something is up with adding multiple values to the array... ... ' If the AD enumeration runs into an OU object, call the Sub again to ... strLine = Trim ... strNewContents = strNewContents & strLine & vbCrLf ...
    (microsoft.public.scripting.vbscript)