Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I have an array of hashes. Well, technically it is an array of hash references, which AFAIK is the only way to construct an array of hashes in Perl. This array is declared globally. Let us call this array AoH.

I also have a globally declared hash, let us call it globHash.

My script jumps around from subroutine to subroutine. Each subroutine makes a few changes to globHash and then calls one or more (usually more) other subroutines which do the same. That is the reason that globHash is declared globally, because it is constantly being changed, re-changed, and changed again.

Eventually, I come to the end of one chain of subroutine calls. At the end of this chain, I push globHash onto AoH like this

push @AoH, \%globHash;
Then, through the magic of the run-time stack, I back up to the beginning of this chain, and follow a different one until that chain, too, comes to an end, and I push the hash onto the array again. Lather, rinse, repeat as necessary (and it is necessary to do so many times).

When all is said and done, I find that I have only succeeded in pushing dozens of identical hashes onto the array. All of the array entries, no matter when they were pushed, have the same values as the last hash I pushed onto the array.

Obviously, references are more like pointers than I had imagined. Now that this has happened, I am not surprised that it has done so (how impressive of me, predicting events that have already occurred). The question remains, now that I know this happens, how do I stop it? If I was righting this in C++, I would do

hash *globHash; array<hash>AoH; // do stuff ... //much later AoH.push( globHash ); delete globHash; globHash = new *hash; // now I have a fresh new hash!
So my question to you, O monks of wondrous knowledge, is this: is there some sort of delete/new equivalent in Perl (and I know that OO constructors are sometimes named new, but that isn't even remotely like what I'm talking about)? Can I get the hash to "reset" itself so that, when I take its reference a second time, it will be a different reference from when I took it the first time?

Some people drink from the fountain of knowledge, others just gargle.


In reply to O, the horrors of references and complex data structures by DeusVult

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-25 06:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found