Reputation: 547
How do I convert types in J?
For example, how to I convert an array of strings like "4" "78" "0" "_1"
to an array of numbers like 4 78 0 _1
Upvotes: 2
Views: 113
Reputation: 8678
Note that what you call a string is, in fact, a byte list, so a string array is just a byte array of higher dimension.
There is a J primitive to interpret a byte array as a number, which is number (dyadic ".
). It's dyadic because you also have to provide a default value in case a string cannot be interpreted as a number, or if some padding has to be done.
The usage is very simple: __".'2 -3 4e 5.6 _ .7'
gives 2 _3 __ 5.6 _ 0.7
(see the documentation). As per the note, this generalizes to higher dimension arrays:
__".'2 -3 4e 5.6 _ .7',:'1 7 9 2 4 1'
2 _3 __ 5.6 _ 0.7
1 7 9 2 4 1
Upvotes: 1