Can Perl detect arrays?
Posted
by
Sandra Schlichting
on Stack Overflow
See other posts from Stack Overflow
or by Sandra Schlichting
Published on 2011-03-07T23:53:28Z
Indexed on
2011/03/08
0:10 UTC
Read the original article
Hit count: 141
I have this script
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
my %x1 = ();
$x1{"a"} = "e";
my %x2 = ();
$x2{"a"} = ["b","c"];
p(\%x1);
p(\%x2);
sub p {
my $x = shift @_;
print $x->{a};
print "\n";
}
which outputs
e
ARRAY(0x2603fa0)
The problem is I don't know when the input is an array or a scalar, and when it is an array I would like to print those values as well.
Can p
be modified to do this?
© Stack Overflow or respective owner