Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: How can I populate attributes of a hash into another ?

by 2teez (Priest)
on Jul 19, 2013 at 07:40 UTC ( #1045285=note: print w/ replies, xml ) Need Help??


in reply to How can I populate attributes of a hash into another ?

ravi06,
I thought, a chapter should be a subset of a book and not the other way round? However, if you are sure of what you wanted, knowing that the key of hash MUST be unique, then you can do like so:

use warnings; use strict; use Data::Dumper; my %book = ( 'name' => 'abc', 'author' => 'monk', 'isbn' => '123-890', 'issn' => '@issn', ); my %chapter = ( 'title' => 'xyz', 'page' => '90', ); ## VOIR HERE my @temp_arr = keys %book; @chapter{@temp_arr} = @book{@temp_arr}; print Dumper \%chapter; print $/, $chapter{name},$/; #prints abc
NOTE: And what about '@issn' in your script? Like you have it, if it is an array variable, it won't interpolate!! Just saying...

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me


Comment on Re: How can I populate attributes of a hash into another ?
Download Code
Re^2: How can I populate attributes of a hash into another ?
by hdb (Prior) on Jul 19, 2013 at 08:40 UTC

    Calls to keys and values are guaranteed to return data in a consistent way, so you could simply write:

    @chapter{ keys %book } = values %book;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2014-10-22 02:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (112 votes), past polls