I want to join a list of integers in their order to obtain a final string, e.g.
a=.11 22 33 44
And I want to get '11223344'. And also, sometimes, I need to put a separator in between '11 22 33 44' like a space in this case. What is the simplest way?
Use format ": to covnert the list of numbers to a string:
":a
11 22 33 44
There are many ways to remove the space between them or to separate them with another string.
' ' -.~ ":a NB. remove spaces (' ') from ":a
11223344
(":a) rplc ' ';', ' NB. replace spaces with ', ' (or anything else, even empty)
11, 22, 33, 44
,('-',~":)"0 a NB. append '-' to every element of a; convert to string
11-22-33-44-
etc
Another solution that can make it easier to deal with some situations.
Use the foreign 8!:0 to convert the numbers to boxed strings (this handles conversion of negative signs from _ to -).
8!:0 a
┌──┬──┬──┬──┐
│11│22│33│44│
└──┴──┴──┴──┘
Use the joinstring verb from the strings script/addon to join the boxed strings as required. For versions prior to J7 you will need to require 'strings' to ensure that the joinstring verb is available.
' ' joinstring 8!:0 a
11 22 33 44
'' joinstring 8!:0 a
11223344
'","' joinstring 8!:0 a
11","22","33","44
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With