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

**URL:** <https://forum.kx.com/t/select-function-for-each-value-in-col-not-working-as-expected/11925>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [July 20, 2019, 7:17pm UTC](https://forum.kx.com/t/select-function-for-each-value-in-col-not-working-as-expected/11925 "2019-07-20T19:17:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ag12](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@ag12](https://forum.kx.com/u/ag12)\
**Post date:** [July 20, 2019, 7:17pm UTC](https://forum.kx.com/t/select-function-for-each-value-in-col-not-working-as-expected/11925/1 "2019-07-20T19:17:00Z")

</div>

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:`a`b;(n:`$"dd ee";`$“f g h”))

/ function to vector-from-scalar by spaces

q)f:{first " "vs x}

q)f “dd ee”

&nbsp;“dd”

/ select&nbsp;

q) select s, f n from t

'type

&nbsp;&nbsp;[1]&nbsp;&nbsp;f:{first " "vs x}

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;^

q)) select s, f each n from t

'type

&nbsp;&nbsp;[6]&nbsp;&nbsp;f:{first " "vs x}

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^

---

<div class="post-metadata">

**Author:** ![rathore\_ajay1](https://avatars.discourse-cdn.com/v4/letter/r/9de0a6/32.png) [@rathore\_ajay1](https://forum.kx.com/u/rathore_ajay1)\
**Post date:** [July 21, 2019, 4:18pm UTC](https://forum.kx.com/t/select-function-for-each-value-in-col-not-working-as-expected/11925/2 "2019-07-21T16:18:00Z")

</div>

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"`

---

<div class="post-metadata">

**Author:** ![kevin\_ohare](https://avatars.discourse-cdn.com/v4/letter/k/90db22/32.png) [@kevin\_ohare](https://forum.kx.com/u/kevin_ohare)\
**Post date:** [July 22, 2019, 10:01am UTC](https://forum.kx.com/t/select-function-for-each-value-in-col-not-working-as-expected/11925/3 "2019-07-22T10:01:00Z")

</div>

Hi Ag,You table definition appears to only set&nbsp;`"dd ee"`&nbsp;to&nbsp;`n`&nbsp;rather than both your string values.  
Also the `&nbsp;`$`&nbsp;only needs used once outside the brackets.&nbsp;`t`&nbsp;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&nbsp;`string`&nbsp;to your&nbsp;`f`&nbsp;function would then convert each of the&nbsp;`n`&nbsp;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&nbsp;`f`&nbsp;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:`a`b;(n:`$"dd ee";`$“f g h”))
> 
> / function to vector-from-scalar by spaces
> 
> q)f:{first " "vs x}
> 
> q)f “dd ee”
> 
> &nbsp;“dd”
> 
> / select&nbsp;
> 
> q) select s, f n from t
> 
> 'type
> 
> &nbsp;&nbsp;[1]&nbsp;&nbsp;f:{first " "vs x}
> 
> &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;^
> 
> q)) select s, f each n from t
> 
> 'type
> 
> &nbsp;&nbsp;[6]&nbsp;&nbsp;f:{first " "vs x}
> 
> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^

---

<div class="post-metadata">

**Author:** ![David\_Martin](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@David\_Martin](https://forum.kx.com/u/David_Martin)\
**Post date:** [July 22, 2019, 12:43pm UTC](https://forum.kx.com/t/select-function-for-each-value-in-col-not-working-as-expected/11925/4 "2019-07-22T12:43:00Z")

</div>

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
