in reply to reference to array element?

The following code does what I would expect, it works ;-)

#!perl use strict; use warnings; my @array = (1..10); print $array[2], "\n"; # prints 3 my $ref = \$array[2]; $$ref = 11; print $$ref, "\n"; # prints 11 print $array[2], "\n"; # prints 11

If your code doesn't do something similar, it would help to post the actual fragment.

Hope this helps, -gjb-

Replies are listed 'Best First'.
Re: Re: reference to array element?
by Flame (Deacon) on Jan 30, 2003 at 02:27 UTC
    Flame blinks.
    Flame blinks again.
    Flame stares at code with open mouth and looks ready to start hitting delete repeatedly.
    Flame restrains himself from hitting delete repeatedly.
    Flame settles down to explain his foolish mistake.

    Ok... I figured out what threw me off. I was using Devel::ptkdb to try to trace down a problem (I'll get to it in a min), when I noticed that the variables being passed were all "\####", when they were suppost to be references (Makes me wonder if you can change the constant of a number... think about it... my $num = \1; $$num = 543;  print 1; Anyway, so that explains the appearance that it wasn't really a reference. As to the actual problem... I was using $$_[0] to try to access the data within the sub, and that doesn't work... need ${$_[0]}.

    Anyway, thanks for helping me cut my debug time significantly by convincing me that what I was sure was the problem, wasn't.

    Edit:: Added link to Devel::ptkdb reference.


    My code doesn't have bugs, it just develops random features.

    Flame ~ Lead Programmer: GMS (DOWN) | GMS (DOWN)

Re: Re: reference to array element?
by Flame (Deacon) on Jan 30, 2003 at 02:14 UTC
    Looking at your code, I think that what I have might actually be doing what I told it to, though I can't be certain without a few quick tests. Of course, that leaves the question of "if that isn't the broken part, what is?"

    I'll post another followup as soon as I figure out what's goin on. Thanks for confirming my belief that that can be done (and that that's the way to do it.)



    My code doesn't have bugs, it just develops random features.

    Flame ~ Lead Programmer: GMS (DOWN) | GMS (DOWN)