in reply to Perl printing in the wrong order

To help troubleshoot next time try putting some print statements in to announce each step with couter values, filenames being opened, etc. Inside loops I like to put in things like

my $file=$logpath . $ip; print "\$counter=$counter: opening $file\n" if $debug; print "\$counter=$counter: printing $date to $file\n" if $debug;