Flame has asked for the wisdom of the Perl Monks concerning the following question:

Is it possible to reference a specific array element? For instance:

my @array = (21..102); my $ref = \$array[5]; $$ref = 4; print $array[5];

Essentially I want to be able to pass this reference to an element of the array to a sub for editing. Or more precicely, I want to go through the array one element at a time and pass one argument at a time to another function for modification. Any idea how I would go about this? Is it even allowed? While I do use normal refs on a regular basis, this is something I'm clueless for... so any help would be appreciated, down to a technical explanation of how they do referencing in perl. (I'm curious but I lack the skill in other languages to actually understand how perl itself was written... hehe)

Thank you for any assistance or advice as to other ways I could achieve this.

Note: I actually did try the equivilant of the above... yielded things like \45 instead of a reference...





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

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

Replies are listed 'Best First'.
Re: reference to array element?
by gjb (Vicar) on Jan 30, 2003 at 01:59 UTC

    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-

      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)

      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)