use of undef

Erik Hollensbe belman at
Thu Jun 14 03:13:14 CDT 2001

heh, it was late. this is what I deserve I guess for not checking my code before I post it to the list :)

On Wed, Jun 13, 2001 at 08:27:13AM -0700, Tom Phoenix wrote:
> On Wed, 13 Jun 2001, Erik Hollensbe wrote:
> > austin already replied about defined(), but you may find that something
> > along these lines would solve your problem much cleaner than a GOTO:
> GOTO? Who is using GOTO?

the label was superfluous - why not just include the comparison in the loop instead? a traditional for would have been another way to accomplish such a feat.

for($count = 0; defined $array[$count]; $count++) { undef; };

unfortunately, perl doesn't allow loops without blocks.

> > @array(0..2, ,undef);
> I think you omitted an equals sign here.
> > $count = 0; # not needed unless use strict
> No, if you have 'use strict' enabled, you would need to declare these
> variables; that line wouldn't accomplish that. If you have warnings
> enabled, you would need to initialize $count before using it as an array
> index, though; that's probably what you were thinking of.

see the first line I wrote :)

> > while(1) { defined($array[$count]) ? $count++ : last };
> Real Perl programmers don't use subscripts. :-) It's almost certainly more
> efficient to use a foreach loop on the array. Also, I prefer not to use
> the ?: operator as a control structure, since it is harder to understand
> (and potentially less efficient) than a simple if/else would be.

i frankly cannot see how a foreach() could be any more efficient than a while(1) and a ?:, as there is no list to parse and/or watch for modifications. ?: is no different than an if() { } else { } comparison anyways. The Deparse pragma would show that your foreach() is being turned into a while() anyways. (with subscripts, no less)

Erik Hollensbe
belman at


More information about the Pdx-pm-list mailing list