Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Update works for line...deletes other records

by PilotinControl (Pilgrim)
on Feb 21, 2013 at 04:15 UTC ( [id://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

Replies are listed 'Best First'.
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
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-03-29 05:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found