perl.beginners
[Top] [All Lists]

Re: help regarding Comparision

Subject: Re: help regarding Comparision
From: Yitzle
Date: Wed, 26 Sep 2007 14:44:03 -0400
Newsgroups: perl.beginners

Input:
--- START ---
abc
def
string1
das
dsfa
string2
dasf
string1
string2
dfsdsf
--- END ---

Code:
--- START ---
#!/usr/bin/perl

use warnings;
use strict;

die "Wrong usage\n" unless (defined $ARGV[0] and -f $ARGV[0]);
open my $FH, "< $ARGV[0]" or die "Can't open file\n";

my $string1 = 0; # Track if the first string occurred last line
while (<$FH>) {
    print "$_"; # Echo the line
    if ($string1) { # We hit string1 last line...
        if (/string2/) { # ...and string2 this line
            print "Found the double string!\n";
        }
    }
    $string1 = (/string1/);
}
close $FH;
--- END ---

Output:
--- START ---
abc
def
string1
das
dsfa
string2
dasf
string1
string2
Found the double string!
dfsdsf
--- END ---


HTH

<Prev in Thread] Current Thread [Next in Thread>