For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2006 > Re: read consecutive lines









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: read consecutive lines
D. Bolliger

2006-10-20, 6:56 pm

Luba Pardo [offlist]:
> D. Bolliger am Freitag, 20. Oktober 2006 15:00:
he[color=darkred]

Hello

Please answer to the list.
[color=darkred]
> As I mentioned before I am trying to match the information of two files
> simultaneously. Then @a1 is file_2 (that have to be evaluated), but I did
> not included as I am getting trouble to process file_1 (READER_1) because
> is too big.
>
> I tried to process this file by saving as an array as I did with READER_2,
> bit it did not work. So I tried to read every three lines...


Below READER_2 is used, not READER_1.

> I used
> =C2=A0 =C2=A0 $I=3D0;


Variable names are case sensitive. You use two variables $I and $i. Read=20

perldoc perldata

for information about variables.

> =C2=A0 =C2=A0 =C2=A0while ($line=3D<READER_2> ) {


$line is a scalar variable (containing one line), not an array (would be=20
@line), so you can't access $line[$i].

> =C2=A0 =C2=A0 @temp2=3Dsplit/[<>]/,$line[$i];
> =C2=A0 =C2=A0 @temp3=3Dsplit/[<>]/,$line[$i+1];
> =C2=A0 =C2=A0 @temp4=3Dsplit/[<>]/,$line[$i+2];
> =C2=A0 =C2=A0 $I++;
> }
>
> but it did not work either. So I though of making a loop and process line
> by line.


Remember to put=20

use strict;
use warnings;=20

at the top of the code and declare your variables with my (perldoc -f my)

Anyway, you got all lines into an array by

my @lines=3D<READER_2>;

Below is a sample code that loops through a file by reading 3 lines at a ti=
me=20
(it's very unelegant, I seem to have a blackout, so look out for other=20
answers).=20
It checks if the input file has the right amount of lines (multiple of 3).
At the end of the loop, you have 3 lines in @lines3 that you can use for=20
further processing with data from <READER_2>.

#!/usr/bin/perl

use strict;
use warnings;

while (1) { # unelegant way to handle 3 lines of big file at a time
my @lines3=3D();
defined ($lines3[0]=3D<DATA> ) or last; # stop if no more lines
defined ($lines3[1]=3D<DATA> ) or die '# of lines not a multiple of 3';
defined ($lines3[2]=3D<DATA> ) or die '# of lines not a multiple of 3';

# demonstration how to access one of the three lines:
#
warn "1/3: $lines3[0]", "2/3: $lines3[1]", "3/3: $lines3[2]";
}


__DATA__
line1
line2
line3
line4
line5
line6
line7 <<< gives error



Hope this helps to get a new version of your code.

Dani
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com