select: function for each value in col not working as expected

What is the proper way to call a function on each item of the vector?

/ create table, one col has strings with spaces

q)t:(s:ab;(n:$"dd ee";$“f g h”))

/ function to vector-from-scalar by spaces

q)f:{first " "vs x}

q)f “dd ee”

 “dd”

/ select 

q) select s, f n from t

'type

  [1]  f:{first " "vs x}

                   ^

q)) select s, f each n from t

'type

  [6]  f:{first " "vs x}

                   ^

Your function f expects a string not a symbol

q)select ('[first;" "vs]') string x from tx----"dd","f"q)select f each string x from tx----"dd","f"

Hi Ag,You table definition appears to only set "dd ee" to n rather than both your string values.
Also the  $ only needs used once outside the brackets. t` would be better defined as follows;

q)t:([]s:`a`b;n:`$("dd ee";"f g h"))

q)t
s n
-------
a dd ee
b f g h

q)meta t
c| t f a
-| -----
s| s
n| s

Adding string to your f function would then convert each of the n values to string when queried.

f:{first " "vs string x}

Your query would also be better defined as

select s, f'[n] from t

If you are always looking only the first element I would recommend using the following instead;

q)update g:{[x] ((s)?'" ")#'s:string[x]}[n] from t
s n  <wbr style='"box-sizing:' inherit>  <wbr style='"box-sizing:' inherit> g<br style='"box-sizing:' inherit>------------<br style='"box-sizing:' inherit>a dd ee "dd"<br style='"box-sizing:' inherit>b f g h ,"f"<br style='"box-sizing:' inherit></wbr></wbr>

It is equivalent to using f but much quicker

q)(update g:f'[n] from t)~update g:{[x] ((s)?'" ")#'s:string[x]}[n] from t <wbr style='"box-sizing:' inherit> <wbr style='"box-sizing:' inherit> <wbr style='"box-sizing:' inherit> <wbr style='"box-sizing:' inherit>/both functions are equivalent<br style='"box-sizing:' inherit>1b<br style='"box-sizing:' inherit><br style='"box-sizing:' inherit>q)p:1000#t<br style='"box-sizing:' inherit>q)\t:1000 update g:f'[n] from t<br style='"box-sizing:' inherit>4<br style='"box-sizing:' inherit>q)\t:1000 update g:f'[n] from p<br style='"box-sizing:' inherit>935<br style='"box-sizing:' inherit><br style='"box-sizing:' inherit>q)\t:1000 update g:{[x] ((s)?'" ")#'s:string[x]}[n] from t<br style='"box-sizing:' inherit>2<br style='"box-sizing:' inherit>q)\t:1000 update g:{[x] ((s)?'" ")#'s:string[x]}[n] from p<br style='"box-sizing:' inherit>237<br style='"box-sizing:' inherit></wbr></wbr></wbr></wbr>

And if your columns create many repeated elements you could also use .Q.fu (apply unique) to speed this up even further.Hope this helps.


On Sunday, July 21, 2019 at 11:22:36 AM UTC+1, ag wrote:

What is the proper way to call a function on each item of the vector?

/ create table, one col has strings with spaces

q)t:(s:ab;(n:$"dd ee";$“f g h”))

/ function to vector-from-scalar by spaces

q)f:{first " "vs x}

q)f “dd ee”

 “dd”

/ select 

q) select s, f n from t

'type

  [1]  f:{first " "vs x}

                   ^

q)) select s, f each n from t

'type

  [6]  f:{first " "vs x}

                   ^

Hi Ag,As Kevin has mentioned you can use the .Q.fu function.
This takes two arguments.
.Q.fu[x;y]
x is a unary atomic function and y is a list.
This will apply x on distinct values of y.Using the table p and function f set by Kevin:

q)f:{first " "vs string x}
q)p:1000#t

Here I have shown the that .Q.fu is the faster function.

q)\t:1000 update g:f'[n] from p
1022
q)\t:1000 update g:.Q.fu[f';n] from p
19

This is useful in a situation when you have a lot of repeats in your list.
Regards,
David