Click to See Complete Forum and Search --> : smallest to largest


BassMasterFlash
04-28-2004, 05:32 AM
Alright so I have a directory full of text files that are actually dates , like this
20040201.txt
20051204.txt
20030425.txt
20041225.txt << (christmas 2004)

so what I'm trying to do is get all this info into the perl script in order from smallest number to largest number so that my dates will read in in order. like this...
20030425.txt
20040201.txt
20041225.txt
20051204.txt

and as they load in i will add a "<" to the beginning and a "/>" to the end to give it an XML style. So i'm not too far into the script but i do have this...

#!/usr/bin/perl

$dirtoget="/home/virtual/mydomain.com/var/www/html/shows/";
opendir(SHOWS, $dirtoget) || die("Cannot open directory");
@showdates= readdir(SHOWS);
closedir(SHOWS);

print "Content-type: text/html\n\n";
print "dates=";
foreach $f (@showdates)
{
unless ( ($f eq ".") || ($f eq "..") )
{
print "<$f/>";
}
}

__________________________________
basically this would just print out ..
dates=<20040201.txt/><20040201.txt/><20030425.txt/><20041225.txt/>

So is there a way to get these dates into order here?

Any Questions , Please Let Me Know,
thanks in advance
Trey

silent11
04-28-2004, 07:00 AM
@showdates= readdir(SHOWS);
@showdates = sort(@showdates);

silent11
04-28-2004, 07:05 AM
I actually have a similar web app that spits out show dates.. here it is..


#!/usr/bin/perl

my (@next_shows,$right_now,@t);
print "Content-type: text/html\n\n";


@t = localtime();
$right_now = sprintf ("%04d%02d%02d" , $t[5]+1900, $t[4]+1, $t[3]);


opendir( DIR, '.' ) || print "can't opendir : $!";
@next_shows = grep { $_ >= $right_now -1 } readdir(DIR);
closedir DIR;
@next_shows = sort(@next_shows);

for (@next_shows) {
print ' &bull; ';
open (CRAP,"<$_") || print $!;
while (<CRAP>) { print; }
print "<br>\n\n";
}

print "No shows currently scheduled" unless @next_shows;


It spits output in html, not XML but it can be eaisly adjusted.

good luck.

BassMasterFlash
04-28-2004, 01:18 PM
Wow.......that was way easier than I thought is was going to be, thanks alot silent11, i put the @showdates = sort(@showdates); in there, and it all came in in order..i'm still checking out your code to see whats up with it, cause i do need to make sure my current dates are current and not past,
thanks again
Trey

silent11
04-28-2004, 01:22 PM
No sweat :)

BassMasterFlash
04-28-2004, 01:47 PM
actually, i need to get a better understanding for this, can you give me a quick explanation for these lines....

@t = localtime();
$right_now = sprintf ("%04d%02d%02d" , $t[5]+1900, $t[4]+1, $t[3]);
________________
and
________________
@next_shows = grep { $_ >= $right_now -1 } readdir(DIR);
________________
i assume that @t just gets the localtime, but what about the other two

thanks again
Trey

silent11
04-28-2004, 02:02 PM
Explaining the localtime() and sprintf() functions would take a whole lotta time. you can read up on them here:

localtime() (http://www.perldoc.com/perl5.8.0/pod/func/localtime.html)

sprintf() (http://www.perldoc.com/perl5.8.0/pod/func/sprintf.html)

Basicall, localtime() (http://www.perldoc.com/perl5.8.0/pod/func/localtime.html) returns an array, @t in our example. sprintf() (http://www.perldoc.com/perl5.8.0/pod/func/sprintf.html) then formats the elements in @t to match our text file formatting yyyymmdd.

in the case of things like "$t[4]+1" we are first adding 1 to the 5th element (indexes start at 0) which is the month. We add 1 because localtime starts it's indexes at 0 too, so Jan would be 0. Adding the 1 turns zero to 1.

sprintf() (http://www.perldoc.com/perl5.8.0/pod/func/sprintf.html) will format our 1 to look like 01.

%02d says, "padd this thing up to two spaces with zeros if needed".

string year, month and day together and we get:

$right_now = sprintf ("%04d%02d%02d" , $t[5]+1900, $t[4]+1, $t[3]);


_________

now for this one:

@next_shows = grep { $_ >= $right_now -1 } readdir(DIR);


grep() (http://www.perldoc.com/perl5.8.0/pod/func/grep.html) takes an array and filters out any elements in that array that we don't want.

so, working from the right to left,
@next_shows = grep { $_ >= $right_now -1 } readdir(DIR);
says: "read the directory. now, is the element (or file name in our case) greater than or equal to yesterday's date ($right_now - 1) in yyyymmdd format? if it is, then stuff it in @next_shows."

Have you picked up a copy of Learning Perl (http://www.amazon.com/exec/obidos/tg/detail/-/0596001320/silent11-20/104-8671344-5332731) ? You will want this if you are learning perl.

BassMasterFlash
04-28-2004, 02:22 PM
i dont have that paticular book, but i have pulled out one that i bought a while back, it doesnt really go into great detail like i need to...for instance , you explaining the sprintf function for some reason clicked a little easier with me.......ah, i dont know, but thanks for the lesson, i really absolutely appreciate the feedback.

thanks

silent11
04-28-2004, 02:28 PM
i really absolutely appreciate the feedback.

maybe you can get me on the guest list for when your band comes to Houston and plays at Fitz. That would be cool :)

BassMasterFlash
04-28-2004, 02:31 PM
one other thing, when ever i'm comparing these show.......
take for instance this line

@next_shows = grep { $_ >= $right_now -1 } readdir(DIR);
___________

ok @next_shows is going to be a .txt file right?, so @next_shows will actually be something like 20041204.txt..

and $rightnow is going to be something like 20040427

now is the extension on @next_shows going to affect anything when comparing @next_shows and $rightnow?

BassMasterFlash
04-28-2004, 02:33 PM
wow, fitz, you are a little close to home.........no problem

silent11
04-28-2004, 02:42 PM
so @next_shows will actually be something like 20041204.txt..

yes. it will. you can either drop the file extension from your files or temporarily drop the .txt extension from the array elements with a map{}.

here is some psudo code:

@file_list = readdir(DIR); #or @file_list = <*.txt>;

#take .txt off of each element / file name
@no_extension = map {s/\.txt$//} @file_list;

# weed out old dates
@next_shows = grep { $_ >= $right_now -1 } @no_extension;

#.. do stuff....

#re-add .txt to each element in @next_shows if you want with
@next_shows = map {$_ . '.txt'} @next_shows;

BassMasterFlash
04-28-2004, 03:22 PM
Im getting back nothing but "dates=" i have one file that i know is greater than the current date. See if you can find an error in this....

#!/usr/bin/perl

$dirtoget="/home/virtual/mydomain.com/var/www/html/shows/";
@t = localtime();
$right_now = sprintf ("%04d%02d%02d" , $t[5]+1900, $t[4]+1, $t[3]);
opendir(SHOWS, $dirtoget) || die("Cannot open directory");
@files= readdir(SHOWS);
@no_extension = map{s/\.txt$//}@files;
@showdates = grep { $_ >= $right_now -1 }@no_extension;
closedir(SHOWS);
@showdates = sort(@showdates);
@showdates = map{$_.'.txt'}@showdates;
print "Content-type: text/html\n\n";
print "dates=";
foreach $f (@showdates)
{
unless ( ($f eq ".") || ($f eq "..") )
{
print "<$f/>";
}
}

BassMasterFlash
04-28-2004, 04:31 PM
I am getting a value of '1' for each @no_extension....i should be getting something like 20041230 right?

BassMasterFlash
04-29-2004, 03:17 AM
a little confusion, but it works now, thanks alot
your on the guest list silent11
trey :D