Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Add an arrayref to a hashref

by Anonymous Monk
on Aug 12, 2019 at 19:16 UTC ( #11104346=perlquestion: print w/replies, xml ) Need Help??

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

Hi there Monks!

Is it possible to add an arrayref:
my $arrayref = [ 'Country', 'location', 'language' ];

To a hashref:
my $hashref = { 'name' => "Harsha", 'designation' => "Manager" }

push %$hashref{personal},$arrayref;

Thanks for looking!

Replies are listed 'Best First'.
Re: Add an arrayref to a hashref
by haukex (Bishop) on Aug 12, 2019 at 19:23 UTC

    Yes, it's possible. But what do you want the final data structure to look like?

    $hashref->{personal} = $arrayref; # Results in: { name => "Harsha", designation => "Manager", personal => ["Country", "location", "language"], }

    But based on the values in the array, they sound more like keys...

    @{$hashref}{@$arrayref} = (); # Results in: { name => "Harsha", designation => "Manager", Country => undef, location => undef, language => undef, }

    Plus a few more possibilities depending on your expected output, so please show that.

      I was looking for this:
      { name => "Harsha", designation => "Manager", personal => ["Country", "location", "language"], }

      I will have to convert into JSON after that, it should work. Thanks!

        To add to ++haukex's answer, if you want to add an item, dereference the array and push to it :

        push(@{ $href->{personal} }, 'birthday' );
        If you don't know the items before you build the aref and store in the href:
        push(@{ $href->{personal} }, $_) for @some_list;

        Hope this helps!


        The way forward always starts with a minimal test.
Re: Add an arrayref to a hashref
by BillKSmith (Prior) on Aug 12, 2019 at 20:40 UTC
    I assume that you want to add the key "personal" to the hash and use the arrayref as the associated value. Refer to section "Using References' of perlref. The paragraph which begins "Anywhere you'd put an identifier..." always works.
    use strict; use warnings; use Data::Dumper; my $arrayref = [ 'Country', 'location', 'language' ]; my $hashref = { 'name' => "Harsha", 'designation' => "Manager" }; ${$hashref}{personal} = $arrayref; print Dumper($hashref);

    OUTPUT

    $VAR1 = { 'name' => 'Harsha', 'designation' => 'Manager', 'personel' => [ 'Country', 'location', 'language' ] };
    Bill
Re: Add an arrayref to a hashref
by jcb (Vicar) on Aug 13, 2019 at 05:41 UTC
    Other monks have also given good replies, but I want to mention the perldsc documentation page which has a section on "hashes of arrays" — almost exactly what you seem to be describing.
Re: Add an arrayref to a hashref (diverehohy)
by Anonymous Monk on Aug 13, 2019 at 04:27 UTC

    rehohy

    my $Record = {}; $Record->{designation} = "Manager"; #d0 $Record->{name} = "Harsha"; #d0 $Record->{personal}[0] = "Country"; #d1 $Record->{personal}[1] = "location"; #d1 $Record->{personal}[2] = "language"; #d1

    diverehohy

    use Data::Diver qw/ DiveVal /; my $Record = {}; DiveVal( $Record, qw/ designation /) = "Manager"; #d0 DiveVal( $Record, qw/ name /) = "Harsha"; #d0 DiveVal( $Record, qw/ personal 0 /) = "Country"; #d1 DiveVal( $Record, qw/ personal 1 /) = "location"; #d1 DiveVal( $Record, qw/ personal 2 /) = "language"; #d1 #use Data::Dump qw/ dd /; #dd( $Record ); use JSON; print JSON->new->ascii(1)->pretty(1)->encode($Record); __END__ { "designation" : "Manager", "personal" : [ "Country", "location", "language" ], "name" : "Harsha" }

    dd

    my $Record = { designation => "Manager", name => "Harsha", personal => ["Country", "location", "language"], };

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2020-10-24 11:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (243 votes). Check out past polls.

    Notices?