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

Good Morning Monks!
My question is as follow: I am updating lines across many files at once and the following code does just that with two(2) files. However once I add a third(3rd) file it does NOT update. Although the time stamp in the files directory says it was...the data in the file stays the same. The other two(2) files DO get updated. Thanks for the help!

sub update_lcrecord_now { my ($lcid,$lcroadname,$lcroadnumber,$locotype,$lcowner,$lcinterrr,$lca +ssign) = @_; open my $lcinfile, '<',"$rrconfig::locodata" or die $!; open my $lcinfile2, '<',"$rrconfig::availlocodata" or die $!; open my $lcinfile3, '<',"$rrconfig::availlocoyng" or die $!; open my $lcoutfile, '>', "$rrconfig::locotemp" or die $!; open my $lcoutfile2, '>', "$rrconfig::availlocotemp" or die $!; open my $lcoutfile3, '>', "$rrconfig::availynglocotemp" or die $!; while (<$lcinfile>) { s/^$lcid\:.*/$lcid\:$lcroadname\:$lcroadnumber\:$locotype\:$lcowner\:$ +lcinterrr\:$lcassign/g; print $lcoutfile $_; } while (<$lcinfile2>) { s/^$lcid\:.*/$lcid\:$lcroadname\:$lcroadnumber\:$locotype\:$lcowner\:$ +lcinterrr\:$lcassign/g; print $lcoutfile2 $_; } # DOESN'T WORK while (<$lcinfile3>) { s/^$lcid\:.*/$lcid\:$lcroadname\:$lcroadnumber\:$locotype\:$lcowner\:$ +lcinterrr\:$lcassign/g; print $lcoutfile3 $_; } close $lcinfile; close $lcinfile2; close $lcinfile3; # DOESN'TT WORK close $lcoutfile; close $lcoutfile2; close $lcoutfile3; # DOESN'T WORK move "$rrconfig::locotemp", "$rrconfig::locodata" or die "move failed: + $!"; move "$rrconfig::availlocotemp", "$rrconfig::availlocodata" or die "mo +ve failed: $!"; move "$rrconfig::availynglocotemp", "$rrconfig::availlocoyng" or die " +move failed: $!"; # DOESN'T WORK }

Replies are listed 'Best First'.
Re: Update Multiple Files
by haukex (Archbishop) on Jan 05, 2021 at 15:30 UTC

    This question seems very similar to your last one, Delete Lines Across Multiple Files, and in fact the same advice (looping over the files instead of copy&pasting code) applies.

      @Haukex: you are correct. Adding/Deleting works perfectly.....updating works fine with 2 out of the 3 files.....the only difference is 2 files are in one directory and 1 is in another. I also gave permissions to FULL CONTROL on the sub directories eliminating any security issues. That did not make a difference.

        So, did you remove operations on the first two files and attempt the modifications on the third one by itself, to rule out that it's not the addition of the third file that breaks things, but it's ONLY the third file that's broken?

        Do so. This is called "halving the problem". If it's only the 3rd file that's borked, you can focus on it alone.

        Also, that's a lot of unneeded repetition, which is always prone to mistakes. Use a loop as suggested.

Re: Update Multiple Files
by choroba (Cardinal) on Jan 05, 2021 at 14:50 UTC
    I populated three files with fake ids, populated the package vars and imported move from File::Copy. It changed all three files.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      @Choroba: your code worked perfectly in stand alone mode. I think there might be an issue with my code since the 3rd file is in a completely different directory than the other 2 files are.

        And do you correctly set $rrconfig::availlocoyng to the full or relative path to the file? Is it just a different directory, or a completely different drive?

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Update Multiple Files
by Corion (Patriarch) on Jan 05, 2021 at 14:36 UTC

    Nowhere do you output the filenames. Are you sure that the filenames as stored in $rrconfig::locotemp, $rrconfig::availlocotemp and $rrconfig::availynglocotemp are what you expect them to be?

    Also, check the original filenames as well, and output these as well.

    If the third file always remains unchanged, my guess is that you're not even generating it. Move the third file away and likely your program will still work.

      @Corion: it does work if the third is taken away....everything is the same.....however the 3rd file is in a DIFFERENT directory than the other files.

Re: Update Multiple Files
by PilotinControl (Pilgrim) on Jan 05, 2021 at 16:23 UTC

    SOLVED!! I overlooked one thing....I tried changing the ID in the 3rd file to match the OTHER files and it WORKED!! So now I need to figure out how when ADDING each entry the IDs match in each file. As of right now the IDs are just added sequentially based on how many lines are in the file.