Reference Question.

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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    = $[2];
$element = $[1];

Or the following way:

Book Example> $element = $[2]}[1];

Well, I tried the latter way, and doesn't work.  So I tried this

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

This works.  But I'm wondering why?

If you have a array_ref, you access an element by doing this:

Basic Example> $[0];

So if $[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:

           ${$       $[0]       }[5]
                        ^^^^^^^^^^^^^^^^^ --- The reference
            ^^^                                 ^^^  -- What you do to
access elements of an array

So I put some spaces to show you I took the reference "$
[0]" and applied
Basic Example> to it.


Re: Reference Question.

Quoted text here. Click to load it

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.

Quoted text here. Click to load it

Um, because that is how Perl works.  I'm not sure what kind
of answer you are looking for.

Quoted text here. Click to load it

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?

Quoted text here. Click to load it

Strip off the $[5] and you get

$$[0], which is the same as


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


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.


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

Re: Reference Question.

DaLoverhino wrote:

Quoted text here. Click to load it

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

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

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


Re: Reference Question.

DaLoverhino schreef:

Quoted text here. Click to load it

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

Affijn, Ruud

"Gewoon is een tijger."

Site Timeline