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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read and compare values from consecutive lines of a file ? 1

Status
Not open for further replies.

Rajey

Programmer
Feb 2, 2005
11
US
Hi Perl gurus :

I need your help…..I have a tab delimited file that I have to read till I am done reading all the lines. Each line has some values that I need to compare with the previous read line. And depending upon if the value match or do not match I need to insert the data into tables. Etc…..

My problem is that I do not know how in Perl we can read the tab delimited file such that I am able to remember the last read line and compare its data with the currently read line.

My initial thought was to store the data which is read from the first line of file into variables using the split function applied to each read line, but doing this in a loop I do not know how to retain the values read from one line so that when I am reading the next line I can extract the data into a variable and then compare with variables from the last red line with the current read line

Any help is appreciated

Thanks

Rajey
 
Code:
open FH, "<file.txt";

while (<FH>) {
  @this_rec=split /\|/,$_; #| is used as a sampel delimieter here, use what you need
  if ($this_rec[??] eq $last[??]) { #make what comparisons you need here
    #do what you gotta here
  }
  @last=@this_rec;
}
close FH;
Not tested, but should give you a good starting point

HTH

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Thanks a lot Paul ! I got to hang of it I had forgotten about the arrays ! I will try out and let u know how it goes

Regards

Rajey
 
Personally, I like Tie::File for this type of thing, but you can use a method like Paul has shown just as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top