Code: Select all
function GetIndex(groupindex, classindex, fieldindex, valueindex, whichindex, direction, browsestyle)
print(arg[whichindex])
end
Code: Select all
function GetIndex(groupindex, classindex, fieldindex, valueindex, whichindex, direction, browsestyle)
print(arg[whichindex])
end
Yeah the arg[] array is only created when you define the function with ... variable arguments. I'm not sure there is an easy, logical way to do what you want. I'd be tempted to redefine the function to use variable arguments, and then just use all the results based on their position in the the arg[] array. You can loop through the array and assign the values to regular variables like valueindex or whatever if you want.Yincognito wrote: ↑May 22nd, 2021, 2:40 pm I have this kind of Lua function (heavily simplified and not working, posted just to get an idea what I'm after):Basically, I want to get the whichindex-th function argument of GetIndex(), so if I call it like GetIndex(7,1,5,2,3,1,1) I want to print the 3-rd argument of the function, i.e. 5. The code above doesn't work, probably because it expects a variable number of arguments being used (i.e. ...), but how can I get what I need for a fixed number of arguments function like this? Besides that, I read there are some changes from Lua 5.0 to higher versions in this regard, so the approach should work with higher versions as well.Code: Select all
function GetIndex(groupindex, classindex, fieldindex, valueindex, whichindex, direction, browsestyle) print(arg[whichindex]) end
Indeed - thank you for taking a look.jsmorley wrote: ↑May 22nd, 2021, 3:29 pm Yeah the arg[] array is only created when you define the function with ... variable arguments. I'm not sure there is an easy, logical way to do what you want. I'd be tempted to redefine the function to use variable arguments, and then just use all the results based on their position in the the arg[] array. You can loop through the array and assign the values to regular variables like valueindex or whatever if you want.
Code: Select all
function GetIndex(whichindex, direction, browsestyle, ...)
print(arg[whichindex])
end
Code: Select all
items[arg[1]][2][arg[2]][2][arg[3]][2][oldindex][1]
Code: Select all
items[groupindex][2][classindex][2][fieldindex][2][oldindex][1]
Code: Select all
function GetIndex(whichindex, direction, browsestyle, ...)
local oldindex, newindex, maxindex = arg[whichindex], arg[whichindex], {GroupCount, ClassCount, FieldCount, ValueCount}
repeat
if newindex + direction < 1 or newindex + direction > maxindex[whichindex] then newindex = oldindex; break else newindex = newindex + direction end
until (browsestyle == 1 and true) or
(browsestyle == 2 and
((whichindex == 1 and string.gsub(items[oldindex][1], '%s*:.*$', '') == string.gsub(items[newindex][1], '%s*:.*$', '')) or
(whichindex == 2 and string.gsub(items[arg[1]][2][oldindex][1], '%s*:.*$', '') == string.gsub(items[arg[1]][2][newindex][1], '%s*:.*$', '')) or
(whichindex == 3 and string.gsub(items[arg[1]][2][arg[2]][2][oldindex][1], '%s*:.*$', '') == string.gsub(items[arg[1]][2][arg[2]][2][newindex][1], '%s*:.*$', '')) or
(whichindex == 4 and string.gsub(items[arg[1]][2][arg[2]][2][arg[3]][2][oldindex][1], '%s*:.*$', '') == string.gsub(items[arg[1]][2][arg[2]][2][arg[3]][2][newindex][1], '%s*:.*$', '')))) or
(browsestyle == 3 and
((whichindex == 1 and string.gsub(items[oldindex][1], '%s*:.*$', '') ~= string.gsub(items[newindex][1], '%s*:.*$', '')) or
(whichindex == 2 and string.gsub(items[arg[1]][2][oldindex][1], '%s*:.*$', '') ~= string.gsub(items[arg[1]][2][newindex][1], '%s*:.*$', '')) or
(whichindex == 3 and string.gsub(items[arg[1]][2][arg[2]][2][oldindex][1], '%s*:.*$', '') ~= string.gsub(items[arg[1]][2][arg[2]][2][newindex][1], '%s*:.*$', '')) or
(whichindex == 4 and string.gsub(items[arg[1]][2][arg[2]][2][arg[3]][2][oldindex][1], '%s*:.*$', '') ~= string.gsub(items[arg[1]][2][arg[2]][2][arg[3]][2][newindex][1], '%s*:.*$', ''))))
return newindex
end
Code: Select all
function Update()
GetIndex(7,1,5,2,3,1,1)
end
function GetIndex(...)
groupindex = arg[1]
classindex = arg[2]
fieldindex = arg[3]
valueindex = arg[4]
whichindex = arg[5]
direction = arg[6]
browsestyle = arg[7]
print(arg[whichindex])
end
I understood the idea from your earlier post, and it's excellent. Thing is, I had already rewritten the whole thing using the arguments' "switch" approach as you were writing your reply, and I was interested in as short of a code as possible too (one of the things I like about Lua snippets), so I went on with my initial idea. In other words, it was just a matter of having the stuff rewritten already, otherwise I would have probably approached this as you suggested.jsmorley wrote: ↑May 22nd, 2021, 4:08 pm If you say so...
I was going to suggest something like this:
Code: Select all
function Update() GetIndex(7,1,5,2,3,1,1) end function GetIndex(...) groupindex = arg[1] classindex = arg[2] fieldindex = arg[3] valueindex = arg[4] whichindex = arg[5] direction = arg[6] browsestyle = arg[7] print(arg[whichindex]) end
Yes, I read that (and others) a couple of times already, LOL - the simple table of functions approach there would have been what I needed, but it's one thing when one has the needed statement and another when he has to write additional code to replicate it. In such cases, looking for simplicity / clarity can lead to writing more complex code, which defeats the purpose of the "quest" in the first place.
Code: Select all
function Update()
GetIndex(7,1,5,2,3,1,1,99)
end
function GetIndex(groupindex, classindex, fieldindex, valueindex, whichindex, direction, browsestyle, ...)
print(arg[whichindex])
end
Code: Select all
function Update()
GetIndex(7,1,5,2,3,1,1,99)
end
function GetIndex(groupindex, classindex, fieldindex, valueindex, whichindex, direction, browsestyle, ...)
print(arg[1])
end
Code: Select all
function Update()
GetIndex(7,1,5,2,3,1,1,99)
end
function GetIndex(groupindex, classindex, fieldindex, valueindex, whichindex, direction, browsestyle, ...)
print(#arg)
end
Correct. I did pass the arguments I wanted to "iterate" to the ... vararg list in my approach though (simplified code):jsmorley wrote: ↑May 23rd, 2021, 11:31 am It's worth putting in your mental toolbox how variable arguments work with the arg[] table...
Note that if you try to fool it with:
It won't work. It will print "nil". The array exists, but there is no 3rd parameter in the arg[] table. How it behaves is that ONLY the arguments that are passed as part of the ... "variable" parameters (the vararg list) will be created in the arg[] table. The variable "whichindex" certainly exists and contains "3", but the 3rd argument it is pointing to is not indexed in arg[]. So in this instance this:Code: Select all
function Update() GetIndex(7,1,5,2,3,1,1,99) end function GetIndex(groupindex, classindex, fieldindex, valueindex, whichindex, direction, browsestyle, ...) print(arg[whichindex]) end
Would print "99", which is the first and only argument value contained in the vararg list / arg[] table.Code: Select all
function Update() GetIndex(7,1,5,2,3,1,1,99) end function GetIndex(groupindex, classindex, fieldindex, valueindex, whichindex, direction, browsestyle, ...) print(arg[1]) end
If you use this:
You will see that it prints "1" as the size of the vararg list / arg[] table.Code: Select all
function Update() GetIndex(7,1,5,2,3,1,1,99) end function GetIndex(groupindex, classindex, fieldindex, valueindex, whichindex, direction, browsestyle, ...) print(#arg) end
Code: Select all
function GetIndex(whichindex, direction, browsestyle, ...)
print(arg[whichindex])
end
Code: Select all
function Update()
GetIndex(3,1,1,7,1,5,2)
end