cancel
Showing results for 
Search instead for 
Did you mean: 

casting number to symbol

analyst_tech_jo
New Contributor
Again, looking for some documentation on why the following does not work:

`$($)1 / does work; shows `1

`$1 / type error why ?
11 REPLIES 11

AquaQMatt
New Contributor
Hi, 

When casting from an numerical type to type symbol, you should first cast the numeric to a string, then cast again to a symbol.

q)testnum:10
q)testnumstring:string testnum
q)testnumstring
"10"
q)`$testnumstring
`10

As to why this is the case in KDB+, I'm not actually too sure. 

Regards,
Matt

Nick
New Contributor
($) forces nomadic evaluation of $ which is 'string'

so your first line is actually:

`$ string 1

`$ 1 does not work because $ requires a character vector as its right ooerand to cast into a symbol with ` as its left operand.

why are you placing parentheses around $?

Nick
New Contributor
thanks to autocorrect:

nomadic -> monadic.

Nick,
What you are saying is probably right (and I believed the same thing too). However,  I am trying to iron out the missing area in the documentation.  Where does the documentation say "$ requires a character vector as its right ooerand to cast into a symbol with ` as its left operand"

I dont see it on the wiki 

you are right, i don't see it.

you can read about it here:

https://a.kx.com/q/d/a/q1.htm#Creating Varchars


One more question. Not really related to the current topic. Since you wrote a book and are pretty much q god level individual, is some (most) part of kdb just trying to infer behavior because doc is missing.

I am just trying to contrast it with lets say a C question where there a standard (although a standard can show behavior as undefined as well).

you are correct, there is no standard. though the wiki is slowly emerging as the definitive source for the language's behavior.

operators are overloaded based on the type of their operands. the behavior can also change based on the value of the operand: positive, negative and null. that leaves a lot of flexibility for the language.

if calling an operator with a specific type currently throws a `type error and a logical alternative is found, the language is enhanced in subsequent releases. updating the wiki sometimes trails.

this can be both frustrating and also the beginning of an 'unexpected journey'.

the wiki benefits from people like you that point out its deficiencies. thank you.

I have been asking rights to edit the wiki since I am a pretty "by the book" computer scientist. 
However, I do not want kx to take my word for it. My edits (like the posts in this forum) will be subject to review.

This will make life for the community better (I think).

Sigh.

I have been asking rights to edit the wiki since I am a pretty "by the book" computer scientist. 
However, I do not want kx to take my word for it. My edits (like the posts in this forum) will be subject to review.

This will make life for the community better (I think).

Sigh.

note that `$x is shorthand for "S"$x (iirc, originally added to make k3 users feel more at home. A token gesture perhaps!).

This alone should mostly explain why the rhs should be a char or char vector. As many other ops, it penetrates lists, tables and dictionaries, e.g.

q)`$"hello"
`hello
q)`$("hello";"world")
`hello`world
q)`$([]("hello";"world"))
x    
-----
hello
world
q)`$`a`b!("hello";"world")
a| hello
b| world
q)`$`a`b!2#enlist("hello";"world")
a| hello world
b| hello world

We did reply to your requests earlier this year for edit rights to the wiki; please check your spam folder in case our responses landed there.

thanks,
Charlie

analyst_tech_jo
New Contributor
I have been asking rights to edit the wiki since I am a pretty "by the book" computer scientist. 
However, I do not want kx to take my word for it. My edits (like the posts in this forum) will be subject to review.

This will make life for the community better (I think).

Sigh.