in reply to Duplicating array contents
Now $p is a reference to @a. You can change the contents of @a via $p.my $p = \@a;
$p is still a reference, but a reference to an anonymous array that contains the contents of @a. You can change the contents of $p, but that will not affect @a (as long as @a didn't contain any references ... we won't go there). You could also copy @a like so:my $p = [@a];
Instead of merely printing the contents out, i find using Data::Dumper to be much more helpful:my @p = @a;
use strict; use warnings; use Data::Dumper; my @a = (1,2,3,4); my @copy = @a; my $ref = \@a; my $refcopy = [@a]; @a = (); print "orginal: ", Dumper \@a; print "array copy: ", Dumper \@copy; print "reference: ", Dumper $ref; print "ref copy: ", Dumper $refcopy;
UPDATE:
This is Perl, so instead of using the cluttered:
use the more readable (IMHO at least):foreach (@{$p}) { print "element $_\n"; }
Have fun. :)print "element $_\n" for @$p;
jeffa
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Duplicating array contents
by Aristotle (Chancellor) on Dec 16, 2002 at 19:29 UTC |