rabbitgrowth
rabbitgrowth

Reputation: 169

How can I read one byte from stdin in Dyalog APL?

I’m trying to make a terminal user interface in Dyalog APL and need a way to read a single byte of user input from stdin. How can I do this? In Python for example, you can use sys.stdin.read(1).

I’ve tried using , but that reads a whole line.

Upvotes: 4

Views: 148

Answers (1)

rabbitgrowth
rabbitgrowth

Reputation: 169

You can do this with ⎕ARBIN, which can be used like this:

[number of bytes to read] [tie number of file to read from] ⎕ARBIN [bytes to output before reading from input]

The number of bytes to read is 1, and there are no bytes to output (). You can assign a tie number to stdin with ⎕NTIE.

stdin←'/dev/stdin' ⎕NTIE 0
byte←⊃1 stdin ⎕ARBIN ⍬

⎕ARBIN returns a numeric vector, so First () is used to get the byte as a scalar.

Upvotes: 3

Related Questions