home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / perl / 7640 < prev    next >
Encoding:
Text File  |  1992-12-31  |  1.4 KB  |  54 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!panther!mothost!merlin.dev.cdx.mot.com!fendahl.dev.cdx.mot.com!mcook
  3. From: mcook@fendahl.dev.cdx.mot.com (Michael Cook)
  4. Subject: Re: Perl range operator
  5. Message-ID: <mcook.725814444@fendahl.dev.cdx.mot.com>
  6. Keywords: range
  7. Sender: news@merlin.dev.cdx.mot.com (USENET News System)
  8. Nntp-Posting-Host: fendahl.dev.cdx.mot.com
  9. Organization: Motorola Codex, Canton, Massachusetts
  10. References: <1992Dec30.131700@se28.wg2.waii.com>
  11. Date: Thu, 31 Dec 1992 15:07:24 GMT
  12. Lines: 40
  13.  
  14. rfs@se28.wg2.waii.com (Robert Starr) writes:
  15.  
  16. >In some recent work I was doing, I noticed the following:
  17.  
  18. >    @x = (10..1);
  19.  
  20. >doesn't generate the range 10 -> 1, as one might expect.  Is there a
  21. >reason it doesn't work this way?
  22.  
  23. 10..1 is a null range.  Having it work that way simplifies the mechanism and
  24. removes much of the need for special-case handling.  Consider the case when
  25. the range ends are not static:
  26.  
  27.     for ($x..$y)
  28.  
  29. you don't have to first test to make sure $x <= $y.  If you really meant to
  30. have a reverse range, you can do this:
  31.  
  32.     @x = reverse(1..10);
  33. or
  34.     for (reverse $x..$y)
  35.  
  36. BTW, that's the way it works in Ada, too:
  37.  
  38.    for i := reverse 1..10 loop
  39.  
  40. For code such as this:
  41.  
  42.    for i := x..y loop
  43.  
  44. the compiler would otherwise have to generate code like this:
  45.  
  46.    if (x <= y)
  47.       for (i = x; i <= y; i++)
  48.          ...
  49.    else
  50.       for (i = x; i >= y; i--)
  51.          ...
  52.  
  53. Michael.
  54.