Matt O'neill wrote:

> hi yeah, i have started to write the code but am having troubles with the
> various operators around the search bit, anyway here goes:
>
> my ($file_name, $search_string) = @ARGV;

You indicated problems with the operators around the search.  I'll assume you mean 
operators contained in the search string.  All in all, your code looks alright, but 
could use some polishing.  Good habits should come early.

The problems with operators may come from the way some characters are used as control 
characters in some contexts.  It might be best to try the qr() [quote-regex] or qm() 
[quotemeta] functions to insure that any control character is properly escaped.

my $cleaned_string = qm($search_string);

Then test against cleaned_string.

> open(INFILE, "< $file_name");

Always check to see that the file is open.  for simple scripts, it is usually best to 
have your program end with an error message, or die():if the open call, or a close 
call, has failed

open(INFILE, "< $file_name") or die("Failed to open $file_name$!");

> while (<INFILE>) {
>
> if ($_ =~ $search_string) {
>
> print "yes\n";
>
> exit();
>
> }
>
> }
>
> print "no\n";
>
> Thanks for your help

A few pointers:

Right now, you have little way to know what is happening, because all you see is yes, 
no, or nothing at all.  That doesn't give you much to go on.  Especially when you are 
first learning, and anytime you are in doubt about what the program might be seeing, 
use print statements to shed some light:

#!path_to_perl_dir/perl -w

use strict;
use warnings;

my ($file_name, $search_string) = @ARGV;
print "Preparing to open $$file_name ";  #  give each variable a separate line here
print "to search for $search_string\n";     #  to better see if one is missing
open(INFILE, "< $file_name");              #  lose those blank lines--unless they 
really signal something
my $cleaned_string = qm($search_string);
while (<INFILE>) {
  print "Testing $_ for match\n";
  if ($_ =~ /$cleaned_string/ {    # regular expresion should be between delimiters
    print "Yes, $search_string was foind in $_\n";
    exit();
  }
}
print "No $search_string was never found.\n";

I don't know what is in the file you are testng, or what kind of strings you are 
testing with, but this should give you a start.  Work with this a little, and let us 
know how it's coming along.

Joseph



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to