HI All, I have a question regarding string list manipulation.
I have a string list called csvData, and I need to append spaces to them so that length of all string are the same. This is my code:
paddingWorkF:{ :x,(y - count x)#" " };
maxLength:max count each csvData;
badIndices: where maxLength <> count each csvData;
fixResult:{ r:paddingWorkF[;y] each x; :r }[csvData badIndices; maxLength];
@[`csvData;badIndices;:;fixResult];
in my case there is only one line that is different from maxLength, so badIndices and fixResult are both single element list. Can anyone please tell me what is wrong with above code? I kept getting 'length error.
Thanks so much!
You can do string padding with the $ operator, might be easier to use that.
q){max[count each x]$'x}(“abc”;“defghiuu”;“sq”;“uwqq”)
"abc "
“defghiuu”
"sq "
"uwqq "
Terry
Hi,
If you have atoms in the list then Terrys code will return a type error, e.g.:
q){max[count each x]$'x}(“a”;“b”;“ac”)
{max[count each x]$'x}
'type
To fix append “” to the beginning of each string:
q){max[count each x]$'“”,/:x}(“a”;“b”;“ac”)
"a "
"b "
“ac”
Thomas Smyth
AquaQ Analytics
Thanks a lot, I will definitely try.
Also FYI I figured out why I was getting the length error ( with some expert help ), csvData is not a global variable but a variable within the calling function, backtick ` doesn’t really work in this case. Obviously when I wastesting my code line by line I had declared csvData as global variable so everything was fine, but it stopped working once I integrated this code in to my project as a function. That really confused me!