home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!spool.mu.edu!wupost!emory!sol.ctr.columbia.edu!eff!enterpoop.mit.edu!linus!philabs!acheron!scifi!watson!yktnews!admin!simon!cwalker
- From: cwalker@rchland.vnet.ibm.com (Cary Walker)
- Subject: Dynamic grepping
- Sender: news@rchland.ibm.com
- Message-ID: <1992Dec23.155457.15647@rchland.ibm.com>
- Date: Wed, 23 Dec 1992 15:54:57 GMT
- Disclaimer: This posting represents the poster's views, not necessarily those of IBM
- Nntp-Posting-Host: simon.rchland.ibm.com
- Organization: IBM Rochester
- Lines: 44
-
- Is there a way to have the string that grep checks for stored in a variable? For
- example, say I wanted to find those lines in an array of lines that contained
- digits. Could I store the regular expression, \d, in a variable and have grep
- check the array against that?
-
- Here's a small sample program to express my point:
-
- #!/usr/bin/perl
-
- unshift(@checkstrs, "1111foo222");
- unshift(@checkstrs, "foofoo");
- $grepstr="\d";
-
- @digitsfound1 = grep(/$grepstr/, @checkstrs);
- @digitsfound2 = grep("$grepstr", @checkstrs);
- @digitsfound3 = grep('$grepstr', @checkstrs);
- @digitsfound4 = grep(/\d/, @checkstrs);
-
- foreach $digitfound1(@digitsfound1) {
- print "1st run=$digitfound1\n";}
- foreach $digitfound2(@digitsfound2) {
- print "2nd run=$digitfound2\n";}
- foreach $digitfound3(@digitsfound3) {
- print "3rd run=$digitfound3\n";}
- foreach $digitfound4(@digitsfound4) {
- print "4th run=$digitfound4\n";}
- The output is:
-
- 2nd run=foofoo
- 2nd run=1111foo222
- 3rd run=foofoo
- 3rd run=1111foo222
- 4th run=1111foo222
-
- The 4th grep expression worked like I wanted it to but the regular expression is
- explicit there and not in a variable.
-
- I've looked into the eval function but to no avail.
-
- Thanks!
-
- -Cary
-
-
-