Hi thanos1983,

Do you know about the ref function? You can use it to tell what kind of data is referenced, affecting how you handle it.

Here's a very quick-and-dirty subroutine show_this() that uses ref to recursively inspect the contents of your hash. As such, it's a primitive replacement for Data::Dumper:

#!/usr/bin/perl -w # use strict; use warnings; use Data::Dumper; my %singleLevelHash = (); my %multiLevelHash = ( 'firstSampleKey' => 'SampleValue', 'secondSampleKey' => { 'secondLevelSampleKey' => { 'thirdLevelSampleKeyOne' => 'thirdLevelSampleValue', 'thirdLevelSampleKeyTwo' => 'thirdLevelSampleValue', 'thirdLevelSampleKeyThree' => 'thirdLevelSampleValue', } } ); # print Dumper \%multiLevelHash; show_this(\%multiLevelHash); # foreach my $firstSampleKey ( sort keys %multiLevelHash ) { # print $firstSampleKey . "\n"; # foreach my $secondLevelSampleKey ( sort keys $multiLevelHash{$fi +rstSampleKey} ) { # print $secondLevelSampleKey . "\n"; # } # } sub show_this { my ($x) = @_; if (ref $x eq "") { printf " Value: '%s'\n", $x; } elsif (ref $x eq 'SCALAR') { printf " SCALAR ref: '%s'\n", $$x; } elsif (ref $x eq 'HASH') { print " HASH ref '$x':\n"; foreach my $key (keys %$x) { my $val = $x->{$key}; print " $key: "; show_this($val); } } elsif (ref $x eq 'ARRAY') { print " ARRAY re '$x':\n"; foreach my $val (@$x) { show_this($val); } } }

Hopefully that gives you an idea how to interpret a HASH ref, differently from a scalar value. Hint -- the solution is in the block:

foreach my $key (keys %$x) { ... }
say  substr+lc crypt(qw $i3 SI$),4,5

In reply to Re: How to print a multi-level Hashes of Hashes without the use of a module by golux
in thread 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.