Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Dynamic hash keys

by markdibley (Sexton)
on Aug 17, 2010 at 10:33 UTC ( [id://855462]=note: print w/replies, xml ) Need Help??


in reply to Re: Dynamic hash keys
in thread Dynamic hash keys

Thanks for the reply. I don't think my requirements are particularly unusual, just that I am not doing it right.

I have a sample that has multiple tests. When a test is assigned to a sample it has a temporary id. After it is verified it is given a permanent id.

I want to put the tests in the hash indexed by their ids for quick retrival by their id and change the id when necessary.

Did I mention that sample and test are objects (kind of - as I don't have any OO training they are quite quasimodo in their OOness). Which means if I normalise and the test id is set in the sample object then I cannot access that id from the test object.

I am probably making things too hard for myself. I have read some of the OO tutorials, but when I get to the bits that I don't know and (definitely) need the language of the tutorial suddenly becomes "encrypted". I'll keep trying.

Thanks again

Replies are listed 'Best First'.
Re^3: Dynamic hash keys
by SuicideJunkie (Vicar) on Aug 17, 2010 at 13:58 UTC

    Rather than changing the ID, why not give the test another attribute ('validated' for example)?

    Then you don't have to worry about collisions with pre-existing IDs when validating.

    foreach my $currentTestID (keys %$tests) { $tests->{$currentTestID}{validated} = validate($tests->{$currentTe +stID}); }
    sub doValidatedTests { my $tests = shift; doTest($_) foreach ( grep {$tests{$_}{validated}} keys %$tests ); }

    When the IDs don't change, you're free to tell the test object what its ID is when you create it, and it will be valid until you drop the test from your hash and the test itself gets garbage collected shortly thereafter.

Re^3: Dynamic hash keys
by moritz (Cardinal) on Aug 17, 2010 at 15:10 UTC

    One solution is to keep separate hashes for objects with temporary id, and for those which are are confirmed.

    When one object is verified, you delete it from the hash that is keyed by temporary ID, assign it its new, permanent ID, and insert it into the hash that holds on the confirmed objects.

    Perl 6 - links to (nearly) everything that is Perl 6.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-26 08:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found