2011-08-03 13 views
5

मुझे एक ऐसे प्रोजेक्ट में निम्न कोड का सामना करना पड़ा जिसके साथ मैं काम कर रहा हूं। मैं फॉर-लूप के पुनरावृत्ति भाग को समझ नहीं पा रहा हूं। चयन() फ़ंक्शन क्या है?यह क्या करता है? i = 1 के लिए, चुनें ('#', ...)

function _log (str,...) 
    local LOG="/tmp/log.web" 
    for i=1,select('#',...) do 
    str= str.."\t"..tostring(select(i,...)) 
    end 
os.execute("echo \"".. str .."\" \>\> " .. LOG) 
end 
+4

बीटीडब्ल्यू, लूप को 'str = table.concat ({str, ...}, "\ t") द्वारा प्रतिस्थापित किया जा सकता है। – lhf

+1

मैं स्ट्रिंग को संयोजित करने के बजाय 'फ़ाइल: लिखना()' का उपयोग करने का सुझाव दूंगा और फिर 'os.execute() 'को कॉल करना बहुत तेज़ होगा। आप लाइन के अंत में फ़ाइल को 'फ्लश' करना चाह सकते हैं। –

+0

अनुकूलन संकेतों के लिए धन्यवाद – AlexStack

उत्तर

7

लुआ मैनुअल से:

If index is a number, returns all arguments after argument number 
index. Otherwise, index must be the string "#", and select returns 
the total number of extra arguments it received. 

Lua has built in multiple arguments, जिसे आप एक तालिका में परिवर्तित कर सकते हैं यदि आप वास्तव में जरूरत के लिए:

function multiple_args(...) 
    local arguments = {...} -- pack the arguments in a table 
    -- do something -- 
    return unpack(arguments) -- return multiple arguments from a table (unpack) 
end 

अंत में, आप अगर इंडेक्स के रूप में "#" पास करें, फ़ंक्शन एकाधिक तर्क प्रदान करने की गिनती देता है डी:

print(select("#")) --> 0 
print(select("#", {1, 2, 3})) --> 1 (single table as argument) 
print(select("#", 1, 2, 3)) --> 3 
print(select("#", {1,2,3}, 4, 5, {6,7,8}) --> 4 (a table, 2 numbers, another table) 

यह website देखें।

+0

लिंक के लिए धन्यवाद। यह सहायक था। – AlexStack

संबंधित मुद्दे