home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!panther!mothost!merlin.dev.cdx.mot.com!fendahl.dev.cdx.mot.com!mcook
- From: mcook@fendahl.dev.cdx.mot.com (Michael Cook)
- Subject: Re: Perl range operator
- Message-ID: <mcook.725814444@fendahl.dev.cdx.mot.com>
- Keywords: range
- Sender: news@merlin.dev.cdx.mot.com (USENET News System)
- Nntp-Posting-Host: fendahl.dev.cdx.mot.com
- Organization: Motorola Codex, Canton, Massachusetts
- References: <1992Dec30.131700@se28.wg2.waii.com>
- Date: Thu, 31 Dec 1992 15:07:24 GMT
- Lines: 40
-
- rfs@se28.wg2.waii.com (Robert Starr) writes:
-
- >In some recent work I was doing, I noticed the following:
-
- > @x = (10..1);
-
- >doesn't generate the range 10 -> 1, as one might expect. Is there a
- >reason it doesn't work this way?
-
- 10..1 is a null range. Having it work that way simplifies the mechanism and
- removes much of the need for special-case handling. Consider the case when
- the range ends are not static:
-
- for ($x..$y)
-
- you don't have to first test to make sure $x <= $y. If you really meant to
- have a reverse range, you can do this:
-
- @x = reverse(1..10);
- or
- for (reverse $x..$y)
-
- BTW, that's the way it works in Ada, too:
-
- for i := reverse 1..10 loop
-
- For code such as this:
-
- for i := x..y loop
-
- the compiler would otherwise have to generate code like this:
-
- if (x <= y)
- for (i = x; i <= y; i++)
- ...
- else
- for (i = x; i >= y; i--)
- ...
-
- Michael.
-