Arrays Of Arrays: Is it an Array or Scalar?

H

Hal Vaughan

I have a function that I'm using to perform operations on strings in an
array. There are times where I'd like to have this function work on arrays
of arrays. Is there a simple way to tell if the value of an element in an
array is a scalar, or is, instead, a reference to another array?

I know I can use a regex to see if the string matches the pattern for an
array reference, but is there a more "elegant" and easier way to do it?

(And, while I'm at it, is there a way to check for a hash as well? I'd
think they'd both be done the same way.)

Thanks!

Hal
 
J

J Krugman

In said:
I have a function that I'm using to perform operations on strings in an
array. There are times where I'd like to have this function work on arrays
of arrays. Is there a simple way to tell if the value of an element in an
array is a scalar, or is, instead, a reference to another array?
I know I can use a regex to see if the string matches the pattern for an
array reference, but is there a more "elegant" and easier way to do it?
(And, while I'm at it, is there a way to check for a hash as well? I'd
think they'd both be done the same way.)



perldoc -f ref

for my $elem (@array) {
if (my $r = ref $elem) {
if ($r eq 'ARRAY') {
# process arrayref
}
elsif ($r eq 'HASH') {
# process hashref
}
else {
# deal with non-array, non-hash ref
}
}
}
 
G

Graham Wood

Hal said:
I have a function that I'm using to perform operations on strings in an
array. There are times where I'd like to have this function work on arrays
of arrays. Is there a simple way to tell if the value of an element in an
array is a scalar, or is, instead, a reference to another array?

I know I can use a regex to see if the string matches the pattern for an
array reference, but is there a more "elegant" and easier way to do it?

(And, while I'm at it, is there a way to check for a hash as well? I'd
think they'd both be done the same way.)

Thanks!

Hal

You could use the ref function to find out what is in your array
elements. If it isn't a reference to a scalar, an array, a hash, code,
another reference or a glob or lvalue, then you can safely say it's a
scalar. I think. I'm sure someone will correct me if I lie.

perldoc -f ref
ref Returns a true value if EXPR is a reference, false otherwise. If
EXPR is not specified, "$_" will be used. The value returned
depends on the type of thing the reference is a reference to.
Builtin types include:

SCALAR
ARRAY
HASH
CODE
REF
GLOB
LVALUE

Graham
 
H

Hal Vaughan

Hal said:
I have a function that I'm using to perform operations on strings in an
array. There are times where I'd like to have this function work on
arrays
of arrays. Is there a simple way to tell if the value of an element in an
array is a scalar, or is, instead, a reference to another array?

I know I can use a regex to see if the string matches the pattern for an
array reference, but is there a more "elegant" and easier way to do it?

(And, while I'm at it, is there a way to check for a hash as well? I'd
think they'd both be done the same way.)

Thanks!

Hal

Thanks to those who responded and said to use the ref() function. I was
thinking more along the lines of type() or typeof(), and couldn't find
anything like that. Once someone pointed me in the right direction (with
ref() ), it was easy to find more info.

The problem with being self-taught is that there are so many holes like this
where you know there should be something, but you have no idea what it's
called, so it can be hard to find. Once you get a name or term to use,
it's easy.

Thanks.

Hal
 
A

Aaron Sherman

Hal Vaughan said:
Is there a simple way to tell if the value of an element in an
array is a scalar, or is, instead, a reference to another array?

You're confusing your terms a bit. Let's start from the basics:

Arrays are collections of scalars (not anything else... ever).

Scalars can be simple values, references, or other (less common)
things I won't go into here.

So, to re-phase what I think you meant:

"Is there a simple way to tell if an element in an array is a
reference?"

And the answer there would be "ref". You can use "perldoc -f ref" or
"perldoc perlrref" for far, far more information on the topic, but a
reference to an array would return "ARRAY" when you use ref on it.
Thus, you might say:

if (ref($myarray[$i])) {
if (ref($myarray[$i]) eq 'ARRAY') {
# It's a reference to a sub-array, do the right thing....
} else {
warn "I don't know how to cope with refs to
".ref($myarray[$i])." in my array":
}
} else {
# It's a simple scalar value, do the right thing.....
}

Good luck!
 
T

Tore Aursand

I have a function that I'm using to perform operations on strings in an
array. There are times where I'd like to have this function work on arrays
of arrays. Is there a simple way to tell if the value of an element in an
array is a scalar, or is, instead, a reference to another array?

Yes, by using the ref() function;

perldoc -f ref

I feel that I personally tend to write functions/methods that no matter
what turns the argument data into an array reference (if that's applicable);

sub foo {
my $input = shift;
$input = ( ref($input) eq 'ARRAY' ) ? $input : [ $input ];

foreach ( @$input ) {
# ...
}
}

That way I never (...) need to know what the function/method wants.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top