Hello Monks,

Although that this seems with a very common question I can not find solution to my problem. I have a hash that contains multiple levels and I am trying to extract the data of the final level.

I want to use for loops to reach the final level and then store the data to one level hash.

I have been reading online the perldoc Access and Printing of a HASH OF HASHES. Even I found a similar question Traverse an unknown multi-dimensional hash but it is not exactly what I want.

On the sample of code that I have created I can see the path of the hash clearly by using Data::Dumper but I want to use foreach loops so I can reach the final stage of my complex hash and extract the data to a simple single level hash.

Update: The %singleLevelHash = (); that I have defined I wan to contain the last level of data.

Update 2: I tried to use everybodies solution to get the desired output, but still I can not figure it how to do it. I will update also the title of my question.

I am trying to extract the final part of hash as given in the sample code under.

Sample of desired output:

%singleLevelHash = ( 'thirdLevelSampleKeyOne' => 'thirdLevelSampleValue', 'thirdLevelSampleKeyTwo' => 'thirdLevelSampleValue', 'thirdLevelSampleKeyThree' => 'thirdLevelSampleValue', );

This is the reason that I wanted to use the foreach loop. Sample of my code provided under.

Sample of code:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %singleLevelHash = (); my %multiLevelHash = ( 'firstSampleKey' => 'SampleValue', 'secondSampleKey' => { 'secondLevelSampleKey' => { 'thirdLevelSampleKeyOne' => 'thirdLevelSampleValue', 'thirdLevelSampleKeyTwo' => 'thirdLevelSampleValue', 'thirdLevelSampleKeyThree' => 'thirdLevelSampleValue', } } ); print Dumper \%multiLevelHash; foreach my $firstSampleKey ( sort keys %multiLevelHash ) { print $firstSampleKey . "\n"; foreach my $secondLevelSampleKey ( sort keys $multiLevelHash{$firs +tSampleKey} ) { print $secondLevelSampleKey . "\n"; } } __DATA__ $VAR1 = { 'firstSampleKey' => 'SampleValue', 'secondSampleKey' => { 'secondLevelSampleKey' => { 'thirdLev +elSampleKeyOne' => 'thirdLevelSampleValue', 'thirdLev +elSampleKeyThree' => 'thirdLevelSampleValue', 'thirdLev +elSampleKeyTwo' => 'thirdLevelSampleValue' } } }; firstSampleKey Type of argument to keys on reference must be unblessed hashref or arr +ayref at hash.pl line 21.

Any suggestions are much appreciated.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to How to print a multi-level Hashes of Hashes and extract parts of it by thanos1983

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.