Karim BELABAS on Tue, 14 Oct 2003 19:13:53 +0200 (MEST)


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

Re: write in a file


On Tue, 14 Oct 2003, Bill Allombert wrote:

> On Tue, Oct 14, 2003 at 04:52:53PM +0200, Dirk Laurie wrote:
> > Can one read in from a Pari-GP-written file one item at a time?
> > For example, in a loop I use 'write(filename,x)'.  It creates
> > a lot of lines starting with 'x='.
> >
> > If I say 'read(filename)', the whole file is read, and all the
> > values of x except the last are lost.  I want to read one, do
> > something to it, read another, etc.
>
> Try a named fifo an a line filter:
> sh1$ mkfifo fifo
> sh1$ gp
> ? for(i=1,10,print(i,read("fifo"))
>
> in another shell write your data to fifo closing the fifo between each
> line.  For example
>
> sh2$ perl -ne 'system("echo '\''$_'\'' > fifo")' <data
>
> It looks awkward but just use the power of unix.

Yet another silly solution, just \r the file. _Not_ "read": \r acts
as if each line had been individually typed, and fills in all corresponding
history entries  [ make sure all lines are ; terminated to avoid huge
outputs. They still go to history ]

Then you can use eval( Str("%" i) )  provided histsize is large enough.
Unfortunately, you have to provide the start / end indexes yourself.

Even more devious, use:

  last(N) = eval( Str( "%" concat( vector(N,i,"`") ) ) )

to access the (end - N)-th history entry.

Decent handling of file descriptors [ associated to a new t_FILE ] is in the
TODO list.

    Karim.

-- 
Karim Belabas                     Tel: (+33) (0)1 69 15 57 48
Dép. de Mathématiques, Bât. 425   Fax: (+33) (0)1 69 15 60 19
Université Paris-Sud              http://www.math.u-psud.fr/~belabas/
F-91405 Orsay (France)            http://www.parigp-home.de/  [PARI/GP]