databrain
|
  |
| Joined: 01 Jan 2013 |
| Total Posts: 3342 |
|
|
| 24 Jun 2013 05:32 PM |
My code:
_G.split = function(str, pat) local t = {} local fpat = "(.-)" .. pat local last_end = 1 local s, e, cap = string.find(str, fpat, 1) while s do if s ~= 1 or cap ~= "" then table.insert(t, cap) end last_end = e+1 s, e, cap = string.find(str, fpat, last_end) end if last_end <= #str then cap = string.sub(str, last_end) table.insert(t, cap) end return t end
local t = _G.split("d", "edrdpderp") for _,v in pairs(t) do print(v) end
It acts as if "Pat" is nil, instead of "edrdpderp" like it should be. I'm new to _G, but I would like to know how you can put more than one arg in a global function.
Emerson wrote an essay on that |
|
|
| Report Abuse |
|
|
|
| 24 Jun 2013 05:34 PM |
you can have almost infinite arguments. dont remember the name for it but technically:
function asd(...) local arguments = {} for i =1, #arguments do print(arguments[i]) end end
asd(1,3,3,5,35,35,35,3,46,5,64,646,45,654,765,65,6,5,4,64,65,46,5,64,6,3,45)
also works with global functions.
There would be an error in your code. |
|
|
| Report Abuse |
|
|
|
| 24 Jun 2013 05:35 PM |
*
function asd(...) local arguments = {...} for i =1, #arguments do print(arguments[i]) end end |
|
|
| Report Abuse |
|
|
databrain
|
  |
| Joined: 01 Jan 2013 |
| Total Posts: 3342 |
|
|
| 24 Jun 2013 05:38 PM |
I tried that, but it still thinks there's only one arg.
Emerson wrote an essay on that |
|
|
| Report Abuse |
|
|
databrain
|
  |
| Joined: 01 Jan 2013 |
| Total Posts: 3342 |
|
|
| 24 Jun 2013 05:40 PM |
changing _G.split= function(str,pat) to function _G.split(str,pat) still did the same thing qq.
Emerson wrote an essay on that |
|
|
| Report Abuse |
|
|
|
| 24 Jun 2013 05:40 PM |
My code as a global function worked fine. "_G.asd(1,1,31,2,3,4,234,23,23,525,235,23,4,5435,4)" returned: 1 1 31 2 3 4 234 23 23 525 235 23 4 5435 4 |
|
|
| Report Abuse |
|
|
databrain
|
  |
| Joined: 01 Jan 2013 |
| Total Posts: 3342 |
|
|
| 24 Jun 2013 05:41 PM |
Must be my split function. This is the exact split function on the lua wiki though.
Emerson wrote an essay on that |
|
|
| Report Abuse |
|
|
|
| 24 Jun 2013 05:42 PM |
try adding
print(tostring(pat))
before you add it to the string and tell me does it print nil |
|
|
| Report Abuse |
|
|
|
| 24 Jun 2013 05:44 PM |
I MIGHT HAVE FOUND A SOLUTION, I AM STUPID SORRY.
_G is a table.
Tables require a "self" argument before your own args.
like this:
table = {}
function table.func(self,arg) print(arg) end
table.func("asd")
would print asd.
Not sure if this is with _G |
|
|
| Report Abuse |
|
|
databrain
|
  |
| Joined: 01 Jan 2013 |
| Total Posts: 3342 |
|
|
| 24 Jun 2013 05:45 PM |
What it's doing is returning str.
Emerson wrote an essay on that |
|
|
| Report Abuse |
|
|
databrain
|
  |
| Joined: 01 Jan 2013 |
| Total Posts: 3342 |
|
|
| 24 Jun 2013 05:46 PM |
Oh, and it's not _G. I was just being stupid and not testing it as a normal function.
Emerson wrote an essay on that |
|
|
| Report Abuse |
|
|
databrain
|
  |
| Joined: 01 Jan 2013 |
| Total Posts: 3342 |
|
|
| 24 Jun 2013 05:58 PM |
I reposted, since the problem is really something else.
Emerson wrote an essay on that |
|
|
| Report Abuse |
|
|