naturalsciences has asked for the wisdom of the Perl Monks concerning the following question:

I'm having trouble with following code. It should take in two files:

One containing codes to compare with certain lines of other input file.

The other, input file, is line after line of heading and information lines heading_withcode1\ninfo1\heading_withcode2n\info2)

Then it should open outputfiles for each code and print heading lines containing code and the next lines (info) after into appropriate files.

It will create the first code.out file and load it nicely with appropriate lines. Then it will open code2.out and stay there computing. I did put the print "." statement there to check out what is going on - for the second code it'll print two dots only and then stay there. If I put the print statement after the nextline statement it'll only print one dot and stay there. It kind of seems like it cant get a nextline on the second iteration and then hangs there. Not getting nextline should then mean it is too close to EOF and also that it hasn't got even number of lines to EOF -> hasn't my seek(IN,0,0) worked as I thought it would or is there something else I have missed?

#!/usr/bin/perl -w use strict; use warnings; open(GRP,'grupid.txt'); my @grupid=<GRP>; open (IN,"<",$ARGV[0]); foreach my $kood (@grupid) { chomp $kood; print "opened $kood\n"; open(TEMP,">","$kood.out"); while (my $line = <IN>) { # print "."; my$nextline=<>; # print "."; if ($line =~ /$kood/) { print TEMP "$line$nextline"; } } close(TEMP); print "closed $kood\n"; seek(IN,0,0); print "rewound\n"; } close(GRP); close(IN);

Replies are listed 'Best First'.
Re: problems with seek?
by choroba (Cardinal) on Feb 17, 2015 at 12:23 UTC
    Are you sure you want to read $nextline from <> and not from <IN>?
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Ahh the humanity! Thank you - I am accursed with making stupid errors like that. I'll check it out but it seems like it might be the problem.