use of undef

Erik Hollensbe belman at subdimension.com
Wed Jun 13 02:29:34 CDT 2001


austin already replied about defined(), but you may find that something
along these lines would solve your problem much cleaner than a GOTO:

@array(0..2, ,undef);
$count = 0; # not needed unless use strict
while(1) { defined($array[$count]) ? $count++ : last };

also note that 'scalar @array' (or just @array in scalar context) will
return the number of elements in an array.

hope it helps,

Erik Hollensbe

----- Original Message -----
From: "Tom Keller" <kellert at ohsu.edu>
To: <pdx-pm-list at pm.org>
Sent: Monday, June 11, 2001 2:31 PM
Subject: use of undef


> Hi all,
> I'm confused about the use of undef.
> I want to count the number of elements in a list until the first undefined
value.
> I tried this:
> @array = (0, 1, 2,  , "next", "group");
> ELEMENTS: foreach $element (@array) {
>       $count++;
>       last ELEMENTS if $element eq undef;
> }
> It gives me the correct count, 3, and leaves the loop correctly, but I get
a warning for each element.
> What am I missing?
>
> Thanks,
> Tom Keller
> TIMTOWTDI
>

TIMTOWTDI



More information about the Pdx-pm-list mailing list