FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Debian > Debian User

 
 
LinkBack Thread Tools
 
Old 01-10-2008, 02:04 AM
ISHWAR RATTAN
 
Default Perl realted question..

Is there a way to place the last line read
when reading from a file? My suspicion is there
is no such thing but i do want to confirm..

-ishwar


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 01-10-2008, 03:17 AM
"Chris Howie"
 
Default Perl realted question..

On Jan 9, 2008 10:04 PM, ISHWAR RATTAN <rattan@cps.cmich.edu> wrote:

Is there a way to place the last line read
when reading from a file? My suspicion is there
is no such thing but i do want to confirm..

What do you mean by "place?"

--
Chris Howie
http://www.chrishowie.com
http://en.wikipedia.org/wiki/User:Crazycomputers
 
Old 01-10-2008, 04:45 AM
Sam
 
Default Perl realted question..

if i read you correctly, you can read the file into an array and use pop, which will return the last element read.Or you could use @array[-1]



On 1/9/08,
Chris Howie <cdhowie@gmail.com> wrote:
On Jan 9, 2008 10:04 PM, ISHWAR RATTAN <rattan@cps.cmich.edu> wrote:


Is there a way to place the last line read
when reading from a file? My suspicion is there
is no such thing but i do want to confirm..

What do you mean by "place?"


--
Chris Howie
http://www.chrishowie.com

http://en.wikipedia.org/wiki/User:Crazycomputers
 
Old 01-10-2008, 05:34 AM
Dave Sherohman
 
Default Perl realted question..

On Wed, Jan 09, 2008 at 09:45:15PM -0800, Sam wrote:
> if i read you correctly, you can read the file into an array and use pop,
> which will return the last element read.Or you could use @array[-1]

That's rather wasteful of memory, which becomes a concern with larger
files. If the objective is indeed to get the last line in the file,
then you only need a scalar to stick each line into and its final stored
value will be the last line:

#!/usr/bin/perl

use strict;
use warnings;

my $last;

while (<>) {
$last = $_;
chomp $last;
}
print $last, "
";

Of course, if this is being done as a standalone operation, then just
using `tail -1 filename` on the command line would definitely be easier
and probably more efficient.


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 01-10-2008, 09:03 PM
Scott Gifford
 
Default Perl realted question..

Dave Sherohman <dave@sherohman.org> writes:

> On Wed, Jan 09, 2008 at 09:45:15PM -0800, Sam wrote:
>> if i read you correctly, you can read the file into an array and use pop,
>> which will return the last element read.Or you could use @array[-1]
>
> That's rather wasteful of memory, which becomes a concern with larger
> files. If the objective is indeed to get the last line in the file,
> then you only need a scalar to stick each line into and its final stored
> value will be the last line:

Or use File::ReadBackwards, which will generally do this in a much
more efficient way. It's in Debian package libfile-readbackwards-perl.

----Scott.


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 01-10-2008, 09:47 PM
Ken Irving
 
Default Perl realted question..

On Wed, Jan 09, 2008 at 10:04:19PM -0500, ISHWAR RATTAN wrote:
>
> Is there a way to place the last line read
> when reading from a file? My suspicion is there
> is no such thing but i do want to confirm..

Just another guess at what you're asking about; perhaps the word
"placed" was intended to be "replaced"; i.e., can you replace a
line that you've just read?

I think the general answer to that is "no", and the approach I've
always taken (using Perl) is to write a new file, copying the old
and adding new content, then rename and remove etc. when done. I
haven't delved into the internals much, though, and maybe there's
a way it could be done, but it hasn't been worth my time to do it.

If you think about a file stored on disk, text files have an arbitrary
structure defined by the end-of-line character(s). It seems like
it should be possible to swap out characters in a line, but the line
size would need to stay the same, perhaps using padding with spaces
or something. Probably much more reasonably and conventionally doable
with fixed-sized records, but if the need was there I don't see why
it shouldn't be possible.

Ken

--
Ken Irving, fnkci+debianuser@uaf.edu


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 01-13-2008, 03:41 AM
Celejar
 
Default Perl realted question..

On Thu, 10 Jan 2008 13:47:41 -0900
Ken Irving <fnkci@uaf.edu> wrote:

> On Wed, Jan 09, 2008 at 10:04:19PM -0500, ISHWAR RATTAN wrote:
> >
> > Is there a way to place the last line read
> > when reading from a file? My suspicion is there
> > is no such thing but i do want to confirm..
>
> Just another guess at what you're asking about; perhaps the word
> "placed" was intended to be "replaced"; i.e., can you replace a
> line that you've just read?
>
> I think the general answer to that is "no", and the approach I've
> always taken (using Perl) is to write a new file, copying the old
> and adding new content, then rename and remove etc. when done. I
> haven't delved into the internals much, though, and maybe there's
> a way it could be done, but it hasn't been worth my time to do it.
> If you think about a file stored on disk, text files have an arbitrary
> structure defined by the end-of-line character(s). It seems like
> it should be possible to swap out characters in a line, but the line
> size would need to stay the same, perhaps using padding with spaces
> or something. Probably much more reasonably and conventionally doable
> with fixed-sized records, but if the need was there I don't see why
> it shouldn't be possible.

It is actually apparently quite simple (I've never tried it); from 'man
perlfaq5':

> How do I change one line in a file/delete a line in a file/insert a
> line in the middle of a file/append to the beginning of a file?
>
> Use the Tie::File module, which is included in the standard
> distribution since Perl 5.8.0.

BTW, the perl-beginners ML is a terrific place for perl help. It's
read by some really helpful perl gurus who can and will answer pretty
much any question you might have about perl. Just be aware that they
will call you on not RTFM ...

> Ken

Celejar
--
mailmin.sourceforge.net - remote access via secure (OpenPGP) email
ssuds.sourceforge.net - A Simple Sudoku Solver and Generator


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 

Thread Tools




All times are GMT. The time now is 09:47 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org