develooper Front page | perl.beginners | Postings from March 2002

Re: Array question...

Thread Previous | Thread Next
From:
Chas Owens
Date:
March 28, 2002 09:22
Subject:
Re: Array question...
Message ID:
1017335920.2090.10.camel@tert.icallinc.com
On Thu, 2002-03-28 at 08:54, Michael D. Risser wrote:
> OK here's the problem:
> 
> I have an array that may or may not have been assigned to, if it has been 
> assigned to I need to do one thing, otherwise I need to do something else.
> 
> I've tried many variations, but I'm having trouble determining if it has been 
> assigned to.
> 
> if(undef @myArray) {
>     # Do thing1
> } elsif(!undef @myArray) {
>     # Do thing2
> }
> 
> and
> 
> if ($myArray[0] ne "") {
>     # thing1
> } else {
>     # thing2
> }
> 
> as well as a few other variations. Printing out the array BEFORE the if block 
> shows nothing in the array, yet it does thing1.
> 
> After trying many different methods, I am totally lost, please help me!
> 
> TIA

Since an array returns the number of elements it contains when 
it is evaluated in scalar context why don't you just say:

#0 is false, non-zero is true so if @myArray has data
#then we do something otherwise we do something else
if (@MyArray) {
	local($") = ','; #make the interpolation of @MyArray use ','s
	print "my \@MyArray = (@MyArray);\n";
} else {
	print "my \@MyArray;\n";
}
	

-- 
Today is Boomtime the 14th day of Discord in the YOLD 3168
You are what you see.

Missile Address: 33:48:3.521N  84:23:34.786W


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About