in reply to Searching and printing complex data structures
Data::Dumper is a good example how to print a data structure. It is also not difficult to write a recursive sub that does a similar thing. See below. You could also add a callback function that searches for whatever. I made some assumptions about the data structure you are looking at but I think my example is relatively general.
use strict; use Data::Dumper; my %hash; $hash{Company_A}{ Identifier_1 } = ["a", "b"]; $hash{Company_A}{ Identifier_2 } = ["b", "g"]; + $hash{Company_A}{ Identifier_3 } = "c"; $hash{Company_B}{ Identifier_1 } = "a"; $hash{Company_B}{ Identifier_2 } = "g"; $hash{Company_B}{ Identifier_3 } = "x"; print Dumper(%hash); traverse( 0, \%hash ); sub traverse { my $level = shift; my $ref = shift; if( ref($ref) =~ /HASH/ ) { print "\n"; for my $key (keys %$ref) { print "\t" x $level, $key; traverse( $level+1, $$ref{$key} ); } } elsif( ref($ref) =~ /ARRAY/ ) { print "\n"; for (@$ref) { print "\t" x $level; traverse( $level+1, $_ ); } } else { print "\t" x $level, $ref, "\n"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Searching and printing complex data structures
by Anonymous Monk on Apr 10, 2013 at 19:26 UTC |