How could I trim trailing null characters from a string column?

Q1. How could I trim trailing null characters from a string column?

Q2. How to perform this query: select sym, strlen(raw) from test_table

Q3. Where do I learn nested object manipulation? Thx.

n:10;

alphabet: “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;

test_table: ( sym: n#alphabet; raw:{(x#alphabet), (n-x)#“\000”} each til n)

sym raw

----------------------------------------------

A “\000\000\000\000\000\000\000\000\000\000”

B “A\000\000\000\000\000\000\000\000\000”

C “AB\000\000\000\000\000\000\000\000”

D “ABC\000\000\000\000\000\000\000”

E “ABCD\000\000\000\000\000\000”

F “ABCDE\000\000\000\000\000”

G “ABCDEF\000\000\000\000”

H “ABCDEFG\000\000\000”

I “ABCDEFGH\000\000”

J “ABCDEFGHI\000”

For #1, if you look at trim and what it uses, you can easily build your own

q)trim

k){ltrim rtrim x}

q)ltrim

k){$[~t&77h>t:@x;.z.s’x;" "=*x;(+/&" "=x)_x;x]}

q)rtrim

k){$[~t&77h>t:@x;.z.s’x;" "=last x;|ltrim@|x;x]}

copy above, define null trims

q)k)nrtrim:{$[~t&77h>t:@x;.z.s’x;“\000”=last x;|nltrim@|x;x]}

q)k)nltrim:{$[~t&77h>t:@x;.z.s’x;“\000”=*x;(+/&"\000"=x)_x;x]}

q)ntrim:{ltrim rtrim x}

q)ntrim:{nltrim nrtrim x}

q)update ntrim raw from test_table

#2

select sym, n:count each raw from test_table

#3

code.kx.com

search on nested

thx ^_^

On Sunday, February 22, 2015 at 8:01:04 AM UTC-5, Yan Yan wrote:

Q1. How could I trim trailing null characters from a string column?

In addition to Charles’ solution, note that nulls are trimmed when you convert strings to symbols:

q)`$“ABCDEFGHI\000”

`ABCDEFGHI

q)string`$“ABCDEFGHI\000”

“ABCDEFGHI”

You may not want to use the round-trip as a general-purpose trim (`$ pollutes the symbol cache), but if your data is destined for a sym column, you can omit the trim.

 

Q2. How to perform this query: select sym, strlen(raw) from test_table

q)select sym,raw?:“\000” from test_table

sym x


A   0

B   1

C   2

D   3 

..

Q3. Where do I learn nested object manipulation?

 

http://code.kx.com/wiki/JB:QforMortals2/tables#Complex\_Column\_Data 

> convert strings to symbols
That sinks into the irrecoverable interned symbol string memory pool.

On Wednesday, February 25, 2015 at 2:46:25 AM UTC-5, Yan Yan wrote:

> convert strings to symbols
That sinks into the irrecoverable interned symbol string memory pool.

Yes, that’s why I wrote: “You may not want to use the round-trip as a general-purpose trim (`$ pollutes the symbol cache), but if your data is destined for a sym column, you can omit the trim.”

 

On Wednesday, February 25, 2015 at 12:31:49 AM UTC+8, Alexander Belopolsky wrote:

On Sunday, February 22, 2015 at 8:01:04 AM UTC-5, Yan Yan wrote:

Q1. How could I trim trailing null characters from a string column?

In addition to Charles’ solution, note that nulls are trimmed when you convert strings to symbols:

q)`$“ABCDEFGHI\000”

`ABCDEFGHI

q)string`$“ABCDEFGHI\000”

“ABCDEFGHI”

You may not want to use the round-trip as a general-purpose trim (`$ pollutes the symbol cache), but if your data is destined for a sym column, you can omit the trim.

 

Q2. How to perform this query: select sym, strlen(raw) from test_table

q)select sym,raw?:“\000” from test_table

sym x


A   0

B   1

C   2

D   3 

..

Q3. Where do I learn nested object manipulation?

 

http://code.kx.com/wiki/JB:QforMortals2/tables#Complex_Column_Data 

thx ^_^