pmgl / microstudio

Free, open source game engine online
MIT License
931 stars 106 forks source link

microScript : add `list.join( separator )` #167

Open SuperUserNameMan opened 2 years ago

SuperUserNameMan commented 2 years ago

hello,

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

Could we have list.join( separator ) too ?

joseconsuegra commented 1 year ago

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

Skaruts commented 1 year ago

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"