PilotinControl has asked for the wisdom of the Perl Monks concerning the following question:
My code deletes all the records not just the one line I need any solutions? Thanks in advace.
sub del { my $output="data.txt"; my ($delroadName, $delroadNumber) = @_; open(DAT,"$output") || die("Can't open file"); my @data = <DAT>; close (DAT); chomp (@data); open(DAT,">$output") || die("Can't open file"); for (@data) { my @field = split(/\:/); next if $field[0] eq $delroadName && $field[0] == $delroadNumber; print ("$_\n"); } close $output; }
data.txt
po:789:auto:home
avr:801:racks:home
with the above code it erases everything in the file not the line needing removed for example if i want to remove avr:801:racks:home the code removes all data
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Deletes All Records not just one
by Plankton (Vicar) on Feb 16, 2013 at 06:26 UTC | |
by tobyink (Canon) on Feb 16, 2013 at 07:52 UTC | |
|
Re: Deletes All Records not just one
by Athanasius (Archbishop) on Feb 16, 2013 at 04:59 UTC | |
|
Re: Deletes All Records not just one
by Anonymous Monk on Feb 16, 2013 at 04:53 UTC | |
|
Re: Deletes All Records not just one
by tmharish (Friar) on Feb 16, 2013 at 10:13 UTC | |
by PilotinControl (Pilgrim) on Feb 16, 2013 at 13:21 UTC | |
by tobyink (Canon) on Feb 16, 2013 at 13:42 UTC | |
|
Re: Deletes All Records not just one
by Anonymous Monk on Feb 16, 2013 at 04:56 UTC |