in reply to How to build a hash?

Thank you, but would that not just rewrite the hash so only one element at a time will be existent in the hash? So you sure that elements will be just "append" to the hash if keys are not already existing?

Replies are listed 'Best First'.
Re^2: How to build a hash?
by davorg (Chancellor) on Apr 25, 2005 at 12:23 UTC

    Trust us. We're sure it'll work :)

    If you don't believe us then why not write a little test program.

    my %hash; $hash{one} = 1; $hash{two} = 2; $hash{three} = 3; foreach (keys %hash) { print "$_ -> $hash{$_}\n"; }
    --
    <http://www.dave.org.uk>

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

Re^2: How to build a hash?
by Jasper (Chaplain) on Apr 25, 2005 at 12:31 UTC
    No, like an array element ($array[$n]) the accessor of a hash element ($hash{'foo'}) is also an lvalue (basically you can assign a value to it). This method of accessing or modifying one value at a time doesn't affect the rest of the hash.

    You can also modify several elements at once using a hash slice, for example
    @hash{'foo','bar','baz'} = (1,2,3);
    Only when you say %hash are you definitely effecting the entire hash.
    %hash = ('foo',1,'bar',2'baz',3); # whole hash overwritten $hash{foo} = 1; # only one element created or modified @hash{@array} = (1,2,3); # only elements specified in @array overwritt +en, or created.
Re^2: How to build a hash?
by Ben Win Lue (Friar) on Apr 25, 2005 at 12:35 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.
    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)
      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.