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
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
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'gives2 _3 __ 5.6 _ 0.7(see the documentation). As per the note, this generalizes to higher dimension arrays: