Consider:
use strict; use warnings; use Tk; my $mw = MainWindow->new (-title => 'Hello World'); my @arr=('first', 'second', 'third'); my %hash = (A => 100, B => 200, C => [@arr], D => \&hi, E => $mw); $hash{'A'}=100; $hash{'B'}=200; $hash{'C'}=[@arr]; for (sort keys %hash) { print "$_ => "; if (ref($hash{$_}) eq 'ARRAY') { print "Array: ", join(', ', @{ $hash{$_} }); } elsif (ref($hash{$_}) eq 'HASH') { # do something else print "Hash: ..."; } else { print "Scalar: $hash{$_}"; } print "\n"; } sub hi { print "hello World"; }
Prints:
A => Scalar: 100 B => Scalar: 200 C => Array: first, second, third D => Scalar: CODE(0x21bdf50) E => Scalar: MainWindow=HASH(0x21d97ac)
Adding:
elsif (ref($hash{$_})) { print "Unknown ref: $hash{$_}";
alters the output to:
A => Scalar: 100 B => Scalar: 200 C => Array: first, second, third D => Unknown ref: CODE(0x21be538) E => Unknown ref: MainWindow=HASH(0x21d9e38)
References come in many flavours. There is probably no good general answer to OP's question for "display" output (as opposed to debugging output), but at least unhandled cases can be caught by noting that ref returns undef for non-reference values.
In reply to Re^2: Complex hash?
by GrandFather
in thread Complex hash?
by tamaguchi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |