Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input string in J script hangs

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 ''
like image 419
Sergey Kamenev Avatar asked Nov 07 '14 21:11

Sergey Kamenev


2 Answers

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 _'
like image 90
tangentstorm Avatar answered Oct 24 '22 06:10

tangentstorm


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 ...

like image 27
jpjacobs Avatar answered Oct 24 '22 06:10

jpjacobs