CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Data file help

 



daniel
Deleted

Jun 19, 2000, 1:12 PM

Post #1 of 4 (661 views)
Data file help Can't Post

Hello. I am trying to create a script that will search through a text file and display all of the lines that are with x amount of days of the current date. My text file looks like this:

date|title
07042000|Fourth of July
07072000|Cody's Birthday

So, the script will open the file, look at the dates, and if any of then are within 7 days of the current date, print them in order. I have never used a database in this form, so if anyone can help me, please do!

Daniel


Kanji
User / Moderator

Jun 19, 2000, 4:07 PM

Post #2 of 4 (661 views)
Re: Data file help [In reply to] Can't Post

Because of the way you store your dates, this sort of thing is best done with the aide of a date manipulation module like Date::Calc ...
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

#!/usr/bin/perl -w

use strict;
use Date::Calc qw( Add_Delta_Days Today );

my %wanted;

foreach my $days ( 0 .. 6 ) {
my $date = sprintf(
"%02d%02d%4d",
( Add_Delta_Days( Today, $days ) )[1,2,0]
);
$wanted{ $date }++;
}

while ( <> ) {
next unless my( $date, $event ) = split /\|/;
print "$date: $event\n" if $wanted{ $date };
}</pre><HR></BLOCKQUOTE>

However, if you stored your dates in the same format spat out by time(), you could simplify that to just ...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

my $today = time();
while ( <> ) {
next unless my( $date, $event ) = split /\|/;
print "$date: $event\n" if
( $date - $today ) >= 0
( $date - $today ) <= 604_800;
}</pre><HR></BLOCKQUOTE>


daniel
Deleted

Jun 19, 2000, 6:09 PM

Post #3 of 4 (661 views)
Re: Data file help [In reply to] Can't Post

Thanks Kanji. I alraedy have the whole date script figured, and it works, so if I just use these lines:

( $date, $event ) = split /\|/; print "$date: $event\n"

Will that split the date and event up in the database? Thanks!

- Daniel

[This message has been edited by daniel (edited 06-19-2000).]


Kanji
User / Moderator

Jun 20, 2000, 5:23 AM

Post #4 of 4 (661 views)
Re: Data file help [In reply to] Can't Post

Yes.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives