Reference Question.

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.
 
X

xhoster

DaLoverhino said:
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.

Right. The inner part is the same as $ref[2] (I got a warning to that
affect, because I turned on warnings), which is trying to access an element
of the undeclared variable @ref (I got an error to that effect, because
I turned on strict).

So this is a typo, which you have corrected below.
So I tried this
instead:

First Try> ${${$ref}[2]}[1];

This works. But I'm wondering why?

Um, because that is how Perl works. I'm not sure what kind
of answer you are looking for.
If you have a array_ref, you access an element by doing this:

Basic Example> ${$array_ref}[0];

Why are you changing both the name of the variable, and the relevant
indices, in the middle of an example? What can this do other than
cause confusion?

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

Strip off the ${...}[5] and you get

$${$array_ref}[0], which is the same as

${${$array_ref}[0]}

As you say, ${$array_ref}[0] is itself an array ref, so, lets call it
$array_ref2, yielding:

${$array_ref2}

So the extra $ is trying to dereference that inner array ref, but is trying
to do so as if it were a reference to a scalar rather than to an array.
Yielding the error "Not a SCALAR reference". If you just delete it, then
the array ref (called $array_ref2) falls into the hands of the earlier
stripped away construct, ${...}[5], which dereferences it like the array
ref it is, and all is well.

All this tedium is a very good reason to use -> instead whenever possible.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
B

Bart Lateur

DaLoverhino said:
$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.

That's because you did a poor job in copy/paste.

$element = ${${$ref}[2]}[1];

I hope the book didn't spell it the way you did.
 
D

Dr.Ruud

DaLoverhino schreef:
$ref = [ 1, 2, [ 10, 20 ] ] ;

To access 20, you can do it a number of ways:

$inside = ${$ref}[2];
$element = ${$inside}[1];

I would just get it with $ref->[2][1].
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,998
Messages
2,570,242
Members
46,834
Latest member
vina0631

Latest Threads

Top