Hi again,

This:

%{%$excel_hash{$keys1} }
is redundant.

Work from the inside.

Let's start again. Say you have a hash reference  $excel_href. (Note you should name it with 'href' since it is not a hash, but a scalar that holds a reference to the hash.) Now you want to loop through the keys, so make it easy on yourself and deference the hashref at the start.

my %excel_hash = %{ $excel_href };
Loop through the keys, using for, which is the same as for each:
for my $key ( keys %excel_hash ) { ... }
So now you have the key, and you need the value, and we know that the value is going to be a "sub-hash", ie a scalar holding a reference to another hash. Since we're lazy and we don;t like typing arrows, and for consistency, we can dereference this hashref, too, and we'll do it right after we fetch the value:
for my $key ( keys %excel_hash ) { my $inner_href = $excel_hash{ $key }; my %inner_hash = %{ $inner_href }; }
Finally, you want to get the value of the key named 'Name' in the inner hash.
for my $key ( keys %excel_hash ) { my $inner_href = $excel_hash{ $key }; my %inner_hash = %{ $inner_href }; my $name = $inner_hash{'Name'}; }
Or, for a version more similar to yours:
for my $key1 ( sort keys %{ $excel_href } ) { while ( my ( $key2, $val2 ) = each %{ $excel_href->{ $key1 } } ) { print "$key2 = $value \n"; } }
Or, for a version sticking to simple for loops (each should be handled with care):
for my $outer_key ( keys %{ $excel_href } ) { print "$outer_key:"; for my $inner_key ( keys %{ $excel_href->{ $outer_key } } ) { print "\t$inner_key = $excel_href->{ $outer_key }->{ $inner_ke +y }\n"; } }
Or, for the convenient way using map (assuming you know the name of the inner key you are trying to read):
print "$_\n" for map {"$_: Name = $excel_href->{ $_ }->{'Name'}"} keys + %{ $excel_href };

I expect all this is clear as mud at first reading, but have a play around and it should start making sense. Post back here with any questions.


The way forward always starts with a minimal test.

In reply to Re^6: excel to hash by 1nickt
in thread excel to hash by colox

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.