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.
 
I checked links.txt and it is the same as the one I'm testing with, and it's in the same directory (come on, I'm not that stupid). I'll try your ideas out KevinADC.

Side note: Got my CGI with Perl book today! :)

_______________________________________

You don't know me.
 
regardless of how !stupid you think you are, there's always someone else, and thankfully, I can't remember the url[sadeyes]

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Lol, I don't think I'm stupid, I was just kidding. Anyways, Keven, your idea didn't work. :(

Could someone try this on their host and see if it works? I can't think what could be wrong! x_x

_______________________________________

You don't know me.
 
It works. If you can check the server error logs.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
So, what should I do?

_______________________________________

You don't know me.
 
What I don't see is why you don't use the Unix command tail...

Just say
Code:
> tail -10

Or am I missing something here..?!?
 
Or well, that would be

Code:
> tail -10 links.txt

of course.

[btw]
Isn't it possible to edit posts..?!?
[/btw]
 
Isn't it possible to edit posts..?!?

No, you need to use the Preview Post/Edit Post options to make sure your post is good to go before submitting.

Tail is fine for unix, but not everyone isusing unix, also with perl we generally stick to perl solutions unless there is no other choice.



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
There is a tail on my Windows system as well (in C:/Program Files/Windows Resource Kits/Tools/tail) and you have it on Mac OS X as well, but that's Unix again of course.

also with perl we generally stick to perl solutions unless there is no other choice

Well, I doubt if you really mean that?!?
But anyway, all I was trying to say is that it looked a bit like inventing the wheel again. At least to me...
 
Well, I doubt if you really mean that?!?

Sure I do, this is the perl forum after all. Perl reinvents many things the OS does. In this case I doubt tail would help. There is something else going on because they can't read the file forward, backwards or side-ways and get it to display.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
OK, whatever.
What I just meant is that if you are doing something that really needs, let's say, a database, you would be well advised to do that, and not try to do it in Perl, even if you are on a Perl forum.
But enough about it. We quite agree I think...

And nonetheless it is pretty strange that the script runs fine in command line mode, but not in cgi mode.
My assumption would also be that it has something to do with permissions.
So I would try to see what $! says, like KevinADC suggests, and also I am quite curious what the server's error log is saying.

Apart from that the original script first chomps the entire file, and prints newlines after the last 10 lines again. Maybe you could skip that?!?

Code:
open(FILE, "<links.txt") or die "Couldn't open links.txt: $!\n";
my @file = <FILE>;
close FILE;

print splice(@file, -10);

And the script does work on my server, so there has to be something else preventing your page form executing right, I would say...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top