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

Weird result in string split function

Started by Evert, 17 August 2012 - 11:37 AM
Evert #1
Posted 17 August 2012 - 01:37 PM
Please delete this!
Topic moved to:
http://www.computercraft.info/forums2/index.php?/topic/3483-weird-result-in-string-split-function/




Hello everyone!
I made a string split function (based on a function from lua-users.org) that seems to give a very strange result.
Here is the code:


local function strsplit(regex, string, times)
local tab = {}
local startt = 1
local positie, eindpos = string.find(string, regex, startt)

if not times then
   times = 0
end

local i = 1
while times == 0 or (positie and i <= times)  do
  if not positie then
	break
  end
  table.insert(tab, string.sub(string, startt, positie-1))
  startt = eindpos + 1
  positie, eindpos = string.find(string, regex, startt)
  i = i+1
end

table.insert(tab, string.sub(string, startt, -1))
return(tab)
end

local test = "apple 1npear 2nbanana 3"
local table = {}
table = strsplit("n",test,3)
table[1] = strsplit(" ",table[1],1)
table[2] = strsplit(" ",table[2],1)
table[3] = strsplit(" ",table[3],1)
print(table[1][1] .. "---" .. table[1][2])
print("---")
print(table[2][1] .. "---" .. table[2][2])
print("---")
print(table[3][1] .. "---" .. table[3][2])
print("---")

In minecraft (tekkit) this gives the output:


apple---1
---
pear 2---
---
banana 3---
---
So I thought, there must be something wrong with the function.

But since I'm a Linux user, I can easily run this on my own computer, but it throws a different (but correct!) result.


$ lua strsplit
apple---1
---
pear---2
---
banana---3
---

I don't know where the fault lies, so maybe you can help me.

Details:
Tekkit 1.2.5 (SMP)
Linux lua version: 5.1.5-2
Edited on 17 August 2012 - 03:35 PM
Cranium #2
Posted 17 August 2012 - 02:57 PM
You might want to post this in the Ask a Pro section. That's where you would get some better help.
Evert #3
Posted 17 August 2012 - 05:34 PM
You might want to post this in the Ask a Pro section. That's where you would get some better help.

Ok. new topic can be found here:
http://www.computercraft.info/forums2/index.php?/topic/3483-weird-result-in-string-split-function/