DolphinDB: in a vector consisting of strings, how to add a “0“ before odd-length strings?

31 Views Asked by At

Suppose a column contains strings with an odd or even number of characters. I want to add a character “0“ before odd-length strings by using

select Code, iif(Code.strlen() % 2 = 0, Code, `0 + Code) from tmpCode

However, it takes 5GB of memory and a long time to execute the above statement on a 30MB table. Is there a more effective way to achieve this?

1

There are 1 best solutions below

0
On BEST ANSWER

You can define a function using the lpad function, as shown below:

def evenLPad(str, pad): lpad{,,pad}:E(str, 2*ceil strlen(str)\2)

If there are too many strings, you can also refer to the following script, which is 20% faster:

def evenLPad(str, pad): lpad{,,pad}:E(str, 254&strlen(str)+1)