Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Take 10 last lines from a txt file and print them? 2

Status
Not open for further replies.

ETbyrne

Programmer
Dec 27, 2006
59
US
Let's say I have a file called links.txt with the below contents.

Code:
<a hre="joe.html">Joe</a><br>
<a hre="herb.html">Herb</a><br>
<a hre="Kim.html">Kim</a><br>
<a hre="June.html">June</a><br>
<a hre="jack.html">Jack</a><br>
... and so on and so fourth...

Let's say I want to grab the 10 last lines and print them to screen. How would I do that? Is it possible?

_______________________________________

You don't know me.
 
First... everything is possible (learn that... I'm pretty sure it's the perl motto :) ).

Maybe something like
open(FILE, "<links.txt");
@file = <FILE>;
chomp @file;
close FILE;

then you can either one at a time do
print "$file[-10]\n";
print "$file[-9]\n";
... and so on
or maybe something like
for (-10..-1) {
print "$file[$_]\n";
}
 
File::Tail would be worth a look as well

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
I'll try those. Thanks guys! I'll post if I have any problems.

Lol, I'll remember the Perl motto. ^_^

_______________________________________

You don't know me.
 
or Tie::File which allows files to be read/written too pretty much like a normal array. Has the advantage of not reading whole files into memory.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Ok, the sorting scrpt above works good. But, I have another problem.

I created a script that you would be a home page for a website, but it doesn't work. Could you tell me what is wrong with this script? Remember, it is supposed to print an html page.

Location:
Code:
#!/usr/bin/perl -w 

print "<html><head><title>Portal Home</title></head>\n";
print "<body bgcolor=\"grey\">\n";
print "<h2>Welcome!</h2>\n";
print "<hr>\n";
print "<b>10 newest portals</b><br>\n";

open(FILE, "<links.txt");
@file = <FILE>;
chomp @file;
close FILE;

print "$file[-1]\n";
print "$file[-2]\n";
print "$file[-3]\n";
print "$file[-4]\n";
print "$file[-5]\n";
print "$file[-6]\n";
print "$file[-7]\n";
print "$file[-8]\n";
print "$file[-9]\n";
print "$file[-10]\n";

print "<hr>\n";

print "</body></html>\n";

_______________________________________

You don't know me.
 
Actualy the link is
sorry about that. And I just wanted to clarify that the script is the home page and is supposed to print html when the script is loaded.

If you can't do that is it possible to activate a script when you load a webpage?

_______________________________________

You don't know me.
 
missing the http header, see bolded line below:

Code:
#!/usr/bin/perl -w

[b]print "Content-type: text/html\n\n";[/b]
print "<html><head><title>Portal Home</title></head>\n";
[the rest of your code here]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
also, keep in mind that \n has no affect on the display in the browser, so if you want each link on it's own line add a <br> tag to the html markup. If not, never mind.... [smile]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks KevinADC for the post! I know that the \n doesn't change anything in the browser, I just put some of them on multiple lines for readability.

_______________________________________

You don't know me.
 
I just tried the script and it prints the page out fine, but it isn't printing the last ten lines of links.txt anymore.

_______________________________________

You don't know me.
 
try like this:

Code:
[gray]#!/usr/bin/perl -w[/gray]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Content-type: text/html[purple][b]\n[/b][/purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[black][b]print[/b][/black] [red]"[/red][purple]<html><head><title>Portal Home</title></head>[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[black][b]print[/b][/black] [red]"[/red][purple]<body bgcolor=[purple][b]\"[/b][/purple]grey[purple][b]\"[/b][/purple]>[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[black][b]print[/b][/black] [red]"[/red][purple]<h2>Welcome!</h2>[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[black][b]print[/b][/black] [red]"[/red][purple]<hr>[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[black][b]print[/b][/black] [red]"[/red][purple]<b>10 newest portals</b><br>[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red]FILE, [red]"[/red][purple]<links.txt[/purple][red]"[/red][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't open links.txt: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[blue]@file[/blue] = <FILE>[red];[/red]
[blue]@file[/blue] = [url=http://perldoc.perl.org/functions/reverse.html][black][b]reverse[/b][/black][/url][red]([/red][blue]@file[/blue][red])[/red][red];[/red]
[url=http://perldoc.perl.org/functions/close.html][black][b]close[/b][/black][/url] FILE[red];[/red]
[black][b]print[/b][/black] [red]"[/red][purple][blue]$file[/blue][[blue]$_[/blue]]<br>[/purple][red]"[/red] [olive][b]for[/b][/olive] [red]([/red][fuchsia]0..9[/fuchsia][red])[/red][red];[/red]
[black][b]print[/b][/black] [red]"[/red][purple]<hr>[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[black][b]print[/b][/black] [red]"[/red][purple]</body></html>[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Still doesn't work. It'll print out everything above the line
Code:
open(FILE, "<links.txt") or die "Can't open links.txt: $!";

everything else won't print on-line, but it works 100% from the command line.

_______________________________________

You don't know me.
 
no error message?

Can't open links.txt: blah blah blah

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
No errors or anything. I'll try re-uploading links.txt.

_______________________________________

You don't know me.
 
Nope, still doesn't work. Tried it on command promt and it works fine though. How do I run a chmod a+r on it?

Side Note: I have a book on CGI with Perl coming in the mail, but I do know Javascript and some C, C++ and C#.

_______________________________________

You don't know me.
 
my guess is the links.txt file exists but is blank. Running from the command line might be accessing a different links.txt file so it looks like it works while the other doesn't, try this:

Code:
#!/usr/bin/perl -w

print "Content-type: text/html\n\n";
print "<html><head><title>Portal Home</title></head>\n";
print "<body bgcolor=\"grey\">\n";
print "<h2>Welcome!</h2>\n";
print "<hr>\n";
print "<b>10 newest portals</b><br>\n";

open(FILE, "<links.txt") or die "Can't open links.txt: $!";
@file = <FILE>;
@file = reverse(@file);
close FILE;
print "*** @file ***<br>\n";
print "\@file is empty<br>\n" if (scalar @file == 0);
print "$file[$_]<br>" for (0..9);
print "<hr>\n";
print "</body></html>\n";


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
are you running it in the same directory, ie does links.txt exist in the same location as your script on the server?

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
You might have to do this since you're not using Cgi::Carp

Code:
open(FILE, "<links.txt") or die [b]print[/b]"Can't open links.txt: $!";



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top