Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: How to build a hash?

by Ben Win Lue (Friar)
on Apr 25, 2005 at 12:35 UTC ( #451158=note: print w/ replies, xml ) Need Help??


in reply to Re: How to build a hash?
in thread How to build a hash?

I had myself problems with it recently. As long as you storing scalars, it work perfectly as coded in the examples above. When it comes to references, your fears become true.
There is no such thing as a malloc in C or a new in C++(Ok, there is a new, but somehow different). What you have to do is placing brackets around an array or curly braces around a hash to get a copy in memory, that is not overwritten.
For example: to create a hash of references to hashes:

my %my_hash; sub my_store($\%){ my($key,$ref) = @_; $my_hash{$key} = {%$ref} }
(example not tested)


Comment on Re^2: How to build a hash?
Download Code
Re^3: How to build a hash?
by davorg (Chancellor) on Apr 25, 2005 at 13:10 UTC
    I had myself problems with it recently. As long as you storing scalars, it work perfectly as coded in the examples above. When it comes to references, your fears become true.

    You sound a little confused. You can only ever store scalars in a hash. A reference is just a special kind of scalar. You can never overwrite an entire hash by assigning to one of its values.

    my %my_hash; sub my_store($\%){ my($key,$ref) = @_; $my_hash{$key} = {%$ref} }

    I'd guess it's almost certainly the unnecessary use of prototypes that is confusing you there :)

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Sorry, maybe my use english is a bit confusing... 8-)
      My problem was, I stored a reference to a hash (%hash_a) in a hash (%hash_b). Worked perfectly for one record.
      Now, when i overwrote the contents of %hash_a, the contents in %hash_b where overwritten, too.
      The whole stuff worked for more than one record only after I introduced the curly braces.

        There are two ways to take a reference to a hash.

        If you use the \%hash syntax then you are taking a reference to the _original_ hash. Any updates that you make through that reference will obviously update the original hash (as, actually, there is only ever one hash).

        If you use the { %hash } syntax then you are creating a new anonymous hash which is a _copy_ of your original hash and getting a reference to this new hash. Any updates you make through this reference will update the copy of the hash and not the original hash.

        I recommend reading "perldoc perlreftut" and "perldoc perlref" to clarify these concepts.

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (12)
As of 2014-09-30 20:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (383 votes), past polls