in reply to How to distinguish an array data type from a scalar data type in a complex hash.

#!/usr/bin/perl -l $dialog->{attributes}{control1} = "abc"; $dialog->{attributes}{control2}[0] = "cde"; $dialog->{attributes}{control2}[1] = "fgh"; for my $control (keys %{ $dialog->{attributes} }) { if (ref $dialog->{attributes}{$control} eq 'ARRAY') { print "@{ $dialog->{attributes}{$control} }"; } else { print $dialog->{attributes}{$control}; } } __END__ abc cde fgh

Or, with an intermediate variable (better readable IMHO):

for my $control (keys %{ $dialog->{attributes} }) { my $val = $dialog->{attributes}{$control}; if (ref $val eq 'ARRAY') { print "@$val"; } else { print $val; } }

And in case there could also be other types (such as hashrefs), you should either test for them, too, or at least write

... } elsif (!ref $val) { print $val; }

for the simple scalar values.

Replies are listed 'Best First'.
Re^2: How to distinguish an array data type from a scalar data type in a complex hash.
by Steve_BZ (Chaplain) on Mar 13, 2010 at 16:18 UTC

    Thank you Almut, just what I needed.

    Regards

    Steve