use of undef

Tom Phoenix rb-pdx-pm at redcat.com
Thu Jun 14 13:19:08 CDT 2001


On Thu, 14 Jun 2001, Erik Hollensbe wrote:

> a traditional for would have been another way to
> accomplish such a feat.
> 
> for($count = 0; defined $array[$count]; $count++) { undef; };

Yes, but you're using indexing, which is not where Perl's strength lies. A
foreach will generally be faster.

> unfortunately, perl doesn't allow loops without blocks.

I think you're talking about how you put undef inside the curly braces of
that for loop. But you could have omitted that. Of course, you can have
loops without blocks.

> > > 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.

What list, what parsing, and what modifications are you talking about? It
sounds as if you think perl is doing something in a foreach loop that
would make that _very_ inefficient!

> The Deparse pragma would show that your foreach() is being turned into
> a while() anyways. (with subscripts, no less)

I am unable to replicate this. Could you show me what you mean?

-- 
Tom Phoenix       Perl Training and Hacking       Esperanto
Randal Schwartz Case:     http://www.rahul.net/jeffrey/ovs/

TIMTOWTDI



More information about the Pdx-pm-list mailing list