Forum Moderators: coopster & phranque

Message Too Old, No Replies

how to deference the following hash of hash of arrays

         

srinshcl

3:06 pm on Nov 13, 2005 (gmt 0)

10+ Year Member



Hi need to deference the following hash of hash arrays to retrive all key value pairs,how to deference the following hash of hash arrays.

%component_tree =
( component1 =>
{
(name => "command1", id=> "001"),
(name => "command2", id=> "002"),

(name => "command3", id=> "003"),

(name => "command4", id=> "004")
},

component2 => {
(name => "command5", id=> "011"),
(name => "command6", id=> "012"),
(name => "command7", id=> "013"),vi hash
(name => "command8", id=> "014")
},
);

SeanW

1:46 pm on Nov 14, 2005 (gmt 0)

10+ Year Member



I don't think this structure is a good one, you're building hashes where I think you want to use arrays.

Try a "use Data::Dumper" at the beginning, then

print Dumper \%component_tree;

after the declaration:

[perl]
$VAR1 = {
'component1' => {
'name' => 'command4',
'id' => '004'
},
'component2' => {
'name' => 'command8',
'id' => '014'
}
};

[/perl]

You're mixing up hashes, arrays, and arrayrefs and hence losing data. How about:

[perl]
#!/usr/bin/perl

use Data::Dumper;

%component_tree = (
component1 => [
{name => "command1", id=> "001"},
{name => "command2", id=> "002"},
{name => "command3", id=> "003"},
{name => "command4", id=> "004"}
],

component2 => [
{name => "command5", id=> "011"},
{name => "command6", id=> "012"},
{name => "command7", id=> "013"},
{name => "command8", id=> "014"}
],
);

print Dumper \%component_tree;
[/perl]

Which gives:

[perl]
$VAR1 = {
'component1' => [
{
'name' => 'command1',
'id' => '001'
},
{
'name' => 'command2',
'id' => '002'
},
{
'name' => 'command3',
'id' => '003'
},
{
'name' => 'command4',
'id' => '004'
}
],
'component2' => [
{
'name' => 'command5',
'id' => '011'
},
{
'name' => 'command6',
'id' => '012'
},
{
'name' => 'command7',
'id' => '013'
},
{
'name' => 'command8',
'id' => '014'
}
]
};
[/perl]