Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Add an arrayref to a hashref

by haukex (Bishop)
on Aug 12, 2019 at 19:23 UTC ( #11104347=note: print w/replies, xml ) Need Help??


in reply to Add an arrayref to a hashref

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.

Replies are listed 'Best First'.
Re^2: Add an arrayref to a hashref
by Anonymous Monk on Aug 12, 2019 at 20:00 UTC
    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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2020-11-26 08:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?