D
DaLoverhino
I'm reading about perl references in a book called "Beginning Perl" by
James Lee. I read ahead to the use of ->, and [], but I'm tripped up
on the simpler examples prior to the use of ->, [], so I don't have
much confidence in what I have read.
Anyways the book has the following example:
$ref = [ 1, 2, [ 10, 20 ] ] ;
To access 20, you can do it a number of ways:
$inside = ${$ref}[2];
$element = ${$inside}[1];
Or the following way:
Book Example> $element = ${${ref}[2]}[1];
Well, I tried the latter way, and doesn't work. So I tried this
instead:
First Try> ${${$ref}[2]}[1];
This works. But I'm wondering why?
If you have a array_ref, you access an element by doing this:
Basic Example> ${$array_ref}[0];
So if ${$array_ref}[5] is itself an array reference, if I want to
access one of it's elements, why does the First T ry work? And why
does this one fail:
${$ ${$array_ref}[0] }[5]
^^^^^^^^^^^^^^^^^ --- The reference
^^^ ^^^ -- What you do to
access elements of an array
reference.
So I put some spaces to show you I took the reference "${$array_ref}
[0]" and applied
Basic Example> to it.
thanks.
James Lee. I read ahead to the use of ->, and [], but I'm tripped up
on the simpler examples prior to the use of ->, [], so I don't have
much confidence in what I have read.
Anyways the book has the following example:
$ref = [ 1, 2, [ 10, 20 ] ] ;
To access 20, you can do it a number of ways:
$inside = ${$ref}[2];
$element = ${$inside}[1];
Or the following way:
Book Example> $element = ${${ref}[2]}[1];
Well, I tried the latter way, and doesn't work. So I tried this
instead:
First Try> ${${$ref}[2]}[1];
This works. But I'm wondering why?
If you have a array_ref, you access an element by doing this:
Basic Example> ${$array_ref}[0];
So if ${$array_ref}[5] is itself an array reference, if I want to
access one of it's elements, why does the First T ry work? And why
does this one fail:
${$ ${$array_ref}[0] }[5]
^^^^^^^^^^^^^^^^^ --- The reference
^^^ ^^^ -- What you do to
access elements of an array
reference.
So I put some spaces to show you I took the reference "${$array_ref}
[0]" and applied
Basic Example> to it.
thanks.