View Full Version : CGI - Show text file in reverse
FloridaBecky
04-27-2005, 12:03 AM
I have a script that displays the lines of a delineated text file on a web page.
I would like to display the lines in reverse order.
Does anyone know how to do this?
netroact
04-27-2005, 02:51 AM
Not sure if this is what you are looking for, but this is what I use:
@lines = reverse <DATABASE>;
mlseim
04-27-2005, 03:01 AM
... or post like a dozen of the lines from your file so we
can see what your file looks like.
FloridaBecky
04-28-2005, 01:13 AM
Thanks, netroact and mlseim.
This is just asimple text file for the user to post meesages on her site. The text file looks like this when displayed in a browser:
http://www.twosonfarm.com/cgi-bin/readbb.cgi
the text lines are separated with pipes:
106|Lesson Horse|Whiskey is an ... (I won't print it all here)|March 26, 2005
The user is able to add or delete lines to the text file, but the newest line are displayed last and I would like the newest to appear at the top. At this point, I have to go into the text file and move the new line to the top.
neotract, I'm not sure where to put the code you suggest. Can you help?
simplified version of my code:
#Find database
open (FILE, "bbdata.txt") || die "Can't find database\n";
@indata = <FILE>;
close (FILE);
print code here .....
foreach $i (@indata)
{
chomp($i);
($number,$title,$message,$date) = split (/\|/,$i);
}
netroact
04-28-2005, 02:55 AM
Sorry! I always foget to set instant notification. It's actually kinda funny that the code you posted is almost exactly the same code that I used.
[CODE
#Find database
open (FILE, "bbdata.txt") || die "Can't find database\n";
@indata = reverse <FILE>;
close (FILE);
print code here .....
foreach $i (@indata)
{
chomp($i);
($number,$title,$message,$date) = split (/\|/,$i);
}
[/CODE]
Have fun!
FloridaBecky
04-28-2005, 12:51 PM
Wow! Thanks! That was so simple! It worked first try ... which is rare for me.
I have been knocking myself out for a week trying to solve that this problem.
I sure appreciate your help! If my code is similar to yours, maybe I'm doing something right! :)
netroact
04-29-2005, 12:03 AM
Glad to help. That's what I like about Perl. It's so simple that's sometimes it's fun.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.