Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.


In reply to Re: Deletes All Records not just one by tmharish
in thread Deletes All Records not just one by PilotinControl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-24 18:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found