Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Deletes All Records not just one

by PilotinControl (Scribe)
on Feb 16, 2013 at 04:43 UTC ( #1019006=perlquestion: print w/ replies, xml ) Need Help??
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; }

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

Comment on Deletes All Records not just one
Download Code
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 Anonymous Monk on Feb 16, 2013 at 04:56 UTC
Re: Deletes All Records not just one
by Athanasius (Monsignor) on Feb 16, 2013 at 04:59 UTC

    The code as shown is not deleting any records. Also, the filehandle DAT is opened for writing, but never closed: close $output; should be close DAT;. But I suspect the real problem is in the line:

    next if ($field[1], $field[2]) = ($delroadName, $delroadNumber);

    Perhaps you meant:

    next if $field[1] eq $delroadName && $field[2] == $delroadNumber;

    Note that = is for assignment. To test for equality, == is used for numbers, and eq for strings.

    Also note that $field[1] is the second field, as arrays start from element zero. (Your code may be correct here — I only mention this as a point to double-check.)

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Deletes All Records not just one
by Plankton (Priest) on Feb 16, 2013 at 06:26 UTC
    Maybe you should do ...
    print DAT "$_\n";
    ... instead of ...
    print ("$_\n");
    You might also might want to learn about yaml or sqlite. Dealing flatfile databases is just a pain, especially when there are better tools.

      A simple ++ is not enough. You have hit the nail on the head. His code currently prints to STDOUT instead of printing to the file.

      package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
Re: Deletes All Records not just one
by tmharish (Friar) on Feb 16, 2013 at 10:13 UTC
    And putting together all of what the others have said ( and adding some sanity checks I follow when things start going wrong ) :
    use strict ; use warnings ; sub del { my ( $delroadName, $delroadNumber ) = @_ ; # This line handles pa +rams for function, # moved to first pa +rt of function my $process_file = "data.txt"; # Variable name change +d to reflect action - # This kind of thin +g can mess things up later. my @data ; { # Adding additional scope to prevent missuse or error use of fileh +andle. open( my $data_file_handle, '<', "$process_file" ) # Changed to +3 param open and replaced bareword file handle. or die("Can't open file: $process_file"); # Changed || +to 'or' in case someone removes brackets for 'open' # Also +added file name to error message. @data = <$data_file_handle> ; # NOTE: 'my' removed t +o ensure scope. close( $data_file_handle ); } chomp( @data ); open( my $out_file_handle,'>', $process_file ) or die("Can't open file: $process_file"); # Modified as above. foreach my $line_from_file ( @data ) { # Chang +ed from for to foreach removed dependence on $_ my @field = split( /\:/, $line_from_file ); # next if( ( $field[0] eq $delroadName ) && ( $field[1] == $delroa +dNumber ) ); # Added brackets and changed $field[0] in both to [0] and [1] next if( ( $field[0] eq $delroadName ) && ( $field[1] == $delroadN +umber ) ); print $out_file_handle $line_from_file, "\n"; # Modified t +o print to file handle - Else the file will be truncated # Since you are open + is with '>' and will remain blank. } # close $output; close $process_file; # You want to close the file handle, not the + string. }

    Update: Corrected stupid mistake as pointed out by tobyink below.

      Thanks for all of the information and the revised code worked it did delete the entries in the file and when i opened the data.txt file here was the input:



        print $out_file_handle $line_from_file, "\n";

        You're currently printing out the variable $_ which isn't being defined by any of the code you posted (but is being defined somewhere else to point to an object of some kind).

        package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1019006]
Approved by Athanasius
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2014-09-15 02:06 GMT
Find Nodes?
    Voting Booth?

    My favorite cookbook is:

    Results (145 votes), past polls