#!C:\Perl use strict; BEGIN{ @ARGV=map glob, @ARGV; } open(RES, ">>results.txt"); print RES "File Number A A% B B% Null Null%\n"; my $A = 0; #these three lines set my initial counts at zero my $B = 0; my $null = 0; my $filenum = 0; while( <> ){ chomp($_); if ($_ eq "stringa"){ $A++;} elsif ($_ eq "stringb"){ $B++;} else { $null++; } if( eof( ARGV ) ) { ## true after the end of each individual file my $popa = sprintf( '%.2f', $A / 1000 ); my $popb = sprintf( '%.2f', $B / 1000 ); my $popnull = sprintf( '%.2f', $null / 1000 ); my $filenum++; #Add one to my filenumber print RES "$filenum $A $popa $B $popb $null $popnull\n"; $A = $B = $null = 0; ## Reset counts for the next file } } #### ... my $var = ....; my $var = sprintf ... $var; ...