$$listref[2][2] = "hello"; # what is $$listref ?
am 26.12.2007 17:40:23 von Florian Kaufmann
Hello
I am struggling a bit with the following example and text from
"Programming Perl".
$listref->[2][2] = "hello";
$$listref[2][2] = "hello";
(...) Therefore, it is $$listref and not $listref[2] that is taken to
be a reference to an array.
I would say, that $listref is a reference to the outer array, and $
$listref[2] (or the equivalent $listref->[2] or ${$listref}[2] ) a
reference to the third of the inner arrays. $$listref is invalid,
since it tries to scalar-dereference an array-reference. @$listref
would be ok, which array-dereferences the array-reference $listref.
Flo
Re: $$listref[2][2] = "hello"; # what is $$listref ?
am 26.12.2007 18:20:22 von Michele Dondi
On Wed, 26 Dec 2007 08:40:23 -0800 (PST), Florian Kaufmann
wrote:
>I am struggling a bit with the following example and text from
>"Programming Perl".
>
>$listref->[2][2] = "hello";
>$$listref[2][2] = "hello";
>(...) Therefore, it is $$listref and not $listref[2] that is taken to
>be a reference to an array.
I would say that it is ${$listref}[2] a.k.a. $listref->[2], i.e. the
element with subscript 2 in the array referenced by $listref, which is
also an arrayref.
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Re: $$listref[2][2] = "hello"; # what is $$listref ?
am 27.12.2007 21:52:57 von Ben Morrow
Quoth Florian Kaufmann :
>
> I am struggling a bit with the following example and text from
> "Programming Perl".
>
> $listref->[2][2] = "hello";
> $$listref[2][2] = "hello";
> (...) Therefore, it is $$listref and not $listref[2] that is taken to
> be a reference to an array.
>
> I would say, that $listref is a reference to the outer array, and $
> $listref[2] (or the equivalent $listref->[2] or ${$listref}[2] ) a
> reference to the third of the inner arrays. $$listref is invalid,
> since it tries to scalar-dereference an array-reference. @$listref
> would be ok, which array-dereferences the array-reference $listref.
Yes. The point being made is that the expression parses as
${ $listref }[2]->[2]
rather than the
${ $listref[2] }[2]
or
${ $listref[2]->[2] }
that a C programmer might expect from analogy with *a[i]. That is, the
variable here is something like
my $listref = [[...], [...], ...];
rather that something like
my @listref = ([...], [...], ...);
The wording is less than ideal: as you say, $$listref, by itself, is a
runtime error. I think the the idea was that $$listref and not
$listref[2] is the subject of the postfix-[] operator, but Perl's
array-deref operator doesn't work the same as C's: it comes in two
parts, $ ... [...], which are inseparable.
Ben