5

I write script in J for linux with #!

But script hang. After Control-D script echoed entered value. But normal ENTER only put cursor on new line.

#!/path/jconsole

a =. 1!:1]3
echo a
exit ''

2 Answers2

3

You can't read a single line of text while j is in script mode, but you can schedule something to run the next time j returns to immediate execution mode by setting the immex phrase with 9!:27 and then setting the immex bit to 1 with 9!:29. Here's an example:

#!/usr/bin/env j

NB. demo showing how to make a simple repl in j.

readln =: [: (1!:01) 1:
donext =: [: (9!:29) 1: [ 9!:27

main =: verb define
  echo ''
  echo 'main loop. type ''bye'' to exit.'
  echo '--------------------------------'
  while. (s:'`bye') ~: s:<input=:readln'' do.
    echo ".input
  end.
  echo '--------------------------------'
  echo 'loop complete. returning to j.'
  NB. or put (  exit'' ) here to exit j.
)

donext 'main _'
tangentstorm
  • 7,183
  • 2
  • 29
  • 38
  • Do you have a suggestion for reading individual keys, rather than a "line" of input? For example if I was making a console-based Tetris in J, how would I read the arrow keys in real time? – Alex Shroyer Nov 19 '20 at 18:51
  • 1
    Well, you could wrap a small c/pascal/rust/go/whatever *.so file that did the terminal IO for you, but once you reach into another language anyway, you're probably better off just writing either a new J Front-end (top-level program that invokes the J engine inside its event loop) or maybe something like rlwrap. Here's something I did along the rlwrap route: https://github.com/tangentstorm/tangentlabs/blob/master/pascal/callj.pas https://github.com/tangentstorm/tangentlabs/blob/master/pascal/callj.pas – tangentstorm Nov 21 '20 at 19:32
  • 1
    Better answer: I made https://github.com/tangentstorm/j-kvm/ – tangentstorm Oct 17 '21 at 21:47
  • Ya know, I was *just* referring to this answer. :) – Alex Shroyer Oct 18 '21 at 16:26
2

The thing is that (1!:1)&3 reads till "end of file". In Linux, pressing ctrl-D sends the EOF signal.

If this is not what you're looking for, I'm afraid there there's nothing else but your "ugly trick"

a=. shell 'read foo; echo -n $foo'

as (1!:1)&1 only works during a session for some reason ...

jpjacobs
  • 9,359
  • 36
  • 45