Clemens Bieg wrote:
> 
> Could someone tell me what I am doing wrong? I want it to iterate over the
> lines of "extract", at each line going through all the lines of
> "allnumbered", checking if the line number is the same, and, if so, print
> out the line from "allnumbered". But with this I am getting only the first
> line, then it stops.
> 
> Many thanks in advance.
> Clemens
> 
> 
> while (<$fh_in_extract>) {
> 
>   if( /^(\d+)/ ) {
>
>     my $n = $1;
> 
>     while (<$fh_in_allnumbered>) {
>       if( /^$n / ) {
>         print $fh_out_final $_;
>       }
>     }
>   }
> }
> 

Once you have reached the end of the 'allnumbered' file all subsequent reads
will return undef, so you should open the file within the outer while loop;
something like the program below.

Note that this is a very slow way of doing things though. Unless both of your
files are huge it is far better to read the smaller one into an array so that
you don't need to keep rereading it. However if it's running fast enough for you
then you may as well leave things as they are.

HTH,

Rob


use strict;
use warnings;

open my $fh_out_final, '>', 'final' or die $!;

open my $fh_in_extract, '<', 'extract' or die $!;

while (<$fh_in_extract>) {

  next unless /^(\d+)/;

  my $n = $1;

  open my $fh_in_allnumbered, '<', 'allnumbered' or die $!;

  while (<$fh_in_allnumbered>) {
    print $fh_out_final $_ if /^$n /;
  }
}

close $fh_out_final or die $!;

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to