Modifiying array elements with references

A

Anonymous user

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
 
G

Gunnar Hjalmarsson

Anonymous said:
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"
 
A

Anno Siegel

Anonymous user said:
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
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top