i got a data file named data.txt (from a auction script)
and inside you the data file you find this
177¦6¦sport shoes¦12.50
175¦7¦model train¦50
184¦7¦sony keyboard¦150
186¦7¦walk man¦25
186¦7¦bike¦300
The script that writes to the data file works like get time print day and month , product and price.
($sec,$min,$hour,$day,$mon,$year,$wday,$yday,$isdst)=localtime(time);
$mon++;
$day = "$1day";
$day--;
$yday++;
print FILE "$1day¦$mon¦$product¦$price\n";
Now my question is i want to delete auction older then one or 2 a 3 days ..
so i want to write something like;
$today = "$day"; # current day number for sample today is 186
the i must have something like if line in data starts not with $day
then remove line ..
or if line starts with $day then read line in memory delete all others
and reprint all lines starting with $day ...
this way it deletes lines older then today
now i ain't a pro coder in perl just beginning ;-)
can some one show me the code how to do this?
thank you
Perl 'Black Book', from CoriolisOpen Press
Perl Cookbook, from O'Reilly & Associates
CPAN[/url] & Perl Online [google.com]
these have all the answers you need to get a huge start in Perl.
I will give you some advice which you won't find in any language books. NEVER make direct edits to a data TXT file. Create a new file and populate it with the new data(what your keeping), then rename your old 'data.txt' file to 'data.txt.bu' for backup, then rename your new file to 'data.txt'. The reason for doing this is if the system faults in the middle of the operation you have not destroyed your data file.
basically, what you want to do is read your entire data file by line, test each line for your condition, then write lines that match your conditions into your new file on its own line, then do the renaming of your files(original file first so not to overwrite. CHECK FOR ERRORS DURING EACH STEP
sometimes i just need a few lines of sample code so i understand how things work wish allow me to contineu ...
thanks for referering the urls..
till now i only had buy the book cgi101
i went to book store and i have spend some big time cash on 2 books;
O'relly Perl cook book and the O'reilly Perl Pocket Reference book ...
Now i go read and studie and soon i wont be asking dork perl questions any more on this forum ...
i will probally be answerting them in stead of referring to google as some do above ...
I think netcommr summed up the process you'd use:
basically, what you want to do is read your entire data file by line, test each line for your condition, then write lines that match your conditions into your new file on its own line, then do the renaming of your files(original file first so not to overwrite.
There's many ways of actually implementing this in Perl, but generally you'd be best reading up on file handling, arrays and if/then/else structures - the books you chose definitely cover those areas in some detail. Good choice ;)
its worth all cash ...
i even got discount $15 on both books as o.reilly excists 25 year and received a cool nylon carrier bag for the books also from o.reilly with logo
my parents will go nuts when they hear how mush i spend on two books hehehehe but its worth the money i think . it will pay it self back one day ...
with buying these .. people claim these are best book s i probally never have to buy again a perl book.
if ibuy diff book and not happy you must bay later again a book..
so i went straight for best books.
even pro perl programmers working ful tiem coding ..
then for them these books are still very usefull to lookup snippets and tricks ..
cook book is very userfreindly ...
the pocket reference looks unknown to me as most info i must learn first
but i am sure once iknow perl this will be helpfull as well.
i read 10 minutes in this bookand iwrote a nice small script...
see my next post ...
i got one thing to change dont know how ...