Print mixed structure : Hash Output « Hash « Perl





Print mixed structure

   

#!/usr/bin/perl
use warnings;
use strict;

my $mixed = [
    'scalar', ['a', 'B', ['c', 'd'], 'e'],
    {And=>{'A'=>'A'}}, \'a scalar ref'
];

print_structure($mixed);

sub print_structure {
    my ($data) = @_;

    foreach (ref $data) {
        /^$/ and print($data,"\n"), next;
        /^SCALAR/ and print('-> ', $$data, "\n"), next;
        /^HASH/ and do {
            print "\n";
            foreach my $key (keys %{$data}) {
                print "$key => ";
                print_structure ($data->{$key});
            }
            next;
        };
        /^ARRAY/ and do {
            print "\n";
            for my $elc (0..$#{$data}) {
                print "[$elc] : ";
                print_structure ($data->[$elc]);
            }
            next;
        };
        print "?$data?";
    }
}

   
    
    
  










Related examples in the same category

1.Display a hash with print
2.Display the list of values
3.Dump the hash
4.Output nested hash