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: Intermediate:
Ordering in Perl

 



edesign
Deleted

Mar 4, 2000, 3:28 PM

Post #1 of 6 (798 views)
Ordering in Perl Can't Post

Hello! I am currently working on a perl script that takes text files in a directory and pastes them into a webpage. This allows me to easily update the news on my site. I was able to get everything to work, but because it is news, I need the text files to be pasted onto the webpage in order of newest to latest and I cannot figure out how to do it. I currently name the text files in the following format, where 00-00-00 is the date:

00-00-00_File_Name_Here.txt

Could I use the titles to make the script order them? And if not, how else could I do it? Please look at what I already have of the script below, and let me know. Thanks!

-Daniel


#!/usr/bin/perl

$newsdir = '/home/ebeanies/public_html/cgi-bin/news';

print "Content-type: text/html\n\n";

opendir THEDIR, "$newsdir" | | die "Unable to open the news!";
@allfiles = readdir THEDIR;
closedir THEDIR;
foreach $file (@allfiles) {
if ("$newsdir/$file" =~ /\.txt/) {
open (newsdir, "<$newsdir/$file");
@newsdir=<newsdir>;
close(newsdir);
foreach $newsdir (@newsdir) {
print "$newsdir\n";
}
print qq~
<!-- Line -->
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="#ffffff" height="5"><table border="0" cellpadding="0" cellspacing="0"><tr><td></td></tr></table>
</td>
</tr>
<tr>
<td bgcolor="#000000" height="1"><table border="0" cellpadding="0" cellspacing="0"><tr><td></td></tr></table>
</td>
</tr>
<tr>
<td bgcolor="#ffffff" height="5"><table border="0" cellpadding="0" cellspacing="0"><tr><td></td></tr></table>
</td>
</tr>
</table>
<!-- End Line --> ~;
}
}


summer
Deleted

Mar 5, 2000, 1:32 AM

Post #2 of 6 (798 views)
Re: Ordering in Perl [In reply to] Can't Post

Daniel,

You can certainly order by filename.

You haven't specified whether you are using
dates in US, European or Japanese order.

Assuming that all the numbers are exactly 2 digits the following approaches will work:

If you are using Japanese year-month-day you can use ASCII ordering, e.g.

foreach $file ( sort @allfiles ) {

If you are using US month-day-year or European day-month-year then you'll have to
get each part of the date and recombine them in an orderable form; here's an example using US month-day-year:

foreach $file ( sort {
my( $amon, $aday, $ayear ) =
$a =~ /^(\d\d)-(\d\d)-(\d\d)/o ;
my( $bmon, $bday, $byear ) =
$b =~ /^(\d\d)-(\d\d)-(\d\d)/o ;
"$ayear$amon$aday" cmp "$byear$bmon$bday"
} @allfiles ) {

I've used ASCII rather than numeric comparison because I would expect it to be faster.


edesign
Deleted

Mar 5, 2000, 7:48 AM

Post #3 of 6 (798 views)
Re: Ordering in Perl [In reply to] Can't Post

Summer,

I am using the US Month-Day-Year form in my script, so each file name looks like this:

Month-Day-Year_File_Name_Here.txt

I think he reason I cannot get it to work is because I add the title of the article onto the date. Would it be possible for the script to only ready the first 8 characters? Let me know. Thanks!

-Daniel

[This message has been edited by edesign (edited 03-05-2000).]


edesign
Deleted

Mar 5, 2000, 12:23 PM

Post #4 of 6 (798 views)
Re: Ordering in Perl [In reply to] Can't Post

Summer,

I got it to work! All I did was use a simple sorting function and twisted it around so it would display the newsest dates first, like so:

foreach $file (sort { int($b) <=> int($a) } @allfiles) {

Thanks for the help!

-Daniel


summer
Deleted

Mar 5, 2000, 2:12 PM

Post #5 of 6 (798 views)
Re: Ordering in Perl [In reply to] Can't Post

Daniel,

I'm surprised that int() solves the problem!

By using int() on mm-dd-yy_filename.txt I would expect perl to just compare months
since int() will tell it to read digits until
it gets a non-digit and return that as its result.

For testing I'd have files (using US date format) called:
03-15-02_first.txt
02-10-01_second.txt
01-05-00_third.txt
etc.

I'd still suggest that the easiest solution is to use YYYYMMDD-filename.txt format; it will work for all years and you can compare without even bothering with int().


edesign
Deleted

Mar 5, 2000, 5:42 PM

Post #6 of 6 (798 views)
Re: Ordering in Perl [In reply to] Can't Post

Summer,

I tried the script out with two different files per month and it seems to be working fine, although I think I am going to switch to the YYYYMMDD_File_Name_Here.txt just to be safe. While we are on the subject, I have another question. In each of my .txt files, I just write each news article in html because many of them use tables, etc. Is it possible to use a chomp feature so each file could look like this:

Article Title
Article Date
Source
Everything below this point is the article

Does that make any sense? Let me know. Thanks!


 
 


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

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