Both Henry &
Raul came up with
what is
likely the shortest solution:
(~.@[ ,. [/.)/ list
I like Henry's second approach, even though it is a bit longer:
(~.@[ ,. <@;/.)/ list
┌────┬─────────┐
│bird│1 8 14 │
├────┼─────────┤
│cow │2 3 5 6 7│
├────┼─────────┤
│dog │4 9 11 13│
├────┼─────────┤
│cat │10 │
├────┼─────────┤
│pig │12 15 │
└────┴─────────┘
However, what if the tag IDs were text, instead of numerical?
animals =:
'bird';'cow';'cow';'dog';'cow';'cow';'cow';'bird';'dog';'cat';'dog';'pig';'dog';'bird';'pig'
ids
=:'one';'two';'three';'four';'five';'six';'seven';'eight';'nine';'ten';'eleven';'twelve';'thirteen';'fourteen';'fifteen'
list2 =. animals,:ids
Using Henry's second verb:
(~.@[ ,. <@;/.)/ list2
┌────┬──────────────────────┐
│bird│oneeightfourteen │
├────┼──────────────────────┤
│cow │twothreefivesixseven │
├────┼──────────────────────┤
│dog │fournineeleventhirteen│
├────┼──────────────────────┤
│cat │ten │
├────┼──────────────────────┤
│pig │twelvefifteen │
└────┴──────────────────────┘
Can you separate the IDs with spaces (or commas) between the IDs?
Modifying the original list by inserting spaces isn't allowed.
Skip Cave
ᐧ
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm