This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
minecraftwarlock's profile picture

String splitting function returning an empty table

Started by minecraftwarlock, 28 September 2014 - 05:00 AM
minecraftwarlock #1
Posted 28 September 2014 - 07:00 AM
I've been trying to code a simple file format for color files but this function returns an empty table:

function splitAt(chrchtr,strng,strt)
local ch = strt or 1
local parts = {}
local str = ""
local lngth = string.len(strng) - ch + 1
for i = 1,lngth do
  if strng.sub(strng,ch,ch) == chrctr then
   table.insert(parts,str)
   str = ""
  else
   str = str..string.sub(strng,ch,ch)
  end
  ch = ch + 1
end
return parts
end
This is the line that calls the code:

local args = splitAt(",",fnc(This is a function!,lol/test,[]),5)
Dragon53535 #2
Posted 28 September 2014 - 07:05 AM
Your problem is that strng.sub is nil so it's not going to be able to == chrctr try doing either string.sub(strng,ch,ch) or strng:sub(ch,ch)
minecraftwarlock #3
Posted 28 September 2014 - 07:19 AM
Your problem is that strng.sub is nil so it's not going to be able to == chrctr try doing either string.sub(strng,ch,ch) or strng:sub(ch,ch)
I did that and I still have the same problem.
Bomb Bloke #4
Posted 28 September 2014 - 08:03 AM
As written, both your splitAt function and the line you try to call it from will error out as opposed to generating an empty table. I guess you copied them to the forums manually and made some typos in the process.

Exactly what does your "fnc" function look like?

You could use string.find() to save yourself the trouble of inspecting every character individually:

local function splitAt(searchFor, searchIn, startAt)
	if startAt then searchIn = searchIn:sub(startAt) end
	
	local results = {}
	
	while true do
		local index = searchIn:find(searchFor)
		
		if index then
			if index > 1 then results[#results+1] = searchIn:sub(1,index-1) end
			searchIn = searchIn:sub(index+#searchFor)
		else
			if #searchIn > 0 then results[#results+1] = searchIn end
			return results
		end
	end
end

local myTable = splitAt(",", "r4,r,4t,45t,r34,43",5)

for i=1,#myTable do print(myTable[i]) end

I've a nagging suspicion string.gmatch() could do it better.
minecraftwarlock #5
Posted 28 September 2014 - 08:08 AM
Exactly what does your "fnc" function look like?
What do you mean?
TheOddByte #6
Posted 28 September 2014 - 09:18 AM
Exactly what does your "fnc" function look like?
What do you mean?
He means this
This is the line that calls the code:

local args = splitAt(",",fnc(This is a function!,lol/test,[]),5)
See? You're calling the function splitAt with another function as an argument, unless you meant it to be a string, then you need to put quotes around it

local args = splitAt( ",", "fnc(This is a function,lol/test,[])", 5 )
minecraftwarlock #7
Posted 28 September 2014 - 10:39 AM
Exactly what does your "fnc" function look like?
What do you mean?
He means this
This is the line that calls the code:

local args = splitAt(",",fnc(This is a function!,lol/test,[]),5)
See? You're calling the function splitAt with another function as an argument, unless you meant it to be a string, then you need to put quotes around it

local args = splitAt( ",", "fnc(This is a function,lol/test,[])", 5 )
I meant for it to be a string also that is just a representation of what's being read from a file.