I am trying to type cast a list that contains nulls (of potentially different types) to a typed list of the non-null elements. I have tried a few attempts including the following: {(type x where not null x)$x} (8.1;`;9.2) And couldn’t get it to work. What is the right way to do this? Thanks in advance.
/type of each
{type each x where not null x}(8.1;`;9.2)
/value of each
{x where not null x}(8.1;`;9.2)
Is this what you need?
Cheers,
Sean
Sent with AquaMail for Android
http://www.aqua-mail.com
Hey Victor,
The reason why you get the error is because you are trying to cast a null symbol into a float which doesn’t really work that way.
I can suggest you the following:
q)l:(8.1;`;9.2)
q)@[l;where null l;:;(l where not null l) -1]
8.1 0n 9.2
The point here is to use index to obtain the null with the type of non-null items.
q)@[l;where null l;:;l -1]
8.1 0n 9.2
Also work since q returns null with the type of the first item of the list when indexing outside the length of the list.
If the first item of the list is the odd one then it won’t work.
q)l:(`;8.1;9.2)
q)@[l;where null l;:;l -1]
`
8.1
9.2
q)(`;1;3.3) -1
`
q)(1;`;1;3.3) -1
0N
q)(3.3;1;`;1;3.3) -1
0n
Sean, I am sorry I was unclear in my original post. I was hoping to keep the structure of the list intact, but type cast the nulls to have the same type as the non-null elements.
Thanks, WooiKent. Your suggestion solved my problem except for the scenario you outlined. I took your code and came up with another way to get the appropriate null. It appears that " " is the most versatile null as it seems the most capable of being typecast to other types. I wonder if there are other ways.
@[l;where null l;:;(neg type l where not null l)$" "]