Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Modifiying array elements with references

Reply
Thread Tools

Modifiying array elements with references

 
 
Anonymous user
Guest
Posts: n/a
 
      01-25-2006
Hello,

i'm a complete newB in perl especialy for references. I would like to
be able to modify some array elements through reference, because i
don't want a new array.

I would like to make the following script work:

------------------------------------------------------>8
#!/usr/bin/perl

use strict;
use warnings;

my @array = ( "1", "2", "3" ); # array of elements

print("before, element 1 = $array[1]\n");

my @refs = \(@array); # array of element references
my $elt_1 = $refs[1]; # reference on 2nd array element

print("before, element 1 = $elt_1\n"); # should print "2"

$elt_1 = "changed"; # should modify the array

print("after, element 1 = $array[1]\n"); # should print "changed"
------------------------------------------------------>8

by should i mean "i'd like to"

TIA for your help

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      01-25-2006
Anonymous user wrote:
> I would like to
> be able to modify some array elements through reference,
>
> I would like to make the following script work:
>
> ------------------------------------------------------>8
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my @array = ( "1", "2", "3" ); # array of elements
>
> print("before, element 1 = $array[1]\n");
>
> my @refs = \(@array); # array of element references
> my $elt_1 = $refs[1]; # reference on 2nd array element
>
> print("before, element 1 = $elt_1\n"); # should print "2"


In that case you need to dereference $elt_1:

print "before, element 1 = $$elt_1\n";
-------------------------------^^

> $elt_1 = "changed"; # should modify the array


You probably meant:

$$elt_1 = "changed";
----^^

> print("after, element 1 = $array[1]\n"); # should print "changed"


--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Anonymous user
Guest
Posts: n/a
 
      01-25-2006
thx a lot !!

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      01-25-2006
Anonymous user <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Hello,
>
> i'm a complete newB in perl especialy for references. I would like to
> be able to modify some array elements through reference, because i
> don't want a new array.
>
> I would like to make the following script work:
>
> ------------------------------------------------------>8
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my @array = ( "1", "2", "3" ); # array of elements
>
> print("before, element 1 = $array[1]\n");
>
> my @refs = \(@array); # array of element references
> my $elt_1 = $refs[1]; # reference on 2nd array element
>
> print("before, element 1 = $elt_1\n"); # should print "2"
>
> $elt_1 = "changed"; # should modify the array
>
> print("after, element 1 = $array[1]\n"); # should print "changed"
> ------------------------------------------------------>8


Gunnar has pointed out your failure to de-reference the scalar
refs you are using. Are you aware that you could also modify the
array through an array ref? That would only be one ref, instead
of one for each element that needs to be changed.

Both methods have applications, but the use of an array ref is
preferred, if possible.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
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
java swing jtextfield modifiying input brf Java 2 12-04-2006 04:54 PM
XML elements to JavaScript Array elements Conversion P XML 1 07-07-2006 09:08 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
EXE tools for viewing/modifiying Acess2000 data on remote server Mike Collier ASP General 2 01-20-2004 03:23 PM
Accessing elements in array ref of array references fatted Perl Misc 1 07-24-2003 08:58 PM



Advertisments