FAQ
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...

Search Discussions

  • John W. Krahn at Apr 16, 2008 at 2:50 am

    Meriadoc Overhill of Nobottle wrote:
    HI all, Hello,
    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. Welcome.
    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
    The first two characters (at least on *nix systems) have to be '#!'.
    And that line should be followed by the warnings and strict pragmas.

    #!/usr/local/bin/perl
    use warnings;
    use strict;

    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";
    You should include the $! variable in the error messages so you know why
    open failed.

    perldoc perlvar

    $count = 1;
    my $line_align = " ";

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

    while($line_align = <FILEALIGN>){
    When you read the first line via <FILESEG> you then read through the
    entire second file so when you get to the second and every subsequent
    line from <FILESEG> this loop will be bypassed as <FILEALIGN> will
    always return undef.

    if($line_align =~ /19990609_1900_1920_inter_fm_dga.$count/){
    In a regular expression the character '.' means that you want to match
    any character at that position. If you want to match a literal '.'
    character you have to escape it /\./. The end of the pattern is not
    anchored so if $count contains '1' then it will also match '10', '11',
    '12345', etc.

    do{

    if($line_align ne "."){
    $line_align = <FILEALIGN>;
    print FILEWRITE $line_align, "$count\n";
    }
    }until $line_align eq ".";
    Do you really need to check for $line_align being equal to '.' twice in
    the same while loop?

    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...
    It would help if we could see what your data looks like.


    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedApr 15, '08 at 1:49p
activeApr 16, '08 at 2:50a
posts2
users2
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase