4

What is the best way to split the string column b

t:([]a:3,4,5;b:("45 | 37 <> 5 | 6";"67 | 981 <> 50 | 7";"1 | 71 <> 15 | 8"))

a   b
3   "45 | 37 <> 5 | 6"
4   "67 | 981 <> 50 | 7"
5   "1 | 71 <> 15 | 8"

to get this

a   b                     c     d    e   f
3   "45 | 37 <> 5 | 6"    45    37   5   6
4   "67 | 981 <> 50 | 7"  67    981  50  7
5   "1 | 71 <> 15 | 8"    1     71   15  8

I was trying something like

update c:"F"${2#x}each b,d:"F"${4_7#x}each b from t

it works in parts, but it does not seems the right way.

Jms
  • 139
  • 14

1 Answers1

3

You could try something like this:

q)t,'flip exec `c`d`e`f!("H H H H";" ")0:b from t
a b                    c  d   e  f
----------------------------------
3 "45 | 37 <> 5 | 6"   45 37  5  6
4 "67 | 981 <> 50 | 7" 67 981 50 7
5 "1 | 71 <> 15 | 8"   1  71  15 8

Assumes the numbers are always space separated and that there are always unwanted symbols in between.

terrylynch
  • 11,844
  • 13
  • 21
  • Why do I get `enlist` in the columns c,d,e,f? Is there a easier way if the numbers are all separated by the same token? like | for instance? – Jms Mar 22 '19 at 18:34
  • Also, would you mind explain what is this solution doing? what is the 0 (zero) after the parentheses doing? – Jms Mar 22 '19 at 18:49
  • You're right - made a slight correction to the function to avoid the `enlist`, aka enlisted atoms. The solution is using `0:` to read/parse the delimited list of strings as per `https://code.kx.com/q/ref/filenumbers/#load-csv`. Here the space `" "` is the delimiter, "H" is converting numbers to integers, and the spaces in between each "H" is ignoring the special characters "|" and "<>". And yes, it would be a little simpler if they were all separated by a single delimiter – terrylynch Mar 22 '19 at 19:08
  • that's perfect, just one last question: what would you do to drop the column `b` in the last table? I know I could do like `(select a from t),'flip exec `c`d`e`f!("H H H H";" ")0:b from t` but there is for sure a more fancy way. – Jms Mar 22 '19 at 19:30
  • `select a from t` is perfectly fine but if you have more than one column and you want to only remove `b` then you could ```delete b from t,'flip exec `c`d`e`f!("H H H H";" ")0:b from t``` – terrylynch Mar 22 '19 at 19:35