in reply to Help with pattern matching and calling wc -l

AWallBuilder:

You should show some sample input, the actual output and expected output to make it simpler for us to see the error. As it is, we need to either:

Also, if you indent your code to show the structure, it makes things a bit easier for us to see how it works. It should also help you when you're creating/modifying/debugging it. It even makes some errors pretty obvious, like so:

#!/usr/bin/perl -w my $Num_Bact_Virus_Chimera=0; my $Num_Bact_Bact_Chimera=0; my $Num_Virus_Virus_Chimera=0; my $outfile='MC38_ChimeraTable.txt'; my $MG_Num=1; open(OUT, ">$outfile") || die "Can't open outputfile $!\n"; print OUT join("\t",qw ( MG_Num Num_Bact_Virus_Chimeras Num_Bact_Bact_Chimera Num_Virus_Virus_Chimera ), "\n") ; while ($MG_Num <= 72) { @files=</outputMC38/*.MG$MG_Num.*>; foreach $file (@files) { if ($file=qr/HybridViralBactContigsList.txt/){ $Num_Bact_Virus_Chimera = qx/wc -l $file/; $Num_Bact_Virus_Chimera=$Num_Bact_Virus_Chimera-1; } if ($file=qr/HybridOnlyBactContigsList.txt/){ $Num_Bact_Bact_Chimera= qx/wc -l $file/; $Num_Bact_Bact_Chimera=$Num_Bact_Bact_Chimera-1; } if ($file=qr/HybridOnlyVirusContigsList.txt/){ $Num_Virus_Virus_Chimera=qx/wc -l $file/; $Num_Virus_Virus_Chimera=$Num_Virus_Virus_Chimera-1; } print OUT join("\t", $MG_Num,$Num_Bact_Virus_Chimera, $Num_Bact_Bact_Chimera, $Num_Virus_Virus_Chimera, "\n"); } } $MG_Num=$MG_Num+1;

Now that the code has some visual structure, one error immediately sticks out: Your while loop is checking when $MG_Num reaches 72, but you don't modify $MG_Num inside your loop. You only change it after the end of your while loop. I'm sure that's only one of your errors, but as you don't have sample data, expected and actual output and the error message, I'll just stop here.... ;^)

...roboticus

Replies are listed 'Best First'.
Re^2: Help with pattern matching and calling wc -l
by Anonymous Monk on Mar 10, 2010 at 13:30 UTC
    Thanks everyone, Okay, as cranky as roboticus sounded the indenting DID help, and so did the wc -l < $file. The script is working fine now.
    #!/usr/bin/perl -w my $Num_Bact_Virus_Chimera=0; my $Num_Bact_Bact_Chimera=0; my $Num_Virus_Virus_Chimera=0; my $outfile='MC38_ChimeraTable.txt'; my $MG_Num=1; open(OUT, ">$outfile") || die "Can't open outputfile $!\n"; print OUT join("\t",qw (MG_Num Num_Bact_Virus_Chimeras Num_Bact_Bact_Chimera Num_Virus_Virus_Chimera), "\n") ; while ($MG_Num <= 72) { @files=</g/bork6/mende/MGSimulation/hybridcontigs/outputMC38/*.MG$MG +_Num.*>; foreach $file (@files) { if ($file=~/HybridViralBactContigsList/){ $Num_Bact_Virus_Chimera=qx/wc -l < $file/; $Num_Bact_Virus_Chimera=$Num_Bact_Virus_Chimera-1; } if ($file=~/HybridOnlyBactContigsList/){ $Num_Bact_Bact_Chimera=qx/wc -l < $file/; $Num_Bact_Bact_Chimera=$Num_Bact_Bact_Chimera-1; } if ($file=~/HybridOnlyVirusContigsList/){ $Num_Virus_Virus_Chimera=qx/wc -l < $file/; $Num_Virus_Virus_Chimera=$Num_Virus_Virus_Chimera-1; } } print OUT join("\t",$MG_Num,$Num_Bact_Virus_Chimera,$Num_Bact_Bact_Chimera,$ +Num_Virus_Virus_Chimera,"\n"); $MG_Num=$MG_Num+1; }

    Output file

    MG_Num Num_Bact_Virus_Chimeras Num_Bact_Bact_Chimera Num_Virus_Viru +s_Chimera 1 0 3502 0 2 0 3356 4 3 0 3363 17 4 1 3374 41 5 2 3499 93 6 0 3498 89 7 0 4005 0 8 1 4415 4 9 0 3986 17 10 4 4382 41 11 3 4381 80 12 1 4415 80 13 0 1 0 14 0 0 4 15 0 0 16 16 0 0 44 17 0 1 86 18 0 1 87 19 0 3133 0 20 0 3301 3 21 0 3120 9

      Ah, well, I didn't intend to sound cranky. But I'm glad that it was some help to you.

      ...roboticus