my $i = 0; while(<>) { foreach(@inputs) { print @filehandles[$i] if (/@regex[$i]/../^END_OF_BLOCK/); $i++; } $i=0; }