Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Before I pass it, however, I'd like to make a copy of the entire data structure, one that will not get modified. None of the following examples do what I need, but they illustrate my train of thought...
I was certain the last example would work...What am I misunderstanding so fundamentally?# this is an arrayref or hashref containing # other arrayrefs and/or hashrefs $orig_ref; # this makes a copy of the *reference* to the same data # DOESNT WORK $copy_ref = $orig_reg; # this dereferences, then references the original data # DOESNT WORK $copy_ref = ref($orig_ref) eq 'ARRAY' ? \@{$orig_ref} : ref($orig_ref) eq 'HASH' ? \%{$orig_ref} : undef; # this derefs, copies, then refs the copy # SHOULD WORK BUT DOESNT? if(ref($orig_ref) eq 'ARRAY') { @copy = @{$orig_ref}; # deref the original @copy2 = @copy; # COPY the original data? $copy_ref = \@copy2; # ref the copied data? } elsif(ref($orig_ref) eq 'HASH') { %copy = %{$orig_ref}; # deref the original %copy2 = %copy; # COPY the original data? $copy_ref = \%copy2; # ref the copied data? }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to copy a referenced data structure?
by jettero (Monsignor) on May 18, 2007 at 17:18 UTC | |
by Anonymous Monk on May 18, 2007 at 18:18 UTC | |
|
Re: How to copy a referenced data structure?
by shmem (Chancellor) on May 18, 2007 at 18:09 UTC | |
by Anonymous Monk on May 22, 2007 at 06:22 UTC | |
by syphilis (Archbishop) on May 22, 2007 at 06:49 UTC | |
|
Re: How to copy a referenced data structure?
by kyle (Abbot) on May 18, 2007 at 17:23 UTC | |
|
Re: How to copy a referenced data structure?
by scorpio17 (Canon) on May 18, 2007 at 17:55 UTC |