Question

In J, i can do the following:

r=:'0123456'
m=:3 } r
echo m

and it prints 3, as it should.

However, unicode seems to not work:

'▁▂▃▄▅▆▇'
m=: 3 } r
echo m

prints nothing. My guess is that this is due to } indexing by byte - what is the proper way to index by char position?

Was it helpful?

Solution

You are correct that the indexing of the list given is by byte. That is because its datatype is literal. If you want it to be interpreted as unicode, then the list needs to be converted to unicode:

   datatype '①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳'       NB. check datatype of list
literal
   # '①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳'              NB. count items in list
60
   ucp '①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳'            NB. convert to unicode point chars
①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳
   datatype ucp '①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳'   NB. check datatype
unicode
   # ucp '①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳'          NB. count items in unicode list
20
   3} ucp '①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳'         NB. index into the list
④
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top