Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

eh, dumb grep problem 2

Status
Not open for further replies.

Oostwijk

Technical User
Oct 19, 2003
82
0
0
NL
To test if I understand the grep command correctly I used this little script:

$items="no way test";
if (grep /$items/,"test"){print "ok";}

These lines should print "ok" sice test appears in $items.
Though "ok" isn't printed. What am I doing wrong here ?
I've tried it the other way around:
if (grep /"test"/,$items){print "ok";}
but that didn't work either
 
That's not what grep is for. If you want to check for a string within a string, just use a regexp:
Code:
if ($items =~ /test/) {
   # it matches
}

grep is used for find relevant elements in a list.
 
(1) "grep" in "perl" is NOT used the same way as "grep"
in Unix shells.

"grep" in perl is used to test if some particular item is present in a "perl" ARRAY.

While "grep" in Unix shell scripts is used to determine if a line (record) of a file contains the expression you are "grepping" for.

The term "grep" is the same, BUT the uses of the term are quite different between "perl" and Unix script languages.

(2) "ishnid" is quite corect - in "perl", for what you said, you want to do, use "pattern matching" with a "regular expression". A "perl" program , as ishnid says, is a great way to accomplish what it seemed to me that you want to do. Since, apparently, you are NOT dealing with an ARRAY, in "perl", you do not have any appropriate use for "grep" in "perl", for what you want to do.

(3) The book, "Perl Black Book, 2nd. edition", Stever Holzner, published by Coriolis, Pages 85-87, 210-212 has a good explanation about "grep" in "perl". I recommend this (I think excellent) book FOR ANYTHING in "perl".


(4) The book, "Perl Developer's Guide", Pescho and DeWolfe, published by McGraw Hill\Osborne, also has good material on "grep" in "perl" on pages 348-342. I recommend this (I think excellent) book FOR ANYTHING in "perl", also.

(4) Two excellent books for "pattern matching", and "regular expressions", in 'perl", which, apparently is what you need to use are:

(5) "Perl by Example", Quigley, puablised by PH PTR, Chapter-7. I recommend this (I think excellent) book FOR ANYTHING in "perl".

(6) "Perl Black Book" (above (3)), Chapter-6.I recommend this (I think excellent) book FOR ANYTHING in "perl".

(7) I enthusiatically recommend the above books!

Bye for now.

End-of-memo: Best to you..from ernieah.
I don't sell books, or work for any
publisher or computer company.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top