my $all_words_regex = join '|', keys %lijst; for my $file (@listfiles) { open my $in, '<', $file or die "Can't open file '$file' for reading: $!"; open my $out, '>', "$file.out" or die "Can't open file '$file.out' for writing: $!"; while (<$in>){ if (m/^($all_words_regex)/){ my $first_word = $1; s/$first_word/$echo/g; s/$echo/$first_word/; } print $out $_; } } #### use strict; use warnings;