Question

My first hour with the (fascinating) J language, and things are looking good, but I've met my first problem I can't analyse with the help of the documentation.

How to count characters in a string:

 y =. 'hello world'
 (~. y) ; " 0 (+/ "  1 =y)
┌─┬─┐
│h│1│
├─┼─┤
│e│1│
├─┼─┤
│l│3│
├─┼─┤
│o│2│
├─┼─┤
│ │1│
├─┼─┤
│w│1│
├─┼─┤
│r│1│
├─┼─┤
│d│1│
└─┴─┘

But how can I lose the boxes? I gather that ; joins two items in a horizontal box, but it seems hard to do this without the box characters. How do I get:

h 1
e 1
l 3

and so on? Is it a presentation option?

Was it helpful?

Solution

Well, for starters you must note that you can't group in a matrix elements of different types. The boxing here helps because it puts all elements on the same foot: all boxes.

One thing you can do is format (":) to text and stitch (,.) instead of link (;):

(~.y),. (2 ": "0 +/ "1 = y)

The 2 in 2 ": serves as a format indicator, 2 digits-wide, padded with a blank space.

UPDATE:

Or in the style of Eelvex:

({. , 2&":&#) /. ~ y

There's a few elements that might be new here. Using append (,) instead of stitch (,.) because each result of key (/.) is produced separately, we're not sticking two arrays side by side, but rather creating one big array one row at a time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top