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

Good Morning Monks!
The sub routine code posted below does what I want except it copies all lines over to a new file instead of one line based on its unique key. What the code does is remove a line from one file however it copies All lines instead of the updated line to the new file. What am I missing? The update code works as it updates a record. What my focus is on is moving that updated line from the original cardata.txt to the updatedcardata.txt Thanks in advance!

use strict; use warnings; use File::Copy;

UPDATE
This is the code that calls the sub below just for clarification

my ($rcid,$car,$owner); print "\nSelect Value To Edit: "; chomp(my $enter=<STDIN>); if ($enter) { if ($enter) { print "\nEnter New Value: "; chomp(my $new_value=<STDIN>); if ($new_value) { if ($enter == 1) { update_record_now($rcid,$new_value,$owner); } elsif ($enter == 2) { update_record_now($rcid,$car,$new_value); } } } } sub update_record_now { my ($rcid,$car,$owner) = @_; open my $infile, '<','cardata.txt' or die $!; open my $outfile, '>', 'cartemp.txt' or die $!; while (<$infile>) { s/^$rcid\:.*/$rcid\:$car\:$owner/g; print $outfile $_; } my $pinfile = "cardata.txt"; my @data; { open(my $data_file_handle, '<', "$pinfile") or die("Can't open file: $ +pinfile"); @data = <$data_file_handle>; close($data_file_handle); close $pinfile; } chomp(@data); open(my $out_file_handle,'>', $pinfile) or die("Can't open file: $pinf +ile"); foreach my $line_from_file ( @data ) { my @field = split( /\:/, $line_from_file ); next if( $field[0] == $rcid ); print $out_file_handle $line_from_file, "\n"; } close $pinfile; close $out_file_handle; close $infile; close $outfile; move 'cartemp.txt', 'updatedcardata.txt'; } __DATA__ 1:Chevy:Bob 2:Ford:Tom 3:Fiat:Dave

Replies are listed 'Best First'.
Re: Data Move
by aaron_baugher (Curate) on May 27, 2015 at 13:46 UTC

    Your code never calls your subroutine, so I assume something is missing here. But in general, if you only want to print the lines that are matched by your find-and-replace regex:

    if( s/^$rcid\:.*/$rcid\:$car\:$owner/g ){ print $outfile $_; }

    Aaron B.
    Available for small or large Perl jobs and *nix system administration; see my home node.

      I just posted the code that I am having an issue with. The search and replace code works fine...and it deletes the record and moves it to a new file...that part of it works...what the code is doing is also moving ALL data from that file to the updated file no just the updated data.

      Aaron B....after moving a few lines of code around I was able to solve the issue...code placement is very key to order and structure. Thanks for your advice.

Re: Data Move
by ww (Archbishop) on May 27, 2015 at 15:57 UTC

    PilotinControl: As I was checking to see that I was citing the correct line numbers in preparing to observe that your (ORIGINAL!) Ln 35 trashes the previous array created with data from Ln 34 each time thru the foreach loop, I found that you've replaced the code -- with an update note to be sure, but with no indication of how the line numbers changed.

    Downvoted, for that, and your consistent habit of ignoring PM's standards and for asking questions to which you've previously been given good answer.

    And, lest you've trashed the context of other answers, here's the original (the lines cited in para 1 are now 50-51 in your updated OP):

    use strict; use warnings; use File::Copy; sub update_record_now { my ($rcid,$car,$owner) = @_; open my $infile, '<','cardata.txt' or die $!; open my $outfile, '>', 'cartemp.txt' or die $!; while (<$infile>) { s/^$rcid\:.*/$rcid\:$car\:$owner/g; print $outfile $_; } my $pinfile = "cardata.txt"; my @data; { open(my $data_file_handle, '<', "$pinfile") or die("Can't open file: $ +pinfile"); @data = <$data_file_handle>; close($data_file_handle); close $pinfile; } chomp(@data); open(my $out_file_handle,'>', $pinfile) or die("Can't open file: $pinf +ile"); foreach my $line_from_file ( @data ) { my @field = split( /\:/, $line_from_file ); next if( $field[0] == $rcid ); print $out_file_handle $line_from_file, "\n"; } close $pinfile; close $out_file_handle; close $infile; close $outfile; move 'cartemp.txt', 'updatedcardata.txt'; } __DATA__ 1:Chevy:Bob 2:Ford:Tom 3:Fiat:Dave

      And again...I will explain the issue:
      1.) The code deletes the data line from one file and moves it to another. Which is what I want.
      2.) However, ALL lines from file (1) are also sent to file (2) and that is not what is desired.
      The answer provided does not address this issue.

        Problem solved...thanks for any and all advice from everyone in the monestary..it helps to have multiple eyes looking over code than just one set!