It is currently April 24th, 2024, 1:57 pm

Splitting a string using delimiters

Discuss the use of Lua in Script measures.
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Splitting a string using delimiters

Post by jsmorley »

If you need to retrieve some string, like a setting for allowed file extensions for example:

[SomeMeasure]
FileTypes=.jpg|.png|.bmp

You will need some way to split that string into individual parts. This little Lua function, which you can stick at the bottom of your script, will take care of this for you. It will split the string on the "|" delimiter (or any delimiter pattern you define) and return a table with the separate elements.

Code: Select all

function split(str, pat)

   local t = {}
   local fpat = "(.-)" .. pat
   local last_end = 1
   local s, e, cap = str:find(fpat, 1)
   while s do
      if s ~= 1 or cap ~= "" then
	 table.insert(t,cap)
      end
      last_end = e+1
      s, e, cap = str:find(fpat, last_end)
   end
   if last_end <= #str then
      cap = str:sub(last_end)
      table.insert(t, cap)
   end
   return t
   
end -- function split
So in our example you would call:

tExtensions = split(sFileTypes, "|")

and you will have a table tExtensions with tExtensions[1] being ".jpg", tExtensions[2] being ".png", and tExtensions[3] being ".bmp".

Your "pattern / delimiter" can be anything, and is not limited to a single character.
User avatar
Yggdrasil
Posts: 24
Joined: June 25th, 2011, 5:09 pm

Re: Splitting a string using delimiters

Post by Yggdrasil »

Hello,

Here is a bit corrected version of the above code snippet. The above code doesn't work if the pat is "%"

Code: Select all

function split(str,delimiter)
	assert(type(str)=="string","bad argument #1 to 'split' (string expected, got "..type(str))
	assert(type(delimiter)=="string","bad argument #2 to 'split' (string expected, got "..type(delimiter))
	local t = {}
	for m in str:gmatch("(.-)%"..delimiter) do
		table.insert(t,m)
	end
	return t
end
Image
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Splitting a string using delimiters

Post by jsmorley »

Nice. Much simpler approach too. I notice that it still fails if the "pattern" contains "%" but is not a single "%", but since I would recommend staying away from using the magic escape as a delimiter anyway, I don't think it is a big thing.

Fails:

sString = "One % Two % Three % Four % Five"
sPat = " % "
tItems = split(sString, sPat)

sString = "One%%Two%%Three%%Four%%Five"
sPat = "%%"
tItems = split(sString, sPat)

Succeeds:

sString = "One%Two%Three%Four%Five"
sPat = "%"
tItems = split(sString, sPat)
User avatar
Yggdrasil
Posts: 24
Joined: June 25th, 2011, 5:09 pm

Re: Splitting a string using delimiters

Post by Yggdrasil »

Yes, it fails as % character needs to be escaped. So if you want to use %% as delimiter, you must call the function with "%%%" as parameter. By the way, I thought about just one character as separator :D
Image
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Splitting a string using delimiters

Post by jsmorley »

Yggdrasil wrote:Yes, it fails as % character needs to be escaped. So if you want to use %% as delimiter, you must call the function with "%%%" as parameter. By the way, I thought about just one character as separator :D
Seems to work fine with " | " as the delimiter, which might be pretty common.
newcoda
Posts: 1
Joined: October 5th, 2012, 7:50 am

Re: Splitting a string using delimiters

Post by newcoda »

Sorry for posting in an old thread - but I am having trouble with this.

I have a string with "." delimiters -- I don't understand the syntax for getting the string divided up and then later called in other measures.

Any help with the proper way to type it out would be greatly appreciated.