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