Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Compare two files, nested while loops? Outer loop not iterating?

by naturalsciences (Beadle)
on Nov 21, 2011 at 17:14 UTC ( [id://939267]=perlquestion: print w/replies, xml ) Need Help??

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

I created a small script to take odd(n) lines from one file, evaluate if this line is a part of any line(whatsoever) from another file and then print this line and the next one (n+1) into an output file.

#!/usr/bin/perl -w use strict; use warnings; my $num_args = $#ARGV + 1; if ($num_args != 3) { print "\nUsage: ./fastqkokku fasta quala outputfile\n"; exit; } my $fasta=$ARGV[0]; my $quala=$ARGV[1]; my $out=$ARGV[2]; open FA, "< $fasta"or die "Can't open $fasta: $!"; open QA, "< $quala" or die "Can't open $quala: $!"; open OUT,"> $out" or die "Can't open $out: $!"; while (my $line = <QA>){ chomp $line; my $nextline = <QA>; while (my $compline = <FA>){ if ($compline =~ m/$line/){print OUT "$compline$nextline"; }} + } close FA; close QA; close OUT;

I repeat myself a bit here but .. well what I tried to accomplish with these while loops was > Outer loop.. iterate through all even $lines, get the odd $nextline for each of them, then for each of those $lines > Inner loop.. iterate through every line in FA, do the match and print out relevant $line$nextline combinations.

After testing the script with different input files I discovered that it will do all of those match evaluations only with the first $line of the outer loop. Is it connected to the nested-ness of those two loops. If I would eliminate the inner one and just leave my $compline = <FA>? would it then open the first line in FA over and over or would it open the n-th line of FA for each iteration of the loop ( iterations based on the QA lines) - both of these would be unsitisfactory. Can you help me to get the outer loop to iterate again, or suggest any other ways I could accomplish my goal which I have doubly described above?

Replies are listed 'Best First'.
Re: Compare two files, nested while loops? Outer loop not iterating?
by monkey_boy (Priest) on Nov 21, 2011 at 17:24 UTC
    You are actually not looping over the inner file, as on the 1st iteration you read to the end of the file and dont reset to the begining, try adding a seek, something like:

    #!/usr/bin/perl -w use strict; use warnings; my $num_args = $#ARGV + 1; if ($num_args != 3) { print "\nUsage: ./fastqkokku fasta quala outputfile\n"; exit; } my $fasta=$ARGV[0]; my $quala=$ARGV[1]; my $out=$ARGV[2]; open FA, "< $fasta"or die "Can't open $fasta: $!"; open QA, "< $quala" or die "Can't open $quala: $!"; open OUT,"> $out" or die "Can't open $out: $!"; while (my $line = <QA>){ chomp $line; my $nextline = <QA>; while (my $compline = <FA>) { if ($compline =~ m/$line/) { print OUT "$compline$nextline"; } } seek(FA,0,0); } close FA; close QA; close OUT;



    This is not a Signature...

      So in effect my original script would have a workflow like that > Iterate outer loop (QA lines)..first iteration.. along comes the inner loop (FA lines) > iterate wildly through it to the EOF, evaluating "if condition" if (hehe) it happens somewhere in the FA. Now should come the next iteration but FA is already read through, so no more lines from QA will ever be matched to anything.

Re: Compare two files, nested while loops? Outer loop not iterating?
by Anonymous Monk on Nov 21, 2011 at 17:31 UTC

      Thanks to previous posters. Posts so far have been really helpful. The behaviour of nested while loops was not something I expected - as I googled it right now it seems it is quite a surprise to many. I guess it is something you cant find so easily in ...for Dummies books anymore :D.

      I do admire the community. Not only do I usually learn a thing or two here or find a quick fix for something stupid I wrote in my script. But quite often when I google something Perl related I find a tremendous amount of links back to this place again and again.

Re: Compare two files, nested while loops? Outer loop not iterating?
by hbm (Hermit) on Nov 21, 2011 at 17:55 UTC

    Perhaps you need Zaxo, after $compline?

    Sorry, that was bad, but see his writeups.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://939267]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-25 18:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found