Assigning a hash of array to another hash

M

mohamad.ridha

Hi all,

I am trying to write a simple script but stuck with one problem on how
to assign a hash of array to another hash.

I have this hash named "record" that consists of two keys "recinfo" and
"recbody" and each key is assigned to hold an array of strings that
have been filled with values. The following code seems to work:

$record {"info"} = [ @recinfo ];
$record {"body"} = [ @recbody ];

But then I need to implement another hash named "recordlist" that has
variable "$recid" as keys, and each key will hold a "record" hash
above. I tried to use the following statement to assign the "record"
that has already filled with values to "recordlist" but to no avail.

$recordlist {"$recid"} = { %record };

Does anyone how to solve this problem?

Thanks in advance!

--Ridha
 
B

Bob Walton

(e-mail address removed) wrote:

....
I have this hash named "record" that consists of two keys "recinfo" and
"recbody" and each key is assigned to hold an array of strings that
have been filled with values. The following code seems to work:

$record {"info"} = [ @recinfo ];
$record {"body"} = [ @recbody ];

But then I need to implement another hash named "recordlist" that has
variable "$recid" as keys, and each key will hold a "record" hash
above. I tried to use the following statement to assign the "record"
that has already filled with values to "recordlist" but to no avail.

$recordlist {"$recid"} = { %record };

Does anyone how to solve this problem?

When I try it, it seems to work fine:

use strict;
use warnings;
my @recinfo=(1,2,3,4,5);
my @recbody=(2,3,4,5,6,7);
my %record;
$record{info}=[@recinfo];
$record{body}=[@recbody];
my %recordlist;
$recordlist{recid}={%record};
use Data::Dumper;
print Dumper \%recordlist;

What is it about the output of the above that isn't what you expect?

Also, check out:

perldoc -q quoting

....
 
T

Tad McClellan

$record {"info"} = [ @recinfo ];


There is not much need for an anonymous array when you already
have a (properly scoped, I assume) named array:

$record{info} = \@recinfo;

$recordlist {"$recid"} = { %record };


perldoc -q vars

What's wrong with always quoting "$vars"?


No need for anon hash when you already have a named hash:

$recordlist{$recid} = \%record;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,001
Messages
2,570,254
Members
46,850
Latest member
VMRKlaus8

Latest Threads

Top