[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: 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


Reply to: