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 | |
by PilotinControl (Pilgrim) on May 27, 2015 at 15:01 UTC | |
by PilotinControl (Pilgrim) on May 27, 2015 at 16:42 UTC | |
|
Re: Data Move
by ww (Archbishop) on May 27, 2015 at 15:57 UTC | |
by PilotinControl (Pilgrim) on May 27, 2015 at 16:29 UTC | |
by PilotinControl (Pilgrim) on May 27, 2015 at 16:44 UTC |