For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2006 > RREAD 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 RREAD CONSECUTIVE LINES
Luba Pardo

2006-10-20, 7:57 am

Dear all,
I am trying to write a script that reads in two or three consecutive lines
to process those. For example that if the first line of file_1 match with an
scalar from a file_2, then print the lines 2 and 3 of the file_1. I tried to
save everything in a array, but the file is extremely large and it kicks me
out.

So, I have tried to work line by line:

open (READER_1,"out2222") || die "\n I can't open the file READER_1 !!\n";
open (READER_2,"out.txt") || die "\n I can't open the file READER_2 !!\n";

@a1= <READER_2>;
close (READER_2);

@arr=();

for ($i=0; $i<=$#a1;$i=$i+1 ) {

$l= <READER_1>;

@temp1=split/[<>]/,$l[$i];
@temp2=split/[<>]/,$l[$i+1];
@temp3=split/[<>]/,$l[$i+2];
@temp4=split/[<>]/,$l[$i+3];

print " @temp1 is temp1\n";
}

BUT I DO NOT GET ANYTHING. I wonder if there is another way to work it out.

Many thanks,

L. Pardo

Paul Lalli

2006-10-20, 7:57 am

Luba Pardo wrote:
> I am trying to write a script that reads in two or three consecutive lines
> to process those. For example that if the first line of file_1 match with an
> scalar from a file_2, then print the lines 2 and 3 of the file_1. I tried to
> save everything in a array, but the file is extremely large and it kicks me
> out.


A good reason to never read an entire file into an array.

> So, I have tried to work line by line:


Which is what you should have done in the first place...

> open (READER_1,"out2222") || die "\n I can't open the file READER_1 !!\n";
> open (READER_2,"out.txt") || die "\n I can't open the file READER_2 !!\n";


Use lexical filehandles.
Use the three-argument form of open
State *why* the open failed if it did.

open my $READER1, '<', 'out2222' or die "Cannot open Reader1: $!\n";
open my $READER2, '<', 'out.txt' or die "Cannot open Reader2: $!\n";

> @a1= <READER_2>;


Didn't you just say you were going to work line by line? Why are you
still reading the entire file into memory?

> close (READER_2);
>
> @arr=();


What is the point of this variable? You never use it again.

> for ($i=0; $i<=$#a1;$i=$i+1 ) {


GAH. And then you go ahead and process the array line by line anyway.
What was the point of reading it into an array?

while (my $r2_line = <$READER2> ){

> $l= <READER_1>;


> @temp1=split/[<>]/,$l[$i];
> @temp2=split/[<>]/,$l[$i+1];
> @temp3=split/[<>]/,$l[$i+2];
> @temp4=split/[<>]/,$l[$i+3];


If you had enabled strict and warnings, Perl woudl have told you what's
wrong with this mess. You have a scalar variable $l. Here you're
trying to use an array variable @l, which does not exist.

my @temp;
for (0..3) {
$temp[$_] = [ split /[<>]/, <$READER1> ] ;
}

> print " @temp1 is temp1\n";


And then you completely ignore the other three variables you were
trying to create?

print "\@temp$_ is @{$temp[$_]}\n" for 0..$#temp;
> }
>
> BUT I DO NOT GET ANYTHING. I wonder if there is another way to work it out.


I have NO idea what you're trying to do, because you haven't told us.
I therefore have no suggestions of "another way" to do whatever it is
you're trying to do. I can only tell you to correct the bugs you have
above.

Paul Lalli

D. Bolliger

2006-10-20, 7:57 am

Luba Pardo am Freitag, 20. Oktober 2006 14:16:
> Dear all,


Hello

> I am trying to write a script that reads in two or three consecutive lines
> to process those. For example that if the first line of file_1 match with
> an scalar from a file_2, then print the lines 2 and 3 of the file_1. I
> tried to save everything in a array, but the file is extremely large and it
> kicks me out.
>
> So, I have tried to work line by line:


You miss

use strict;
use warnings;

and the handling of the errors/warnings that would show up (declare variables
with my etc.).

Without these, its very easy to write messy code that won't do what you want.

> open (READER_1,"out2222") || die "\n I can't open the file READER_1 !!\n";
> open (READER_2,"out.txt") || die "\n I can't open the file READER_2 !!\n";


Include $! in the die string to get the reason when open failes.

> @a1= <READER_2>;


The contents of @a1 are never used, only its size.

> close (READER_2);
>
> @arr=();


Never used.

> for ($i=0; $i<=$#a1;$i=$i+1 ) {


Not clear to me what you want in this for loop:

> $l= <READER_1>;


$l contains one line from READER_1.

> @temp1=split/[<>]/,$l[$i];


There is no array @l that could be accessed with $l[$i].

> @temp2=split/[<>]/,$l[$i+1];
> @temp3=split/[<>]/,$l[$i+2];
> @temp4=split/[<>]/,$l[$i+3];
>
> print " @temp1 is temp1\n";
> }
>
> BUT I DO NOT GET ANYTHING. I wonder if there is another way to work it out.


Dani
Mumia W.

2006-10-20, 6:56 pm

On 10/20/2006 07:16 AM, Luba Pardo wrote:
> Dear all,
> I am trying to write a script that reads in two or three consecutive lines
> to process those. [...]


Do you want to show some sample data? Do you want to show what the input
data is supposed to look like and what the output is supposed to look like?


Sponsored Links







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

Copyright 2008 codecomments.com