CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Reading two lines at a time

 



blackeagle225
Novice

Jun 22, 2011, 12:11 PM

Post #1 of 8 (5085 views)
Reading two lines at a time Can't Post

Hello,

I have a data set, with 4 columns of data. I am trying to compare the element in the first row, column 4 with the second row, column 4 element. I am having a hard time, and would appreciate being pointed in the right directions. Thanks in advance.


miller
User

Jun 22, 2011, 1:48 PM

Post #2 of 8 (5084 views)
Re: [blackeagle225] Reading two lines at a time [In reply to] Can't Post

What have you tried thus far?

- Miller


blackeagle225
Novice

Jun 22, 2011, 1:59 PM

Post #3 of 8 (5083 views)
Re: [miller] Reading two lines at a time [In reply to] Can't Post

Hi Miller,

I tried a ton of different things, including seek, and loops within loops. I finally did figure it out. I just did

while (<FILE>) {
$CurrentLine = $NextLine;
$NextLine = $_;

....

It does require going through the loop one time to set both variables. Seems to be working now though. Thanks for your help anyway.


miller
User

Jun 22, 2011, 2:23 PM

Post #4 of 8 (5081 views)
Re: [blackeagle225] Reading two lines at a time [In reply to] Can't Post

Cool, glad you figured it out. I'd probably do something like the following:


Code
my $lastline = <FILE>; 

while (<FILE>) {
# Work with $_ and $lastline
...

$lastline = $_;
}



(This post was edited by miller on Jun 22, 2011, 3:09 PM)


FishMonger
Veteran / Moderator

Jun 23, 2011, 8:00 AM

Post #5 of 8 (5064 views)
Re: [blackeagle225] Reading two lines at a time [In reply to] Can't Post

I'd do it like this:

Code
while ( my $line1 = <$fh> ) { 
my $line2 = <$fh>;

}


My reasons: 1) it uses a lexical var for the filehandle, 2) it declares both vars in the smallest scope that the require, and 3) it doesn't needlessly use $_.


(This post was edited by FishMonger on Jun 23, 2011, 8:05 AM)


miller
User

Jun 23, 2011, 12:03 PM

Post #6 of 8 (5055 views)
Re: [FishMonger] Reading two lines at a time [In reply to] Can't Post

Agreed FishMonger,

However, does he want pairwise reading, or want to be doing logic on each adjacent line?

If based off his thread title, then yours would be the correct solution. But I think he actually wants to compare each line with it's adjacent, and your method would skip even pairs.

- Miller


rovf
Veteran

Jun 24, 2011, 5:12 AM

Post #7 of 8 (5043 views)
Re: [blackeagle225] Reading two lines at a time [In reply to] Can't Post

Since your implicitly require, that the input file contains an even number of lines, I would make this fact more explicit, even if the code looks a bit more complicated:


Code
while(1) { 
my $odd_line=<FILE>;
last unless defined $odd_line;
my $even_line=<FILE>;
die "This is odd: File has an odd number of lines\n" unless defined $even_line;
# process your pair of lines here
}



blackeagle225
Novice

Jun 24, 2011, 6:05 AM

Post #8 of 8 (5039 views)
Re: [miller] Reading two lines at a time [In reply to] Can't Post

Yeah, thanks Miller for understanding what I meant. Obviously still very green. I wanted to read a line, and read the next line and compare the two against one another. So far the program seems fine, but I appreciate all the help.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives