Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > a reference to an element of an array or hash, how to create?

Reply
Thread Tools

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

 
 
S P Arif Sahari Wibowo
Guest
Posts: n/a
 
      08-05-2008
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!

--
(stephan paul) Arif Sahari Wibowo
_____ _____ _____ _____
/____ /____/ /____/ /____
_____/ / / / _____/ http://www.arifsaha.com/
 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      08-05-2008
S P Arif Sahari Wibowo <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      08-05-2008
S P Arif Sahari Wibowo wrote:
>
> 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
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
 
Reply With Quote
 
Zak B. Elep
Guest
Posts: n/a
 
      08-05-2008
S P Arif Sahari Wibowo <(E-Mail Removed)> writes:

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


--
I like the idea of 256 bits, though: 32 for the (Unicode) character leaves
room for 224 Bucky bits, which ought to be enough for anyone.
-- Roland Hutchinson, in alt.folklore.computers
 
Reply With Quote
 
S P Arif Sahari Wibowo
Guest
Posts: n/a
 
      08-05-2008
On Tue, 5 Aug 2008, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> perl -le 'my @x=(1..4); my $p=\$x[2]; $$p=9; print "@x"'


On Tue, 5 Aug 2008, John W. Krahn wrote:
> $ 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!

--
(stephan paul) Arif Sahari Wibowo
_____ _____ _____ _____
/____ /____/ /____/ /____
_____/ / / / _____/ http://www.arifsaha.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Once again pointer to first element vs pointer to array cast topointer to element Szabolcs Borsanyi C Programming 6 05-23-2008 11:06 AM
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
If given 1 element in an array of textboxes... find which element number it is \A_Michigan_User\ Javascript 4 11-16-2007 12:58 PM
How to delete array element and add to previous element Al Cholic Ruby 18 07-28-2007 04:56 PM



Advertisments