cancel
Showing results for 
Search instead for 
Did you mean: 

1 \"hello\" return hello1

Mannix
New Contributor III
New Contributor III

Why does: 
q)1 \"hello\"

hello1

I have checked that: 2 "hello" returns hello2.

I know that 1 and 2 are permanent handles to stdout and stderr, but I'm not sure why the handles would be returned after the string.

2 ACCEPTED SOLUTIONS

rocuinneagain
Contributor III
Contributor III

This behaviour happens with file handles also:

Applying the handle to data appends it to the file as bytes.
Applying the 
neg of the handle to char data appends it as text.
The result of a successful operation is the positive or negative handle.

File system | Basics | kdb+ and q documentation - Kdb+ and q documentation (kx.com)

As to why I am not sure. The choice could have been made to return a generic null (::) instead possibly.

 

For stdout and stderr it can feel strange at first but it is not printing 'hello1' in one go. Instead 'hello' is printed as you requested then as you are in an interactive session the q) prompt wants to display the returned object so it then prints '1'.

q)1"hello"; //The ; stops the q) prompt from printing the returned object '1'
helloq)
q)-1"hello"; //-1 is often more useful as it includes a \n newline char
hello

 

View solution in original post

SJT
Valued Contributor
Valued Contributor

To enlarge on Rian’s answer, the hello1 you see is not the result. The string hello is written to the console. The result 1 is also written to the console and followed by a newline as usual. Assigning the result makes the difference clear.

 

q)1 "hello"
hello1
q)a:1 "hello"
helloq)
q)a
1
q)

 

 

View solution in original post

2 REPLIES 2

rocuinneagain
Contributor III
Contributor III

This behaviour happens with file handles also:

Applying the handle to data appends it to the file as bytes.
Applying the 
neg of the handle to char data appends it as text.
The result of a successful operation is the positive or negative handle.

File system | Basics | kdb+ and q documentation - Kdb+ and q documentation (kx.com)

As to why I am not sure. The choice could have been made to return a generic null (::) instead possibly.

 

For stdout and stderr it can feel strange at first but it is not printing 'hello1' in one go. Instead 'hello' is printed as you requested then as you are in an interactive session the q) prompt wants to display the returned object so it then prints '1'.

q)1"hello"; //The ; stops the q) prompt from printing the returned object '1'
helloq)
q)-1"hello"; //-1 is often more useful as it includes a \n newline char
hello

 

SJT
Valued Contributor
Valued Contributor

To enlarge on Rian’s answer, the hello1 you see is not the result. The string hello is written to the console. The result 1 is also written to the console and followed by a newline as usual. Assigning the result makes the difference clear.

 

q)1 "hello"
hello1
q)a:1 "hello"
helloq)
q)a
1
q)