Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Update works for line...deletes other records

by PilotinControl (Scribe)
on Feb 21, 2013 at 04:15 UTC ( #1019881=perlquestion: print w/ replies, xml ) Need Help??
PilotinControl has asked for the wisdom of the Perl Monks concerning the following question:

my code below works fine updating the line in a file however it deletes all other records...not sure why? Thanks in advance for the solution to the code below.

sub editgenord { print colored ("| ORIGINAL DATE: |\n\n",'green'); my $origgenordDate = <STDIN>; chomp $origgenordDate; print colored ("| REVISED DATE: |\n\n",'green'); my $revgenordDate = <STDIN>; chomp $revgenordDate; my $file = "generalorderdata.txt"; local $^I = ".bak"; local @ARGV = ($file); while (<>) { chomp; my ($origDate,$gnOrd,$revDate) = split(/\:/); if ($origDate eq $origgenordDate) { print "$origDate:$gnOrd:$revgenordDate\n"; } } unlink("$file.bak"); close $file; print colored ("GENERAL ORDER UPDATED SUCCESSFULLY!!\n",'green'); sleep 3; genord(); } # END UPDATE GENERAL ORDER

Comment on Update works for line...deletes other records
Download Code
Re: Update works for line...deletes other records
by Anonymous Monk on Feb 21, 2013 at 04:53 UTC
Re: Update works for line...deletes other records
by tmharish (Friar) on Feb 21, 2013 at 06:16 UTC
Re: Update works for line...deletes other records
by 7stud (Deacon) on Feb 21, 2013 at 07:52 UTC

    From 'man perlrun':

     -i[extension]
                specifies that files processed by the "<>" construct are to be
                edited in-place.  It does this by renaming the input file, opening
                the output file by the original name, and selecting that output
                file as the default for print() statements. 
    

    Okay, so once perl renames the original file, the original file name doesn't exist anymore. Subsequently, when perl opens a file name that doesn't exist for writing, what's in that file to start?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (12)
As of 2014-07-28 15:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (202 votes), past polls