in reply to Preserve array contents in for() loop

for my $b (my@c = @a) {

In other words, make a copy. However, if you want complete safety, you will need to make a deep copy, not the shallow copy in my example. Storable has a dclone() method, which may be of use.

------
We are the carpenters and bricklayers of the Information Age.

Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

I shouldn't have to say this, but any code, unless otherwise stated, is untested

Replies are listed 'Best First'.
Re^2: Preserve array contents in for() loop
by dda (Friar) on Sep 21, 2004 at 13:06 UTC
    Thanks for the answer, though I had removed the similar code from my question before posting it. :) Could you please elaborate on 'deep copy vs. shallow copy'?

    --dda

      Let's say you have an array of hashes. Well, that's really an array of hash references. If you do foreach my $x (my @b = @a), what you're doing is making a copy of the hash references. So, $x = 3; won't affect @a, but $x->{foo} = 3; will affect $a[2]{foo}, if $x was aliased to the third element in @a, for instance.

      A deep copy will make a copy of both @a and everything that each element in @a might have, should they be references themselves to stuff, and if that stuff is a reference, etc.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

      I shouldn't have to say this, but any code, unless otherwise stated, is untested