home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / perl / 5588 < prev    next >
Encoding:
Text File  |  1992-08-29  |  2.1 KB  |  60 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!sun-barr!ames!think.com!paperboy.osf.org!meissner
  3. From: meissner@osf.org (Michael Meissner)
  4. Subject: Re: help - range passed via ARGV
  5. In-Reply-To: smothers@seaview.mcd.intel.com's message of 28 Aug 92 02:23:21 GMT
  6. Message-ID: <MEISSNER.92Aug28150508@tiktok.osf.org>
  7. Sender: news@osf.org (USENET News System)
  8. Organization: Open Software Foundation
  9. References: <SMOTHERS.92Aug27182321@seaview.mcd.intel.com>
  10. Distribution: comp.lang.perl
  11. Date: 28 Aug 92 15:05:08
  12. Lines: 46
  13.  
  14. In article <SMOTHERS.92Aug27182321@seaview.mcd.intel.com>
  15. smothers@seaview.mcd.intel.com (Charles Smothers ~) writes:
  16.  
  17. | I know this was posted about a month ago, could someone mail
  18. | me some of the replys.  Here is the question:
  19. | I need to expand a range that is passed via ARGV.  A simple
  20. | program that will do the following will give me the help
  21. | I need:
  22. | % myprog 1..3 7 10..12     <- command line
  23. | 1 2 3 7 10 11 12           <- output
  24. | %
  25.  
  26. My email bounced, so I will post this.  Hopefully the answer is more
  27. correct than scalar (%array) which I gave out.  Mea cupla :-)
  28.  
  29. Try:
  30.     @badargs = grep (! /^\d+(\.\.\d+)$/, @ARGV);
  31.     $" = "' '", die "$0: arguments are not numeric: '@badargs'\n" if (scalar (@badargs) != 0);
  32.     @foo = ();
  33.     grep (push (@foo, eval ($_)), @ARGV);
  34.     $, = " ";
  35.     $\ = "\n";
  36.     print @foo;
  37.  
  38. To explain it, lines 1-2 abort if any line is not a case range or
  39. simple number (to prevent calling eval with something that you don't
  40. expect).  Line 3 prevents an unused variable warning when using perl
  41. -w.  Line 4 puts into @foo the expanded case ranges.  The trick is
  42. that perl uses the syntax <value1>..<value2> to make an array of
  43. ranges from value1 to value2.  The remaining lines just print out the
  44. output.
  45.  
  46. If instead you wanted to take non numeric items as is without
  47. aborting, you would use:
  48.  
  49.     @foo = ();
  50.     grep (push (@foo, (/^\d+\.\.\d+$/ ? (eval ($_)) : ($_)), @ARGV);
  51.     $, = " ";
  52.     $\ = "\n";
  53.     print @foo;
  54. --
  55. Michael Meissner    email: meissner@osf.org        phone: 617-621-8861
  56. Open Software Foundation, 11 Cambridge Center, Cambridge, MA, 02142
  57.  
  58. You are in a twisty little passage of standards, all conflicting.
  59.