use of undef

Erik Hollensbe belman at
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>
To: <pdx-pm-list at>
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
> 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


More information about the Pdx-pm-list mailing list