for( my $ix = 0; $ix < @ref; $ix++ ){ my $it = $ref[$ix]; if( $it =~ m/$in/i ){ print OUT $it,$ref[$ix+1]; } }