a reference to an element of an array or hash, how to create?

  • Thread starter S P Arif Sahari Wibowo
  • Start date
S

S P Arif Sahari Wibowo

Hi!

Do you know how to create a reference to an element of an array
or hash? For instance I have an array @a, and I like $a2r
contains a reference to $a[2]. Anyway to do it in perl?

Keep in mind this is not the case where $a[2] contain a
reference and $a2r contains the same reference, but instead $a2r
contain a reference to the scalar stored in $a[2].

Let me explain more using the pseudo code below, RefTo represent
the mechanism in question:

my @a= (1, 2, 3, 4);

my $p= RefTo $a[2];

$$p= 9;

print join(",", @a) # should print 1,2,9,4


So, any idea wether there is such mechanism as RefTo in Perl?

Thanks!
 
X

xhoster

S P Arif Sahari Wibowo said:
Hi!

Do you know how to create a reference to an element of an array
or hash? For instance I have an array @a, and I like $a2r
contains a reference to $a[2]. Anyway to do it in perl?

The Perl "reference to" operator is the backslash.
my @a= (1, 2, 3, 4);

my $p= RefTo $a[2];

$$p= 9;

print join(",", @a) # should print 1,2,9,4

perl -le 'my @x=(1..4); my $p=\$x[2]; $$p=9; print "@x"'
1 2 9 4

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

John W. Krahn

S said:
Do you know how to create a reference to an element of an array or hash?
For instance I have an array @a, and I like $a2r contains a reference to
$a[2]. Anyway to do it in perl?

Keep in mind this is not the case where $a[2] contain a reference and
$a2r contains the same reference, but instead $a2r contain a reference
to the scalar stored in $a[2].

Let me explain more using the pseudo code below, RefTo represent the
mechanism in question:

my @a= (1, 2, 3, 4);

my $p= RefTo $a[2];

$$p= 9;

print join(",", @a) # should print 1,2,9,4

$ perl -le'my @a = ( 1, 2, 3, 4 ); my $p = \$a[ 2 ]; $$p = 9; print join
",", @a'
1,2,9,4



John
 
Z

Zak B. Elep

S P Arif Sahari Wibowo said:
Do you know how to create a reference to an element of an array or hash?
For instance I have an array @a, and I like $a2r contains a reference to
$a[2]. Anyway to do it in perl?

,----[ perl -de 0 ]
| alteran:~ zakame$ perl -de 0
|
| Loading DB routines from perl5db.pl version 1.28
| Editor support available.
|
| Enter h or `h h' for help, or `man perldebug' for more help.
|
| main::(-e:1): 0
| DB<1> @a = ( 1, 2, 3 )
|
| DB<2> $a2r = \$a[2]
|
| DB<3> x $a2r
| 0 SCALAR(0x190fe24)
| -> 3
| DB<4> x $$a2r
| 0 3
`----
 
S

S P Arif Sahari Wibowo

perl -le 'my @x=(1..4); my $p=\$x[2]; $$p=9; print "@x"'

$ perl -le'my @a = ( 1, 2, 3, 4 ); my $p = \$a[ 2 ]; $$p = 9; print join ",", @a'

Duh! I should try it first, didn't think it will be that easy
before.

Thanks everyone!
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top