Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello: I'm having trouble getting the following to modify the MATCHED line in my flatfile, why isn't it rewriting / replacing the old line with the new line?
sub modify_flat_file{ open (USERFILE, "$flat_file"); $ID_matched = 0; while (<USERFILE>) { chop($_); @f = ($a, $ID, $b, $c)=split(/\|/,$_); if ($f[1] eq $FORM{'ID'}) { $ID_matched = 1; last;} close (USERFILE); if ($ID_matched == 1){ print USERFILE "$FORM{'new_a'}|$ID|$FORM{'new_b'}$FORM{'new_c'}'\n";

Replies are listed 'Best First'.
Re: modifying a matched line in a flatfile
by lshatzer (Friar) on Jun 04, 2002 at 17:56 UTC
Re: modifying a matched line in a flatfile
by silent11 (Vicar) on Jun 04, 2002 at 17:55 UTC
    The file handle USERFILE was closed, and when it was initally opend it was opend for read only. Look at the pod for open(). Update: I haven't used it, but you may want to give Text::xSV a try.

    -Silent11
Re: modifying a matched line in a flatfile
by Abigail-II (Bishop) on Jun 05, 2002 at 09:52 UTC
    So, what did Perl say when you tried this? You did turn on warnings, didn't you? ;-)

    You might want to take a look at Tie::File. And at the FAQ - your question is found there too.

    Abigail