Question

For my program, I'm attempting to replace the value of a specific hash in an external file with a newly created value. The external file has the value tab-delimited from the key, and I had read the hash in from the external file. I've been looking around online, and this is the closest way I could figure out how to do it, yet it doesn't seem to work.

            open(IN, ">>$file") || die "can't read file $file";
            while (<IN>) {
            print IN s/$hash{$key}/$newvalue/;
            }
           close (IN) 

I'm not quite sure what I'm missing in this formula.

Was it helpful?

Solution

Tie::File can fix this for you.

use Tie::File;

tie @array, 'Tie::File', $file or die "Could not tie $file: $!";

for (@array) {
    s/$hash{$key}/$newvalue/;
}
untie @array;

OTHER TIPS

You are trying to read and write to the same file, that is not going to work. You have to read, substitute then write in another file. Afterwards, you can replace the input file by the one you've just written if you really want one file.

This won't be efficient, but it should work, unless my perl-fu is bad:

open(IN, "<<$file") || die "can't read file $file";
open(OUT, ">>${file}.tmp") || die "can't open file $file";
while (<IN>) {
    print OUT s/$hash{$key}/$newvalue/;
}
close(IN);
close(OUT);
exec("mv ${file}.tmp $file");

There might be a command to do the move for you in perl, but I'm not a perl guy.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top