pmgl/microstudio

microScript : add `list.join( separator )`

SuperUserNameMan opened this issue · 2 comments

hello,

In microscript, we do have string.split( separator ).

Could we have list.join( separator ) too ?

Hi,

You can achieve this in Microscript 2.0 using the prototypes feature:

List.join = function(separator)
if this.length > 0 then
local str = ""
for i in this
str = separator + str
end
return str.substring(1)
else
return ""
end
end

so:

a = ["a","b","c"]

print (a.join("-"))

it would output:

a-b-c

That function actually exists, but I don't think it's documented (I don't know when it was added).

You can type this in the console to test if a function exists

> [].join

[native function]

That means the function exists, so you can try to figure out how to use it. Like this:

> [1,2,3].join(", ")  // notice I added a space too

"1, 2, 3"