in reply to problem with string substitution output

You iterate over all words, and in each iteration you open the original file, change something, and print the altered output to another file.

This means that the output file is overwritten with what you print in the last iteration.

So you have to change the nesting of the loops. You can get around using the loop over the words altogether with a little trick:

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 $_; } }

BTW all of your programs should start like this:

use strict; use warnings;

And you should declare all your variables with either my (most of the time) or our.

Replies are listed 'Best First'.
Re^2: problem with string substitution output
by Anonymous Monk on May 23, 2008 at 16:20 UTC
    Hi Moritz,

    Thanks for your reply. However, I still can't get it to work.

    I have defined all variables, I use strict and warnings. I have changed the lines for opening the in and out files to
    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: $!";
    because otherwise it gave me this error:
    Unsuccessful open on filename containing newline at test.pl line 18

    With your alterations my program looks like this:
    use strict; use warnings; my $echo="ECHO"; my $value=undef; my $key=undef; my %lijst=(); open (DFFILE,$ARGV[0]) || die "DF-file not found\n"; open (LIST,$ARGV[1]) || die "List not found\n"; while (<DFFILE>) { ($value, $key) = split(/\t/, $_); $lijst{$key} = $value; } my @listfiles = <LIST>; 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 $_; } } close(DFFILE);
    But this gives me the exact same result as it did without your alterations. Or am I doing something wrong?

    Also, I realize I left out one aspect of my program. The substitution only needs to be done when the value of the hash key is 1.

    Matje
      Unsuccessful open on filename containing newline at test.pl line 18
      This tells you that have to remove the newlines from the lines you are reading from your file list:
      while (<DFFILE>) { chomp; # remove newline at the end. ($value, $key) = split(/\t/, $_); $lijst{$key} = $value; }
        I know about chomp and I'm pretty sure that's not the problem, since I already tried that ;)
        It also wouldn't explain why writing it with parentheses does work. But thanks anyway! Matje