6

I have a table

t: flip `S`V ! ((`$"|A|B|"; `$"|B|C|D|"; `$"|B|"); 1 2 3)

and some dicts

t1: 4 10 15 20 ! 1 2 3 5;
t2: 4 10 15 20 ! 0.5 2 4 5;

Now I need to add a column with values on the the substrings in S and the function below (which is a bit pseudocode because I am stuck here).

f:{[s;v];
    if[`A in "|" vs string s; t:t1;];
    else if[`B in "|" vs string s; t:t2;];
    k: asc key t;
    :t k k binr v;
}

problems are that s and v are passed in as full column vectors when I do something like

update l:f[S,V] from t;

How can I make this an operation that works by row? How can I make this a vectorized function? Thanks

2
  • it is. edited, thanks for pointing out Commented Feb 14, 2018 at 4:09
  • no worries more as an aside interesting use case for sorted attribute on dictionary instead of your k: asc key t;:t k k binr v; (although tricky with binr) v:4 5 10 13 20 21;t:`s#reverse neg[4 10 15 20]! 1 2 3 5 //neg as binr;t neg v 1 2 2 3 5 0N Commented Feb 14, 2018 at 15:46

2 Answers 2

5

You will want to use the each-both adverb to apply a function over two columns by row.

In your case:

update l:f'[S;V] from t;
Sign up to request clarification or add additional context in comments.

Comments

1

To help with your pseudocode function, you might want to use $, the if-else operator, e.g.

f:{[s;v]
  t:$["A"in ls:"|"vs string s;t1;"B"in ls;t2;()!()];
  k:asc key t;
  :t k k binr v;
 };

You've not mentioned a final else clause in your pseudocode but $ expects one hence the empty dictionary at the end.

Also note that in your table the columns S and V have been cast to a symbol. vs expects a string to split so I've had to use the stringoperation - this could be removed if you are able to redefine your original table.

Hope this helps!

1 Comment

ah, so, the dollar is an if-else-if-else-...-else, like a nested ?[c1;r1;;?[c2;r2;?[....]]]. That s quite useful to know, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.