dkaplowitz has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have a working script that searches a file for the existence a simple string and prints some output and creates a file:
#!/usr/bin/perl use warnings; use strict; open OUTPUT, ">c:/temp/warning.txt"; open LOGFILE, "shift.txt" or die "What you talkin bout Willis? ($!)"; while (<LOGFILE>) { print "$_\n" if /secret\.html/; print OUTPUT "The text you were looking for was found\."; last; } close OUTPUT;
I'd like to expand this script
A: to search the same file for when the string "secret.html" does _not_ exist, and
B: to be able to search for a more complex string than "secret.html". Actually, it'll be 4 more complex strings, to be exact, examples of which are:
"imagdata.zip is ready for transfer"
"ConnectDirect Transfer was initiated"
"Imagdata.zip has been renamed to Imagdata_yyyy-mm-dd.zip"
"Finished pruning D:\IP\29509"
I don't really know how to tell Perl to ignore things like whitespaces and the ":\" in pathnames, etc. And I don't yet really know how to create and use an array, so I don't mind just making a variable for each string, something akin to:
stringa = '"imagdata.zip is ready for transfer"'; stringb = '"ConnectDirect Transfer was initiated"';, etc.
Can anyone help me with the syntax? Thanks! Dave

Replies are listed 'Best First'.
Re: Search for complex string, and if it doesn't exist...
by Abigail-II (Bishop) on Mar 25, 2004 at 16:58 UTC
    A: to search the same file for when the string "secret.html" does _not_ exist, and
    Just change the 'if' to 'unless'.
    B: to be able to search for a more complex string than "secret.html". Actually, it'll be 4 more complex strings, to be exact, examples of which are:
    That's just 4 exact strings. Searching for exact strings is trivial. But...
    I don't really know how to tell Perl to ignore things like whitespaces and the ":\" in pathnames, etc.
    now we enter the realm of vagueness. You have to describe exactly what you mean. Descriptions of the form "things like" and "etc" isn't something we can use to help you write the regex. 90% of writing a regex is to be able to describe what you mean - and until you are able to do so, noone can help you write a regex, except by guessing.

    Abigail

      Thanks Abigail for the reply. And Oops! Sorry if I was vague. Actually, the "complex strings" I mentioned are these:
      "imagdata.zip is ready for transfer"
      "ConnectDirect Transfer was initiated"
      "Imagdata.zip has been renamed to Imagdata_yyyy-mm-dd.zip"
      "Finished pruning D:\IP\29509"
      So, I'm trying to search for a condition where the above four lines are _not_ in the log file being searched. I'm trying the "unless" instead of "if" now. Dave
        So, I'm trying to search for a condition where the above four lines are _not_ in the log file being searched.

        while (<LOGFILE>) { if ( /this|that|the\.other/ ) { print $_; print "The text you didn't want to find was found.\n"; last; } }

        -QM
        --
        Quantum Mechanics: The dreams stuff is made of

Re: Search for complex string, and if it doesn't exist...
by Scarborough (Hermit) on Mar 26, 2004 at 15:26 UTC
    Abigail is right what you wont to do is just search for different strings they are no more complex then secret.html. I think you might be geting worried about nothing, but when you come across things like :\ for the first time you can usually get by with doing a backslash to make the next character literal ie. :\\.