Strange 'for' behaviour?

A

Amer Neely

Read the FAQ plus numerous books I have on hand about a 'for' loop and
the '..' range operator. Every example I've seen assumes the list is in
ascending order.

I'm stumped as to the behaviour of a for loop I'm trying to get working.

for $year (1999 .. 2005)
{
print "$year,";
}

does what you would expect .. prints '1999, 2000, 2001, 2002, 2003,
2004, 2005'.

But thie:

for $year (2005 .. 1999)
{
print "$year,";
}

prints nothing. Pourqoui? What is going on that I don't see?

--
Amer Neely, Softouch Information Services
Home of Spam Catcher
W: www.softouch.on.ca
E: (e-mail address removed)
Perl | MySQL | CGI programming for all data entry forms.
"We make web sites work!"
 
A

A. Sinan Unur

Read the FAQ plus numerous books I have on hand about a 'for' loop and
the '..' range operator. Every example I've seen assumes the list is
in ascending order.

It seems like you did not look at the documentation that explains how
the range operator works:

perldoc perlop
for $year (2005 .. 1999)
{
print "$year,";
}

prints nothing. Pourqoui? What is going on that I don't see?

From the documentation mentioned above:

Range Operators
... In list context, it returns a list of values counting (up by
ones) from the left value to the right value. If the left value
is greater than the right value then it returns the empty list.

Besides, what you are doing with the loop is seriously ugly:

#! /usr/bin/perl

use strict;
use warnings;

my $asc = join ',', 1999 .. 2005;
my $dsc = join ',', reverse 1999 .. 2005;

print "$asc\n$dsc\n";

__END__

Sinan
 
T

Trudge

A. Sinan Unur said:
It seems like you did not look at the documentation that explains how
the range operator works:

perldoc perlop


From the documentation mentioned above:

Range Operators
... In list context, it returns a list of values counting (up by
ones) from the left value to the right value. If the left value
is greater than the right value then it returns the empty list.

Besides, what you are doing with the loop is seriously ugly:

#! /usr/bin/perl

use strict;
use warnings;

my $asc = join ',', 1999 .. 2005;
my $dsc = join ',', reverse 1999 .. 2005;

print "$asc\n$dsc\n";

__END__

Sinan

Thank you for the timely and informative response. In my copy of the
Camel Book, it mentions the 'magical autoincrement algorithm' but
doesn't state what you quoted above regarding 'counting (up by
ones)..'.

And thank you for the snippet example.
 
A

A. Sinan Unur

A. Sinan Unur said:
Besides, what you are doing with the loop is seriously ugly: [ snip ]
my $asc = join ',', 1999 .. 2005;
my $dsc = join ',', reverse 1999 .. 2005;

This may even be an occasion to use the old C-style for() loop:

for (my $year = 2005; $year >= 1999; $year++) {...}

While it can be prone to fencepost errors, it's still a valid choice.

ITYM

for (my $year = 2005; $year >= 1999; $year--) {...}

Sinan
 
T

Tad McClellan

Tim Hammerquist said:
Indeed. Thanks for the correction.

/me reverts to "lurk" mode until he can learn to post while not half
asleep.


No, no! Let's simply turn it into a Learning Opportunity and you'll
end up being credited with starting a valuable sub-thread.

Lessee...


Before I even noticed the typo, I was going to suggest

foreach my $year ( reverse 1999 .. 2005 )

because it would be harder to screw that one up.

Then I noticed that you had proven my case!

Thanks man.



Moral (IM(NS)HO, of course):

TMTOWTDI allows you to choose the way that is easiest
to follow and maintain, so choose that one.

Sub-Moral:

Review the Three Rules of Software Development:

Optimize for labor, optimize for labor and optimize for labor.



There. That ought to do it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top