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!

first perl script...help anyone?

Status
Not open for further replies.

jimcasy

Technical User
Apr 15, 2008
2
FR
HI all,
I.m very new to Perl, but I've been told it's such a powerful language for text processing I wanted to try it and learn. So, I'm writing my first script which I need to process some text file.
Basically that's what I want to do: I have 3 files, I want to read some informations from the first 2, and then write on the third. In depth, I want to read every line from the first and retrieve that line (or part of it) in a line of the second. Now, in the second file, I want to read all the lines subsequent to the line retrieved untile a termination charachter is found (in my case, when a line equal to the dot (".") is met) and print some infos in third. I want to repeat the operation until all the lines from the first line are ended.
That's what I wrote, but it doesn't work, in particular the variable $count is always equal to 1. THat means it's not reading oll the lines from the first file.

!/usr/local/bin/perl -w

open(FILEWRITE, ">write_seg06091999.txt")|| die "Could not open $write_seg06091999.txt\n";
#open(FILESEG, "seg06091999.txt")|| die "Could not open $seg06091999.txt\n";
#open(FILEALIGN, "walign06091999.txt")|| die "Could not open $walign06091999.txt\n";
open(FILESEG, "seg_short.txt")|| die "Could not open seg_short.txt\n";
open(FILEALIGN, "align_short.txt")|| die "Could not open align-short.txt\n";
$count = 1;
my $line_align = " ";

## read every line of a file
while ($line_seg = <FILESEG>) {

while($line_align = <FILEALIGN>){
if($line_align =~ /19990609_1900_1920_inter_fm_dga.$count/){
do{

if($line_align ne "."){
$line_align = <FILEALIGN>;
print FILEWRITE $line_align, "$count\n";
}
}until $line_align eq ".";
last;
}
}
$count = $count+1;

}

you have some ideas why it's not working?
probably ny programming style and my explantion sucks, so feel free to ask for more clear explanations...

 
open(FILEWRITE, ">write_seg06091999.txt")|| die "Could not open $write_seg06091999.txt\n";

$write_seg0609199.txt shouldn't have the $ and you should include $!
die "Could not open write_seg06091999.txt: $!\n"; or something.

add
use strict;
use warnings;
to your script.

You should also close all your file handles after your done with them.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I would suggest you use chomp() on the lines you read, or include newlines in your patterns.

Every line has a newline at the end (that chomp will get rid of for you), so no line you read will ever equal '.' (as the line will actually consist of a dot and a newline ".\n"...).

It seems like your script will read through the entire FILEALIGN file in the first iteration of the loop. So $count is indeed always 1 on your screen. After the first iteration it will go through FILESEG till the end, incrementing $count on its way, but you'll never see this, as nothing is printed anymore.
 
actually, I solved my problem, and a great deal was the problem of the newline...I wasted quite a lot of time getting that the comparison were failing because in the original text file there was a newline after the dot...thanks for the chmop suggestion
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top