Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Perhaps not the most elegant of ways to handle this (I suspect it can be done better), and may go a little further than you intended (I am not sure this way would necessarily be recommendable), but perhaps it may prove useful. The code can handle a (multilevel structure that consists only of scalars, arrays, and hashes, which it sends to a function for processing (which, in this case, is only display). Hope it helps.

Code:

#!/usr/bin/perl -w use strict; my %hash = ( key1 => { key2 => { key3 => 'asdf', key3b => ';lkj', key3c => { key4 => 'a', key4b => [ 0 .. 3 ], key4c => 'c' } }, key2b => 'wer' } ); my @array = ( 0 .. 5, [ 100 .. 105, [ 1000 .. 1005 ], 107 .. 109 ], 7 .. 9 ); my $scalar = 'blah'; dump_data( 0, '$hash', \%hash ); dump_data( 0, '$array', \@array ); dump_data( 0, '$scalar', \$scalar ); sub dump_data { my ( $level, $base, $data ) = @_; my $nextlevel = $level + 1; if ( ref($data) eq 'ARRAY' ) { foreach my $k ( 0 .. $#{$data} ) { my $baseval = $base . '[' . $k . ']'; dump_it( $nextlevel, $baseval, $data->[$k] ); } } elsif ( ref($data) eq 'HASH' ) { foreach my $k ( sort( keys( %{$data} ) ) ) { my $baseval = $base . '{' . $k . '}'; dump_it( $nextlevel, $baseval, $data->{$k} ); } } elsif ( ref($data) eq 'SCALAR' ) { my $baseval = $base; dump_it( $nextlevel, $baseval, ${$data} ); } } sub dump_it { my ( $nextlevel, $baseval, $datum ) = @_; my $reftype = ref($datum); if ( $reftype eq 'HASH' ) { dump_data( $nextlevel, $baseval, \%{$datum} ); } elsif ( $reftype eq 'ARRAY' ) { dump_data( $nextlevel, $baseval, \@{$datum} ); } else { process_data( $nextlevel, $baseval, $datum ); } } sub process_data { my ( $nextlevel, $baseval, $datum ) = @_; my $indentation = ' ' x $nextlevel; print $indentation, $baseval, ' = ', $datum, "\n"; }

Sample run results:

$ perl test-mlh.pl $hash{key1}{key2}{key3} = asdf $hash{key1}{key2}{key3b} = ;lkj $hash{key1}{key2}{key3c}{key4} = a $hash{key1}{key2}{key3c}{key4b}[0] = 0 $hash{key1}{key2}{key3c}{key4b}[1] = 1 $hash{key1}{key2}{key3c}{key4b}[2] = 2 $hash{key1}{key2}{key3c}{key4b}[3] = 3 $hash{key1}{key2}{key3c}{key4c} = c $hash{key1}{key2b} = wer $array[0] = 0 $array[1] = 1 $array[2] = 2 $array[3] = 3 $array[4] = 4 $array[5] = 5 $array[6][0] = 100 $array[6][1] = 101 $array[6][2] = 102 $array[6][3] = 103 $array[6][4] = 104 $array[6][5] = 105 $array[6][6][0] = 1000 $array[6][6][1] = 1001 $array[6][6][2] = 1002 $array[6][6][3] = 1003 $array[6][6][4] = 1004 $array[6][6][5] = 1005 $array[6][7] = 107 $array[6][8] = 108 $array[6][9] = 109 $array[7] = 7 $array[8] = 8 $array[9] = 9 $scalar = blah

In reply to Answer: I've got a hash of hashes how do i get my values out by QandAEditors

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!
  • 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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (8)
    As of 2015-07-06 05:21 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (70 votes), past polls