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!

Only print if there is a match

Status
Not open for further replies.

perlnewbie9292

Programmer
Jul 15, 2009
25
US
Hello Perl experts,

I was hoping someone can help me with the following problem which I am stuck and have not had much luck trying to get to work.

The problem that I am running into is that I only want to "print" if the file that I am reading contains the ^Subject and ^Content-Type strings, if the file does not have these strings then skip over it or don't print anything.

Any help would be greatly appreciated.

Code:
my $dir = "$ARGV[0]";
find(\&matchingFiles, $dir);


sub matchingFiles {
   if ( (-f) && /^strrd-.*\.txt$/ ) {
        my $filename = "$File::Find::name";
        open(RDSFILE, "< $filename") or return(0);
        print "$filename\n";
        my $postHeader;
        my $filecontents;
        while (<RDSFILE>){
            $filecontent = $_;
            if ( 1 .. $content && /^Subject:/ ) {
                $content += /^Content-Type:/;
                print;
            }
        }
    }
    close(RDSFILE);
}
 
Are $filecontents, $filecontent and $content all supposed to be the same variable?

Anyway... rather than [p]print[/b]ing the output immediately, just accumulate the interesting data in a variable, and at the end of processing the file, only print it if those two strings were encountered at some point.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top