I'm writing a recursive routine that does destructive processing on a passed ref to an AoHoA... but as I return from each level of recursion, I need to 'undo' the changes made.

Basically, I need to provide a deepcopy of (or some subtree of) the structure as I recurse in so that I retain an unchanged copy as I come back up the tree.

It sounds to me as if you are all barking up the wrong tree. From the definition of your problem, it sounds like you just want to use local, to localise a copy of the data structure you are working on. When you leave the recursed scope, the data structure will resume its previously stored values, without any effort on your part.

If you need to make permanent modifications from time to time then you will have to arrange a signalling mechanism between levels, so that the parent receives the replacement of the child subtree, and does the replacement.


update for aragorn: you are free to initialise a localised variable to the value of the variable it is localising, which gets rid of that problem.

update for BrowserUk: Yes, indeed locality doesn't propagate, but can't you just localise as you go, or does that make the code too messy? Can you show an example of the data structure?

#! /usr/bin/perl -w <code> use strict; use vars qw/ $r $r2 /; $r = { foo => [ { one => 1, two => 2, three => 3 }, { four => 4, five => 5, six => 6 }, ], }; { print "orig : $r->{foo}[1]{five}\n"; $r2 = $r->{foo}; local $r2->[1]{five} = $r2->[1]{five} + 500; print "then : $r->{foo}[1]{five}\n\n"; } print "after: $r->{foo}[1]{five}\n";

Hope this helps, i.e. what I am trying to get at is that it is always nice to try and get the language to do the dirty work for you, rather than writing code in the language to achieve the same functionality.


print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'

In reply to Re: Deepcopy of complex structures. by grinder
in thread Deepcopy of complex structures. by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.