in reply to searching for multiple strings in a line

I'm not sure why the script is stuck in a loop, but I do see one problem in:
foreach $x ($TheLine)
This loop will execute exactly once, setting $x to the value $TheLine. As your comment hinted at, you definitely want $TheLine to be an array, not a scalar variable.

If you can provide a sample of what your input file looks like, and an example input $String, that would make debugging a lot easier.

Replies are listed 'Best First'.
Re^2: searching for multiple strings in a line
by dhudnall (Novice) on Jul 10, 2007 at 17:14 UTC
    Well, my input file would consist of pulled SQLs such as the following:
    Select LTrim(RTrim(a.eeds_emp_id)) from whatever && Select Max(campaig +n_key) from whatever......
    THEN: I would enter a KEYWORD such as SELECT and it would search a line such as teh one above....say that it found the word SELECT...continue searching the line to see if there are any OTHER select statements in the line...if it is...then it count the line as having TWO Select statements and return a message saying...."this file contains SQLs with nested statements...etc. I know this may seem useless but for my project it is very necessary. With GREP, it is pulling the entire line and saying it is ONE SQL whereas I NEED it to pull that SQL and then run this program through the file to make sure I didn't miss any NESTED SQLS.
      split will separate your input line based upon the input keyword (such as SELECT). If SELECT appears more than once in the line, then the @things array will have more than one element, and you will get your message output.
      #!/usr/bin/perl use warnings; use strict; #This is where we read in the arguments from the command line my $file_name = shift; my $String = shift; #This is where we read in the file and output to the file open(INFILE, $file_name) or die "Can't open file\n"; #while reading from the file... while (<INFILE>) { $_ = uc(); my @things = split(/$String/); if ($#things > 0) { print "There is a nested SQL in this file\n"; exit; } }
        Once again thank you toolic. The code above worked. I figured out why my code wasn't working as well but the code you have above it much cleaner. Thank you for your time.

      One problem is that you can have multiple SELECT statements without having subselects, like so:

      SELECT A, B FROM FOO WHERE ... UNION SELECT C as A, D as B FROM BAR WHERE ...

      (Unless, of course, you consider the select statements as subselects of the union statement.) There may be other examples.

      Depending on your needs, you may need a more exhaustive parser--SQL::Parser might assist, but I can't comment any further as I've never used it.

      ...roboticus