in reply to How Do I Populate a Hash of Hashes?

srins firstly, I think there's far too much information in your post. ++ for trying to give a working example, but it would have been better if you'd cut this down to what you've got problems with, which is building the hash. (The fact that this comes from a spreadsheet is neither here nor there.)

secondly, I'd be inclined to re-jig your hash format a bit. Your example shows a hash of a hash, containing values which are array references with hashes in them?. Seems a bit overboard, you could get away with just a hash of a hashes containing array refences, but I'd put the array reference in its own named key, just for flexibilty. for example

you show $hash{'mode'}{'component'} = [{'command' => 'cmd1'}, {'command' => 'cm +d2'}]; simplist would be $hash{'mode'}{'component'} = ['cmd1', 'cmd2']; but I'd go for $hash{'mode'}{'component'}{'commands'} = ['cmd1', 'cmd2'];
ignoring your code and just taking your data, I'd attack this by processing the rows in order, keeping track of the current mode and component, and, when I found a command, sticking it straight into the hash. something like this...
#!/perl -w use Data::Dumper; use strict; my %hash; my $current_comp = ""; my $current_mode = ""; my %the_hash; while (<DATA>) { chomp; my ($coord,$data) = split /=>/; my ($row,$col) = split /,/, $coord; if ($col == 0) { $current_comp = $data; next; } if ($col == 2) { $current_mode = $data; next; } if ($col == 4) { push @{$the_hash{$current_mode}{$current_comp}{'commands'}}, $ +data; next; } } print Dumper(\%the_hash);
the tricksy part of that is the line...
push @{$the_hash{$current_mode}{$current_comp}{'commands'}}, $data;
.. which makes perl do all sorts of things for us. Just the fact that we're referencing the hash key "$the_hash{$current_mode}{$current_comp}{'commands'}" makes perl auto-vivify it, then the whole thing is wrapped up as an array refence, Perl again is very obliging, assumes we know what were doing and creates us a hash reference for us which we can then push to.

The output should end up looking something like this

$VAR1 = { 'mode1' => { 'comp3' => { 'commands' => [ 'command1', 'command2' ] }, 'comp1' => { 'commands' => [ 'command1' ] } }, 'mode2' => { 'comp2' => { 'commands' => [ 'command1', 'command2', '' ] } } };
HTH, Rob
---
my name's not Keith, and I'm not reasonable.