danmcb has asked for the wisdom of the Perl Monks concerning the following question:

Why can I do :

my @l = (1 .. 10);

and get the expected result, but not

my @l = (10 .. 1);

(not that it's really a problem as we have reverse(), but I just wondered.

Replies are listed 'Best First'.
Re: literal lists
by svenXY (Deacon) on Sep 06, 2005 at 09:30 UTC
    Well, the Camel Book justs says:
    "If the left value is greater than the right value, a null list is returned. (To produce a list in reverse order, see the reverse operator.)"
    No more explanation...
    Sorry, Sven

      hah ... the answer is "cos they made it that way ..."

      fair enough.

Re: literal lists
by phaylon (Curate) on Sep 06, 2005 at 11:30 UTC
    For instance:
    my ( @a, @b ); @a = ( 1, 2, 3 ); foreach ( 0 .. $#a ) { }
    Walks the values 0, 1 and 2.
    foreach ( 0 .. $#b ) { }
    does nothing. But $#b returns -1 because there are no values in it. If it would count down, It'd walk the values 0 and -1.

    Ordinary morality is for ordinary people. -- Aleister Crowley
      true. In that case I wonder why $#b evaluates to -1, rather than undef?

        I guess because undef in an integer context will be taken as '0' (albeit with a warning if you have asked for warnings), whereas -1 is not a valid array index:

        @b = (a .. z); + $#b = undef; + print "@b"; + $#b = -1; + print "@b";

        /J\