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.

In reply to Re: how to build the hash for below code by reasonablekeith
in thread How Do I Populate a Hash of Hashes? by srins

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.