Hi,

I am a newbie to a perl.

I have a problem in getting old and new contents of file.

The file is like this:

StartTimeStamp,EndTimeStamp,counter1,counter2
Aug15 2010 22:45:43,Aug15 2010 23:00:00,6,0
Aug15 2010 23:00:00,Aug15 2010 23:07:43,3,0

The output should be:

Aug15 2010 23:00:00,Aug15 2010 23:07:43,3,0

After some process are run the file gets updated.

StartTimeStamp,EndTimeStamp,counter1,counter2
Aug15 2010 22:45:43,Aug15 2010 23:00:00,6,0
Aug15 2010 23:00:00,Aug15 2010 23:07:43,3,0
Aug15 2010 24:00:00,Aug15 2010 24:07:43,8,0
Aug15 2010 25:00:00,Aug15 2010 25:07:43,2,0

The output should be:

Aug15 2010 24:00:00,Aug15 2010 24:07:43,8,0
Aug15 2010 25:00:00,Aug15 2010 25:07:43,2,0

My question is get how to get the old and new contents of this file.

I tried using pop function but did not get it.

Any suggestions how to get the old and new values from a file in Perl?

Regards
Amith

#!/usr/bin/perl
#print_last_2_lines.pl
use strict;
use warnings;

my $previous; #Declare $previous and $latest outside the loop
my $latest;   # because we want to print them outside the loop

while (<DATA>){ #Loop through file, saving last two lines
    $previous = $latest;
    $latest = $_;
}
print $previous, $latest;

__DATA__
StartTimeStamp,EndTimeStamp,counter1,counter2
Aug15 2010 22:45:43,Aug15 2010 23:00:00,6,0
Aug15 2010 23:00:00,Aug15 2010 23:07:43,3,0
Aug15 2010 24:00:00,Aug15 2010 24:07:43,8,0
Aug15 2010 25:00:00,Aug15 2010 25:07:43,2,0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.