in reply to Preserve array contents in for() loop
You are iterating over an array
for my $b (@a) { ... }
and actually changing its elements
$b *= 2;
So, obviously @a will change. You getting exactly what you are doing.
Others have explained that you can make a copy of @a and then iterate over the copy. That way @a will not change.
However, why don't you explain what exactly you want to achieve so we can help you better. The above snippet you have provided shows no syntactical errors, just perhaps an unintentional logical error. A list (an array) is just a variable, so by definition, it contains varying values. In most cases, you want this to be assignable. If you don't want it to change, just don't do anything to it... do things with it instead.
By the way, you don't need to pass a ref to Dumper. It can take just about any variable, so just give it the array.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Preserve array contents in for() loop
by davido (Cardinal) on Sep 21, 2004 at 15:12 UTC | |
by punkish (Priest) on Sep 21, 2004 at 15:24 UTC |