testing for array or scalar in a hash.

F

Fred S

Hi,
I have a hash containing, data, everything is generated on the fly so
my hash, has some scalars, and some arrays, in it.
How can I test wether a value of the hash is an array and then loop
through it ?
i did the following loop:
it will print the arrays but will skip the scalars,

foreach (keys %Options){
print $_." => \n";
foreach my $val (@{$Options{$_}}){
print "\t".$val;
print "\n";
}
}

Thanx for the help.


Fred
 
G

Gunnar Hjalmarsson

Fred said:
I have a hash containing, data, everything is generated on the fly
so my hash, has some scalars, and some arrays, in it.
How can I test wether a value of the hash is an array and then loop
through it ?

How about:

foreach (keys %Options){
if (ref $Options{$_} eq 'ARRAY') {
print ...
 
G

Greg Bacon

: I have a hash containing, data, everything is generated on the fly so
: my hash, has some scalars, and some arrays, in it.
: How can I test wether a value of the hash is an array and then loop
: through it ?
: [snip code]

Use Perl's ref operator to see whether you have an array reference:

#! /usr/local/bin/perl

use warnings;
use strict;

my %Options = (
scalar => 42,
arrayref => [ qw/ apples oranges bananas / ],
);

for (keys %Options) {
print $_, " => \n",
map "\t$_\n",
ref $Options{$_} ? @{ $Options{$_} } : $Options{$_};
}

This generates the following output:

scalar =>
42
arrayref =>
apples
oranges
bananas

You could pare the line lengths by iterating with the each operator.

Hope this helps,
Greg
 
J

John W. Krahn

Fred said:
I have a hash containing, data, everything is generated on the fly so
my hash, has some scalars, and some arrays, in it.
How can I test wether a value of the hash is an array and then loop
through it ?
i did the following loop:
it will print the arrays but will skip the scalars,

foreach (keys %Options){
print $_." => \n";
foreach my $val (@{$Options{$_}}){
print "\t".$val;
print "\n";
}
}

Use the ref operator.

for ( keys %Options ) {
print $_." => \n";
if ( ref eq 'ARRAY' ) {
print "\t", join( "\t", @{$Options{$_}} ), "\n";
}
}


perldoc -f ref


John
 
D

David K. Wall

Fred S said:
I have a hash containing, data, everything is generated on the fly
so my hash, has some scalars, and some arrays, in it.
How can I test wether a value of the hash is an array and then
loop through it ?

perldoc -f ref

Example:

use strict;
use warnings;

my %h = (
a => [1,2,3,4],
h => {a=>1,b=>2,c=>3},
s => 'string'
);

foreach my $k (sort keys %h) {
print "$k -> ", ref $h{$k}, "\n";
}

Note that the output for $h{s} is blank, because $h{s} is a scalar,
not a reference to a scalar.
 
B

Bart Lateur

Fred said:
I have a hash containing, data, everything is generated on the fly so
my hash, has some scalars, and some arrays, in it.
How can I test wether a value of the hash is an array and then loop
through it ?

If *all* you have in there are array (refs) and scalars, then you can
use ref() as a boolean test. For scalars, it will return the empty
string (false), for array refs, "ARRAY" (true).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,085
Latest member
cryptooseoagencies

Latest Threads

Top