use warnings; use strict; my @errors; for my $input (1..3) { # main loop, just a demo # do something with $input here print "pattern$input\n"; if ($input%2) { # whatever your error condition is push @errors, "error$input"; } } print "\n#errors\n"; # main loop over, now output @errors for my $error (@errors) { print "$error\n"; } print "cellpattern\n";