Strange 'for' behaviour?

Discussion in 'Perl Misc' started by Amer Neely, May 24, 2005.

  1. Amer Neely

    Amer Neely Guest

    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:
    Perl | MySQL | CGI programming for all data entry forms.
    "We make web sites work!"
     
    Amer Neely, May 24, 2005
    #1
    1. Advertising

  2. Amer Neely <> wrote in
    news:U1uke.1$:

    > 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

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, May 24, 2005
    #2
    1. Advertising

  3. Amer Neely

    Trudge Guest

    A. Sinan Unur wrote:
    > Amer Neely <> wrote in
    > news:U1uke.1$:
    >
    > > 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
    >
    > --
    > A. Sinan Unur <>
    > (reverse each component and remove .invalid for email address)
    >
    > comp.lang.perl.misc guidelines on the WWW:
    > http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html


    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.
    ---
    Amer Neely
     
    Trudge, May 24, 2005
    #3
  4. Tim Hammerquist <> wrote in
    news::

    > A. Sinan Unur <> wrote:
    >> 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
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, May 24, 2005
    #4
  5. Tim Hammerquist <> wrote:
    > A. Sinan Unur <> wrote:
    >> Tim Hammerquist <> wrote:
    >> > for (my $year = 2005; $year >= 1999; $year++) {...}

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

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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, May 25, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Antonio

    Strange encoding behaviour

    Antonio, Dec 29, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    428
    Antonio
    Dec 29, 2004
  2. Jan
    Replies:
    2
    Views:
    1,440
    Mike Treseler
    Dec 16, 2004
  3. David Cantin

    Strange behaviour with perl and apache

    David Cantin, Nov 3, 2003, in forum: Perl
    Replies:
    1
    Views:
    456
    Jim Gibson
    Nov 3, 2003
  4. Dennis Johansson
    Replies:
    1
    Views:
    500
    Dennis Johansson
    Aug 21, 2003
  5. Andy Chambers
    Replies:
    1
    Views:
    389
    Daniel Dyer
    May 14, 2007
Loading...

Share This Page