Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: update flat file

by dvergin (Monsignor)
on Jan 13, 2003 at 06:28 UTC ( [id://226409]=note: print w/replies, xml ) Need Help??


in reply to update flat file

Indenting is a wonderful tool for making it much easier to see what is happening in a piece of code. It is a standard practice among folks who want to get a quick visual grasp of the over-all flow of their code. Here is your program with indenting added:
#!/usr/bin/perl use strict; use warnings; open FILEB, 'B.txt' or die "B: $!\n"; while (<FILEB>) { my ($id,$add, $expire) = (split /\|/)[5,2,3]; open FILEA, 'A.txt' or die "A: $!\n"; open NEWA, '>A.txt.new' or die "New A: $!\n"; while (<FILEA>) { if (/\|$id\|/) { my @rec = split /\|/; @rec[10, 11] = ($add, $expire): $_ = join ('|', @rec); } print NEWA, $_; } close FILEA; close NEWA: } rename 'A.txt.new', 'A.txt';
Notice how much more clear the logic of your code becomes when formatted in this way?

Moving on... you do not describe what result you are getting (beyond saying "it is not working").

But as you can see, every time through the   while (<FILEB>)   loop you are re-writing a new A.txt.new over the top of the one you wrote on the previous time through the loop. Does that shed any light on the unsatisfactory results you are getting?

There are other issues with this code and you may soon see a full re-write of your program posted here. But I suspect this is the main issue at this stage in your work.

UpdateAcidHawk correctly nails several other specific problems. Much of what he calls attention to should have been evident from the error messages produced by your code. Next time, please say more than just "it is not working".

Replies are listed 'Best First'.
Re: Re: update flat file
by britney (Acolyte) on Jan 14, 2003 at 17:11 UTC
    Hi acidhawk, I really don't know how to get date from B.txt( if they have more than 100 lines..), so i think I will use shell cript to grep today date in add field and dump it in a temp file like B2.txt. Then use this perl program to look for all the $id in B2.txt and search in A.txt then replace adddate and expiredate. I am new and still need to learn from all of you. I found my error in not to use : (mistyping ;). Thanks for your help,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://226409]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-03-28 20:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found