Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Will recursive code update data in an array?

by Masem (Monsignor)
on Apr 25, 2001 at 01:18 UTC ( [id://75271]=note: print w/replies, xml ) Need Help??


in reply to Will recursive code update data in an array?

If you pass the array as a reference, yes, the array elements can be modified. But if you pass the array directly, a copy will be made, and only the copies will be modified, the original array will not be changed:
my @a = [ 1 .. 10 ]; by_value ( 5, @a ); # @a still is [1..10] by_ref( 5, \@a ); # @a will be [120, 240,... ] sub by_value { my ( $time, @a ) = @_; if ( $time == 0 ) return; foreach my $b ( @a ) { $b *= $time; } by_value( $time--, @a ); } sub by_ref { my ( $time, $a_ref ) = @_; if ( $time == 0 ) return; foreach my $b ( @$a_ref ) { $b *= $time; } by_ref( $time--, $a_ref ); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://75271]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-24 12:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found