while ( my $line = ) { foreach ( 0 .. $#first_half ) { $line =~ s/$first_half[$_]/$second_half[$_]/; } }