And here's a version of Xiao-Yong Jin's implementation that cleans up
after itself (and checks for errors (though that might be overkill):
cfgets=:3 :0
y=. {.y,999 NB. max line length
libc=. (' ',~unxlib'c') 2 :'(m,n)&cd'
fd=. 'dup >i i'libc 0
if. 0>fd do.
echo 'dup error: ',1{::cderx''
else.
f=. {. 'fdopen * i *c'libc fd;,'r'
if. 0=f do.
echo 'fdopen error: ',1{::cderx''
else.
buf=. mema y
if. 0=buf do.
echo 'mema error'
else.
if. 0='fgets *c *c i *' libc (<buf);y;<f do.
echo 'fgets error: ',1{::cderx''
else.
r=. ({.~ i.&LF)memr buf,0,y
if. memf buf do.
echo 'memf error'
else.
if. 'fclose >i *'libc <f do.
echo 'fclose error: ',1{::cderx''
if. 'close >i i'libc <fd do.
echo 'close error: ',1{::cderx''
end.
end.
end.
r
end.
end.
end.
end.
)
Only good for jconsole, but might be useful there.
Also, I've not tested this under windows. It might ({.~ <./@i.&CRLF)
on the r=. line, or maybe something more than that for windows.
Thanks,
--
RaulOn Thu, Jun 21, 2018 at 11:16 AM Raul Miller <[email protected]> wrote:
>
> Ah, never mind - this works fine in jconsole:
>
> l=.({.~i.&LF)memr b,0,s[(c,' fgets *c *c i *')cd(<b=.mema
> s);(s=.256);<f=.{.(c,' fdopen * i *c')cd(d=.0 cd~' dup > i
> i',~c=.unxlib'c');1 1$'r'
>
> (Though repeated use should split that between setup and the actual
> line read, and of course, you would want a larger value for s if you
> wanted to read longer lines.)
>
> Thanks,
>
> --
> Raul
>
> On Thu, Jun 21, 2018 at 11:02 AM Raul Miller <[email protected]> wrote:
> >
> > Oddly, that doesn't work on my machine.
> >
> > It looks plausible, though.
> >
> > Thanks,
> >
> > --
> > Raul
> > On Thu, Jun 21, 2018 at 3:09 AM Xiao-Yong Jin <[email protected]> wrote:
> > >
> > > You can also call c library functions.
> > >
> > > l=.({.~i.&LF)memr b,0,s[(c,' fgets *c *c i *')cd(<b=.mema
> > > s);(s=.256);<f=.{.(c,' fdopen * i *c')cd(d=.0 cd~' dup > i
> > > i',~c=.unxlib'c');1 1$'r'
> > > hello read line
> > > (;$)l
> > > ┌───────────────┬──┐
> > > │hello read line│15│
> > > └───────────────┴──┘
> > > (c,' fclose i *')cd<f
> > >
> > >
> > > > On Jun 19, 2018, at 8:13 PM, Raul Miller <[email protected]> wrote:
> > > >
> > > > Actually, thinking about this - 1!:1 reads the whole file, so it's
> > > > stty won't make a difference .- you'd need to use control-D to
> > > > terminate everything you send to it interactively.
> > > >
> > > > One approach - not incredibly efficient, but interactive
> > > > line-at-a-time input isn't about efficiency, might be:
> > > >
> > > > LINE=: 2!:0'read line; echo "$line"'
> > > >
> > > > This relies on 2!:0 being handled by /bin/sh (and note that you'll get
> > > > a trailing LF character when you do this). The double quotes are so
> > > > that redundant whitespace isn't removed by the shell...
> > > >
> > > > Or, if you want that as a verb:
> > > >
> > > > readline=: 2!:0 bind 'read line; echo "$line"'
> > > >
> > > > I hope this helps.
> > > >
> > > > --
> > > > Raul
> > > > On Tue, Jun 19, 2018 at 8:57 PM Raul Miller <[email protected]>
> > > > wrote:
> > > >>
> > > >> Ah.. experimenting with this:
> > > >>
> > > >> stdin'' reads till end of file (hit control-D to finish input)
> > > >> readln'' as defined in this thread returns immediately, reading an
> > > >> empty string.
> > > >>
> > > >> At least on *my* system.
> > > >>
> > > >> I tried running
> > > >>
> > > >> stty eol ^J before starting J, thinking that would fix it, but it did
> > > >> not. But I'm running OSX where it might be that there's some "new and
> > > >> improved" mechanism for defining the end of line character.
> > > >>
> > > >> Anyways, this is likely an OS issue and the solution probably will
> > > >> depend on your OS.
> > > >>
> > > >> I hope this helps,
> > > >>
> > > >> --
> > > >> Raul
> > > >>
> > > >> On Tue, Jun 19, 2018 at 8:41 PM Eric Iverson
> > > >> <[email protected]> wrote:
> > > >>>
> > > >>> I think the replies here miss an important part of the question. The
> > > >>> #!
> > > >>> script.
> > > >>>
> > > >>> #! /usr/bin/j8
> > > >>> stdin''
> > > >>>
> > > >>> On Tue, Jun 19, 2018 at 8:21 PM, Don Guinn <[email protected]> wrote:
> > > >>>
> > > >>>> It appears that the right argument is ignored. Perhaps have it be
> > > >>>> displayed
> > > >>>> on the same line as the input.
> > > >>>>
> > > >>>> readln 'Name: '
> > > >>>>
> > > >>>> On Tue, Jun 19, 2018 at 6:18 PM Raul Miller <[email protected]>
> > > >>>> wrote:
> > > >>>>
> > > >>>>> 1 -:&(3!:3) 01
> > > >>>>> 1
> > > >>>>>
> > > >>>>> No difference in what's represented, so the only difference is in
> > > >>>>> how
> > > >>>>> you type it / the leading zero / ...
> > > >>>>>
> > > >>>>> Thanks,
> > > >>>>>
> > > >>>>> --
> > > >>>>> Raul
> > > >>>>>
> > > >>>>> On Tue, Jun 19, 2018 at 8:15 PM Devon McCormick <[email protected]>
> > > >>>>> wrote:
> > > >>>>>>
> > > >>>>>> How does "01" differ from "1" in this context?
> > > >>>>>>
> > > >>>>>> "readln" works for me within a session but I think James wants to
> > > >>>>>> do
> > > >>>>>> something like pipe input into a J script. This is not something
> > > >>>>>> I've
> > > >>>>> done
> > > >>>>>> but it would be interesting if we can do it.
> > > >>>>>>
> > > >>>>>> On Tue, Jun 19, 2018 at 5:10 PM, Henry Rich <[email protected]>
> > > >>>>> wrote:
> > > >>>>>>
> > > >>>>>>> ? The 01 is correct as it stands. On Windows I get correct
> > > >>>> operation:
> > > >>>>>>>
> > > >>>>>>> readln =: [: (1!:01) 1:
> > > >>>>>>> readln''
> > > >>>>>>> abcdefg
> > > >>>>>>> abcdefg
> > > >>>>>>>
> > > >>>>>>> The first line is what I typed, the second is the JE's result
> > > >>>>>>>
> > > >>>>>>> Henry Rich
> > > >>>>>>>
> > > >>>>>>>
> > > >>>>>>> On 6/19/2018 5:05 PM, james faure wrote:
> > > >>>>>>>
> > > >>>>>>>> Either way, it's no good
> > > >>>>>>>>
> > > >>>>>>>> ________________________________
> > > >>>>>>>> From: Programming <[email protected]> on
> > > >>>>> behalf
> > > >>>>>>>> of devonmcc <[email protected]>
> > > >>>>>>>> Sent: Tuesday, June 19, 2018 11:00:39 PM
> > > >>>>>>>> To: [email protected]
> > > >>>>>>>> Subject: Re: [Jprogramming] getline
> > > >>>>>>>>
> > > >>>>>>>> The "01" following the "!:" is probably supposed to be "0]1".
> > > >>>>>>>>
> > > >>>>>>>>
> > > >>>>>>>> Sent from my Verizon, Samsung Galaxy smartphone
> > > >>>>>>>> -------- Original message --------From: Michal Wallace <
> > > >>>>>>>> [email protected]> Date: 6/19/18 16:06 (GMT-05:00) To:
> > > >>>>>>>> [email protected] Subject: Re: [Jprogramming] getline
> > > >>>>>>>> readln =: [: (1!:01) 1:
> > > >>>>>>>>
> > > >>>>>>>>
> > > >>>>>>>>
> > > >>>>>>>>
> > > >>>>>>>> On Tue, Jun 19, 2018 at 2:45 PM james faure
> > > >>>>>>>> <[email protected]
> > > >>>>>
> > > >>>>>>>> wrote:
> > > >>>>>>>>
> > > >>>>>>>> Hello, how can one read stdin one line at a time ?
> > > >>>>>>>>>
> > > >>>>>>>>>
> > > >>>>>>>>> #! /usr/bin/j8
> > > >>>>>>>>>
> > > >>>>>>>>> stdin''
> > > >>>>>>>>>
> > > >>>>>>>>>
> > > >>>>>>>>> is no good.
> > > >>>>>>>>>
> > > >>>>> ----------------------------------------------------------------------
> > > >>>>>>>>> For information about J forums see
> > > >>>>> http://www.jsoftware.com/forums.htm
> > > >>>>>>>>>
> > > >>>>>>>> ------------------------------------------------------------
> > > >>>> ----------
> > > >>>>>>>> For information about J forums see
> > > >>>>> http://www.jsoftware.com/forums.htm
> > > >>>>>>>> ------------------------------------------------------------
> > > >>>> ----------
> > > >>>>>>>> For information about J forums see
> > > >>>>> http://www.jsoftware.com/forums.htm
> > > >>>>>>>> ------------------------------------------------------------
> > > >>>> ----------
> > > >>>>>>>> For information about J forums see
> > > >>>>> http://www.jsoftware.com/forums.htm
> > > >>>>>>>>
> > > >>>>>>>
> > > >>>>>>>
> > > >>>>>>> ---
> > > >>>>>>> This email has been checked for viruses by AVG.
> > > >>>>>>> https://www.avg.com
> > > >>>>>>>
> > > >>>>>>>
> > > >>>>>>> ------------------------------------------------------------
> > > >>>> ----------
> > > >>>>>>> For information about J forums see http://www.jsoftware.com/
> > > >>>> forums.htm
> > > >>>>>>>
> > > >>>>>>
> > > >>>>>>
> > > >>>>>>
> > > >>>>>> --
> > > >>>>>>
> > > >>>>>> Devon McCormick, CFA
> > > >>>>>>
> > > >>>>>> Quantitative Consultant
> > > >>>>>> ----------------------------------------------------------------------
> > > >>>>>> For information about J forums see
> > > >>>>>> http://www.jsoftware.com/forums.htm
> > > >>>>> ----------------------------------------------------------------------
> > > >>>>> For information about J forums see
> > > >>>>> http://www.jsoftware.com/forums.htm
> > > >>>> ----------------------------------------------------------------------
> > > >>>> For information about J forums see
> > > >>>> http://www.jsoftware.com/forums.htm
> > > >>>>
> > > >>> ----------------------------------------------------------------------
> > > >>> For information about J forums see http://www.jsoftware.com/forums.htm
> > > > ----------------------------------------------------------------------
> > > > For information about J forums see http://www.jsoftware.com/forums.htm
> > >
> > > ----------------------------------------------------------------------
> > > For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm