Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Deepcopy of complex structures.

by bart (Canon)
on Jan 21, 2003 at 11:39 UTC ( [id://228653]=note: print w/replies, xml ) Need Help??


in reply to Deepcopy of complex structures.

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.

...

Is there something better?

Yeah... avoid doing the processing in a destructive manner, if you can. That surely sounds like the wrong approach. If you absolutely need it, you can keep track of what nodes you have already visited by storing a stringified reference in a hash, for example.

Replies are listed 'Best First'.
Re: Re: Deepcopy of complex structures.
by BrowserUk (Patriarch) on Jan 21, 2003 at 13:23 UTC

    Oh, if only life were so easy. :^)

    Basically, the algorithm recursively processes this data structure, and deeper levels of the recursion rely on earlier levels having removed branches of the structure. This pruning effectively stops previously processed and rejected branches of the data being reprocessed at any given level.

    However, as the recursion unwinds (having so far failed to resolve the solution), I need to restore the pruned branches in order that they may be considered when walking other branches taken higher (shallower) in the recursion process. The structure contains many cross-references at the leaf nodes. It is these that get pruned as the process progresses at the deeper levels, but that must be restored as the recursion unwinds.

    The routine only selectively clones subtrees at any given level and passes these in when it recurses, so it is, in effect, tracking the changes and unwinding them. but given that the nature and depth of the structure is variable between runs, coming up with a generalised alternative tracking mechanism is more complex (and costly) than cloning the subtrees to lexicals as I go.


    Examine what is said, not who speaks.

    The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://228653]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-24 03:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found