Forum Moderators: coopster & phranque

Message Too Old, No Replies

(Perl) delete/expire lines in data file

(Perl) delete/expire lines in data file

         

StopSpam

12:31 am on Jul 6, 2003 (gmt 0)

10+ Year Member



(Perl) delete expire data in file

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

netcommr

1:24 am on Jul 6, 2003 (gmt 0)

10+ Year Member




The WebmasterWorld community wishes not to do work for visitors, but instead try and help those learn how to do work for themselves. I think this is a good policy and helped me to learn more myself. So I will give ya a few good sources which help me a great deal.

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

netcommr

2:10 am on Jul 6, 2003 (gmt 0)

10+ Year Member




also perlfaq [theoryx5.uwinnipeg.ca] docs

StopSpam

10:59 am on Jul 7, 2003 (gmt 0)

10+ Year Member



My apoligize...
Sorry if you felt that i ask you to do all work ...
Thats not what i want. ... As i then wouldnot learn anything ...
perl coding aint easy for me and i just need advise once a while and i really apriciate all support i find on this forum... i learned a lot here ...
idont ask you to write all coding for me ...

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

StopSpam

2:22 pm on Jul 7, 2003 (gmt 0)

10+ Year Member



Well to make you all happy

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 ...

sugarkane

2:37 pm on Jul 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Certainly wasn't a 'dork' question StopSpam!

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 ;)

StopSpam

3:02 pm on Jul 7, 2003 (gmt 0)

10+ Year Member



thanks ;-) i allready found a answer to question i posted in the o,reilly cook book....

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.

jatar_k

4:46 pm on Jul 7, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Those are great books StopSpam,

I actually don't have the cookbook but I have the pocket reference and "PERL in a nutshell" which is really good as well. Strangely even though I am sooooo proficient in perl these books help a lot. ;)

StopSpam

11:01 pm on Jul 7, 2003 (gmt 0)

10+ Year Member



Ye si agree these books are great ...

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 ...

netcommr

6:06 am on Jul 8, 2003 (gmt 0)

10+ Year Member




did not mean for you to feel like you over stepped any bounds here, but your on the right track!

have fun :)