CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Array reference

 



perlfarmer
User

Oct 6, 2009, 7:09 PM

Post #1 of 6 (756 views)
Array reference Can't Post

I have written a subroutine that takes an array reference and processes the array. It is called somewhat like this:


Code
my @array; 
&processArray(\@array);


However, after calling &processArray, @array remains unaltered. Within &processArray the processing definitely takes place but the changes are not reflected in @array. Why is this? If \@array really is a reference to @array then &processArray should be operating directly on @array and not some copy which is local to the subroutine.


shawnhcorey
Enthusiast


Oct 7, 2009, 4:35 AM

Post #2 of 6 (749 views)
Re: [perlfarmer] Array reference [In reply to] Can't Post

It is difficult to answer without seeing the sub in question.

Also, don't use the & for sub calls; it makes `use strict;` ineffective.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


perlfarmer
User

Oct 7, 2009, 7:02 AM

Post #3 of 6 (746 views)
Re: [shawnhcorey] Array reference [In reply to] Can't Post

Basically the sub looks like this:


Code
my ($arrayRef) = @_; 
my @array = @{$arrayRef};
foreach my $index (0..$#array) {
$array[$index] = processed($array[$index]);
}



shawnhcorey
Enthusiast


Oct 7, 2009, 7:29 AM

Post #4 of 6 (739 views)
Re: [perlfarmer] Array reference [In reply to] Can't Post


In Reply To
Basically the sub looks like this:


Code
my ($arrayRef) = @_; 
my @array = @{$arrayRef};
foreach my $index (0..$#array) {
$array[$index] = processed($array[$index]);
}



@array is a local copy of the array. Therefore, the original is not changed, just the copy. Try (untested):

Code
my ($arrayRef) = @_; 
foreach my $index (0..$#$arrayRef) {
$arrayRef->[$index] = processed($arrayRef->[$index]);
}


See:
http://perldoc.perl.org/perlreftut.html
http://perldoc.perl.org/perlref.html

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


toolic
User

Oct 7, 2009, 7:35 AM

Post #5 of 6 (738 views)
Re: [perlfarmer] Array reference [In reply to] Can't Post

I believe your array is not modified in the main program because you are making a copy of the array reference inside your sub when you perform your assignment:


Code
my ($arrayRef) = @_;


This code will modify your main array by directly operating on the @_ array:


Code
use strict; 
use warnings;
use Data::Dumper;

my @array = 0 .. 2;
print Dumper(\@array);
processArray(@array);
print 'processed ', Dumper(\@array);

sub processArray {
for my $index (0..$#_) {
$_[$index] = processed($_[$index]);
}
}

sub processed { return $_[0] + 1 }

__END__

$VAR1 = [
0,
1,
2
];
processed $VAR1 = [
1,
2,
3
];


You can also modify your array in-place using map
instead of your sub:


Code
map { processArray($_) } @array;



perlfarmer
User

Oct 7, 2009, 8:05 AM

Post #6 of 6 (734 views)
Re: [shawnhcorey] Array reference [In reply to] Can't Post

Many thanks. :-)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives