karthikin2asic has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have hash addresses in an array. Is it possible to access each of these hash addresses and add a key using references? Can you please share the code with me? Thanks, -Karthik

Replies are listed 'Best First'.
Re: Updating hash using it's reference ...
by AppleFritter (Vicar) on Jul 12, 2014 at 09:59 UTC

    It certainly is. For instance, the first element in your array is $array[0]; since this is a hash reference, you can deference it and turn it into a proper hash again by saying %{ $array[0] } (the whitespace isn't necessary; it just improves readability). And since that's just a hash, you can access its elements as usual, e.g. ${ $array[0] }{'somekey'}.

    That last bit of syntax is clunky, of course, so Perl provides the -> postfix operator for dereferencing and element access as well; using that, you can also write $array[0]->{'somekey'}, which is much more readable and makes it more obvious what's going on.

    Example:

    #!/usr/bin/perl use feature qw/say/; use warnings; use strict; my @array = ( { 'Fred' => 'Flintstone', 'Barney' => 'Rubble' } ); say $array[0]; say %{ $array[0] }; say ${ $array[0] }{'Fred'}; say $array[0]->{'Fred'}; $array[0]->{'Betty'} = 'Rubble'; say $array[0]->{'Betty'};

    Output:

    $ perl test.pl HASH(0x8002bc80) BarneyRubbleFredFlintstone Flintstone Flintstone Rubble $

    EDIT: removed superfluous use Data::Dumper; -- good catch, hexcoder. :)

      That last bit of syntax is clunky ... the -> postfix operator for dereferencing and element access as well ...

      And for even less clunky syntax, Perl understands that any array/hash/code/etc. reference below the topmost level can only be a reference, so it will use the  -> operator implicitly if you do not use it explicitly. It's only at the topmost level that you have to supply a  -> or not based on the nature of the variable you are accessing:  -> must be used with an array/hash/code reference: see Anonymonk's reply for an example of this.

      c:\@Work\Perl>perl -wMstrict -le "use feature qw/say/; use Data::Dump; ;; my @array = ( { 'Fred' => 'Flintstone', 'Barney' => 'Rubble', } ); ;; say $array[0]{'Fred'}; ;; $array[0]{'Marge'} = 'Simpson'; say $array[0]{'Marge'}; ;; dd \@array; " Flintstone Simpson [ { Barney => "Rubble", Fred => "Flintstone", Marge => "Simpson" }, ]

      See also perldsc (Perl Data Structures Cookbook).

        Ah, I didn't know that! Thanks for the tip, my enlightened brother!
        I would discourage not using the postfix dereferencing, it's the first clue that something is a reference! It matters because modifying references can have side effects on the thing that is referenced!
Re: Updating hash using it's reference ...
by Anonymous Monk on Jul 12, 2014 at 08:15 UTC
      Thanks a lot for the reply. Is there a way to directly use, instead of using a package?

        Data::Dump is only there for showing you the output. Access is done through plain Perl. See the links that Anonymous Monk already provided.

        Yes, try Applefritters demo code. Just leave out the line use Data::Dumper; before trying. It is not necessary.
Re: Updating hash using it's reference ...
by bulrush (Scribe) on Jul 12, 2014 at 15:59 UTC
    I like these references to references.
    1. Perl refcard in PDF, in landscape, 3 columns per page.
    2. perlcheat
    Perl 5.8.8 on Redhat Linux RHEL 5.5.56 (64-bit)
Re: Updating hash using it's reference ...
by perlfan (Parson) on Jul 13, 2014 at 11:14 UTC
    I prefer to use references all the way down, it reads better to me this way.
    my $array_ref = [ { foo=> 1, herp => 'a'}, { bar => 2, derp => 'b'}, { baz => 3, burp => 'c'}, ]; # to iterate foreach my $h (@$array_ref) { foreach $k (keys %$h) { ++$h->{$k}; # note ++ "increments" strings as well } }
    Or in a more direct way.
    my $array_ref = [ { foo=> 1, herp => 'a'}, { bar => 2, derp => 'b'}, { baz => 3, burp => 'c'}, ]; # unrolled iteration loop ++$array_ref->[0]->{foo}; # now 2 ++$array_ref->[0]->{herp}; # now b ++$array_ref->[1]->{bar}; # now 3 ++$array_ref->[1]->{derp}; # now c ++$array_ref->[2]->{baz}; # now 4 ++$array_ref->[2]->{burp}; # now d
Re: Updating hash using it's reference
by hippo (Archbishop) on Jul 12, 2014 at 08:44 UTC
    #!/usr/bin/perl -w use strict; use warnings; use Data::Dumper; my @foo = ( { 'name' => 'rover' }, { 'name' => 'felix' } ); # Modify the first hashref in @foo print Dumper (\@foo); $foo[0]->{'food'} = 'Scooby Snacks'; print Dumper (\@foo);

    And so you have added a key to one of the hash references in your array.