How to split a string into multiple strings?

in Lua programming language, we could use split("an:example:split",":") to split a string. But that doesn’t work in Defold! and get this error:
attempt to call global ‘split’ (a nil value)

Is that from another tool? I don’t think it’s standard Lua.

Here’s an example of splitting. You can use that module or use the function on its own.

3 Likes

I think Roblox has a helper function called split. As mentioned, it’s not part of standard Lua.

To test things like that, either use a command line verion of Lua, or an online version like this: Lua: demo (albeit it is using Lua 5.4 whereas we use 5.1))

1 Like

There’s also our own codepad. Here’s one with a split() helper function:

https://defold.com/codepad/#?c=#cp_sprite&s1=DYewxghsAEBmCuA7MAXAliR0DOAHYaKAFNgDTQAmApgQLaFUBOAlAFDQfSiQy4SMps0ALzQA3gF92nWCEbQ0CrNhSM0iAOYA6DbQgowACxLlqdBi0ohpnTnwHYA2gGJ7ggNQBGALoiFN6CpECgDGKhR4Riw3bFYgkNYEZHRMJUISGlg2AEgNEC0IRDQ9FCoiACItcvJy3BBsQgxELQBPaug8rQAFABkAQQBNACE+gGEAaQB9HoB5Ga7JroBJADkAcS6Z9fJPAAZd8k6AUT6AZVW16dWTgCVyACY2VmzuKGgYvzwCYnLCgC4qAAPCC0fBUP5fQjtcpERwAPT+3nczHKOVk8jQpCE6gUfDQjGwRBizCsz1suDUiGImJwbFs8TiwVYiSQqCacHUUAywCyzIZLOS7PguAo+jK2EyphQT35STZqUwk1oVGw2AgGnFkugytV6qokzQFHIOrVGvIEuCTBlTIF8qwivUuHgxAlPPIEDtBqN0A9KUQ1oScr90EVYVAEAo3N5jJCQA

1 Like

That method you said worked! Of course, I had to change its name so that there is no problem!
You were right! :ok_hand:
I saw this feature in the Lua language in this book and I thought that it exists naturally in Lua like other languages; But in this book, he made a function for this purpose that I didn’t notice! :sweat_smile:
Programming in Lua, Fourth Edition
Roberto Ierusalimschy

2 Likes

Your method gave me the same error message as before! :point_down:
attempt to call global ‘split’ (a nil value)

But did you copy the “split” function from the example?
The example clearly works.

This one " britzl" gave it?
Yes! That doesn’t work in my defold editor! But works online! :joy:

Then I suggest you do a double check your code.

I could easily use the same script in the editor:

1 Like

it’s interesting!
I copied that code again and checked it visually, But it still shows the same problem! :open_mouth:
attempt to call global ‘split’ (a nil value)

It was because of “Local” function!
Now it works!

@Mathias_Westerdahl @Pkeod @britzl
The methods you provided are practical for short strings, But I need this splitting operation for long strings.
For example, a string of 150 thousand characters! :grin:
Is that possible?! :thinking:

I guess you could try by passing in a long string? :slight_smile:

I entered the string into it, But it only shows small pieces of it! :cry:
It seems that Defold can store up to about 800 characters in each string variable! :pensive:
So, it’s impossible for it to split a string with 150 thousand characters!

I really don’t think we have an internal limit other than what Lua does.
Since you can easily load a 4mb json file, and get it as a string, then I don’t think that the limit is near 800 characters.

I would start using your test code outside of Defold, in Lua 5.1, to make sure your code works.

1 Like

We are not modifying LuaJIT or Lua 5.1 in any significant way. If it works in any of those two versions of Lua it also works in Defold.

1 Like

Yes, Defold is still my favorite game engine. :smiling_face_with_three_hearts:
But for solving this problem, I also thought about using files instead of memory. :slightly_smiling_face:

Here’s a script for loading The Adventures of Sherlock Holmes (~600kb) and splitting it on any whitespace character and printing the last 100 words:

local function split(s, delimiter)
	local parts = {}
	for i in string.gmatch(s, delimiter) do
		parts[#parts+1] = i
	end
	return parts
end

function init(self)
	http.request("https://sherlock-holm.es/stories/plain-text/advs.txt", "GET", function(self, id, response)
		if response.status < 200 or response.status >= 400 then
			print("Unable to download text")
			return
		end
		local text = response.response
		local words = split(text, "(%S+)")
		for i=#words - 100,#words do
			local word = words[i]
			print(i, word)
		end

		print(("The text is %d characters and %d words. Above are the 100 last words."):format(#text, #words))
	end)
end

Will print:

DEBUG:SCRIPT: 104427	is
DEBUG:SCRIPT: 104428	now
DEBUG:SCRIPT: 104429	the
DEBUG:SCRIPT: 104430	head
DEBUG:SCRIPT: 104431	of
DEBUG:SCRIPT: 104432	a
DEBUG:SCRIPT: 104433	private
DEBUG:SCRIPT: 104434	school
DEBUG:SCRIPT: 104435	at
DEBUG:SCRIPT: 104436	Walsall,
DEBUG:SCRIPT: 104437	where
DEBUG:SCRIPT: 104438	I
DEBUG:SCRIPT: 104439	believe
DEBUG:SCRIPT: 104440	that
DEBUG:SCRIPT: 104441	she
DEBUG:SCRIPT: 104442	has
DEBUG:SCRIPT: 104443	met
DEBUG:SCRIPT: 104444	with
DEBUG:SCRIPT: 104445	considerable
DEBUG:SCRIPT: 104446	success.
DEBUG:SCRIPT: 104447	----------
DEBUG:SCRIPT: 104448	This
DEBUG:SCRIPT: 104449	text
DEBUG:SCRIPT: 104450	is
DEBUG:SCRIPT: 104451	provided
DEBUG:SCRIPT: 104452	to
DEBUG:SCRIPT: 104453	you
DEBUG:SCRIPT: 104454	"as-is"
DEBUG:SCRIPT: 104455	without
DEBUG:SCRIPT: 104456	any
DEBUG:SCRIPT: 104457	warranty.
DEBUG:SCRIPT: 104458	No
DEBUG:SCRIPT: 104459	warranties
DEBUG:SCRIPT: 104460	of
DEBUG:SCRIPT: 104461	any
DEBUG:SCRIPT: 104462	kind,
DEBUG:SCRIPT: 104463	expressed
DEBUG:SCRIPT: 104464	or
DEBUG:SCRIPT: 104465	implied,
DEBUG:SCRIPT: 104466	are
DEBUG:SCRIPT: 104467	made
DEBUG:SCRIPT: 104468	to
DEBUG:SCRIPT: 104469	you
DEBUG:SCRIPT: 104470	as
DEBUG:SCRIPT: 104471	to
DEBUG:SCRIPT: 104472	the
DEBUG:SCRIPT: 104473	text
DEBUG:SCRIPT: 104474	or
DEBUG:SCRIPT: 104475	any
DEBUG:SCRIPT: 104476	medium
DEBUG:SCRIPT: 104477	it
DEBUG:SCRIPT: 104478	may
DEBUG:SCRIPT: 104479	be
DEBUG:SCRIPT: 104480	on,
DEBUG:SCRIPT: 104481	including
DEBUG:SCRIPT: 104482	but
DEBUG:SCRIPT: 104483	not
DEBUG:SCRIPT: 104484	limited
DEBUG:SCRIPT: 104485	to
DEBUG:SCRIPT: 104486	warranties
DEBUG:SCRIPT: 104487	of
DEBUG:SCRIPT: 104488	merchantablity
DEBUG:SCRIPT: 104489	or
DEBUG:SCRIPT: 104490	fitness
DEBUG:SCRIPT: 104491	for
DEBUG:SCRIPT: 104492	a
DEBUG:SCRIPT: 104493	particular
DEBUG:SCRIPT: 104494	purpose.
DEBUG:SCRIPT: 104495	This
DEBUG:SCRIPT: 104496	text
DEBUG:SCRIPT: 104497	was
DEBUG:SCRIPT: 104498	formatted
DEBUG:SCRIPT: 104499	from
DEBUG:SCRIPT: 104500	various
DEBUG:SCRIPT: 104501	free
DEBUG:SCRIPT: 104502	ASCII
DEBUG:SCRIPT: 104503	and
DEBUG:SCRIPT: 104504	HTML
DEBUG:SCRIPT: 104505	variants.
DEBUG:SCRIPT: 104506	See
DEBUG:SCRIPT: 104507	http://sherlock-holm.es
DEBUG:SCRIPT: 104508	for
DEBUG:SCRIPT: 104509	an
DEBUG:SCRIPT: 104510	electronic
DEBUG:SCRIPT: 104511	form
DEBUG:SCRIPT: 104512	of
DEBUG:SCRIPT: 104513	this
DEBUG:SCRIPT: 104514	text
DEBUG:SCRIPT: 104515	and
DEBUG:SCRIPT: 104516	additional
DEBUG:SCRIPT: 104517	information
DEBUG:SCRIPT: 104518	about
DEBUG:SCRIPT: 104519	it.
DEBUG:SCRIPT: 104520	This
DEBUG:SCRIPT: 104521	text
DEBUG:SCRIPT: 104522	comes
DEBUG:SCRIPT: 104523	from
DEBUG:SCRIPT: 104524	the
DEBUG:SCRIPT: 104525	collection's
DEBUG:SCRIPT: 104526	version
DEBUG:SCRIPT: 104527	3.1.
DEBUG:SCRIPT: The text is 610921 characters and 104527 words. Above are the 100 last words.
1 Like

Spoiler alert!

5 Likes

Thank you very much for this valuable help!:blush::hugs:
I kept thinking how to find this solution!:thinking::man_facepalming:
thank you!:pray::heart:

1 Like