TopAz Posted April 1, 2012 Share Posted April 1, 2012 function addNum(...) num = 0 for i, b in ipairs{...} do num = num + b end return num end Thanks in advance. Link to comment
Castillo Posted April 1, 2012 Share Posted April 1, 2012 function addNum ( ... ) local num = 0 for i, b in ipairs ( ... ) do -- Loop through the table given. num = num + 1 -- Increase 'num' variable by one. end return num -- Return the 'num' variable. end outputChatBox ( addNum ( { "Hello", "World" } ) ) Link to comment
BinSlayer1 Posted April 1, 2012 Share Posted April 1, 2012 Useless code. You could easily use # such as if the table was called pants you can call #pants and it will return the number of elements Link to comment
Kenix Posted April 1, 2012 Share Posted April 1, 2012 BinSlayer1,Solidsnake14 *FACEPALM* Example function fSize( ... ) return #( ... ) end print( fSize( { [1] = true; [3] = 4 } ) )-- 1 function fSize( ... ) return #( ... ) end print( fSize( { [1] = true; [2] = 2; [3] = 4 } ) ) -- 3 It's correct function fTableSize( ... ) local nCount = 0 for _ in pairs( ... ) do nCount = nCount + 1 end return nCount end print( fTableSize { [1] = 1; [3] = 3 } )-- 2 ipairs looping only numeric indexes in table and in order. I show upper examples. Also table.getn, operator # use this method too. Link to comment
Castillo Posted April 1, 2012 Share Posted April 1, 2012 @Kenix: I only fixed his script, I was going to use "pairs" instead of "ipairs", but I didn't. Link to comment
TopAz Posted April 1, 2012 Author Share Posted April 1, 2012 I got another problem. Why is this local somedata = { ["name"] = "TopAz", ["age"] = "1000", ["profession"] = "Programmer", ["language"] = "CSharp" } for key,value in pairs(somedata) do print(key .. " | " .. value) end Outputting this language | CSharp age | 1000 name | TopAz profession | Programmer Instead of this name | TopAz age | 1000 profession | Programmer language | CSharp Link to comment
Castillo Posted April 1, 2012 Share Posted April 1, 2012 That's because is not an indexed table. Link to comment
TopAz Posted April 1, 2012 Author Share Posted April 1, 2012 That's because is not an indexed table. Do you know how to fix it? ipairs is not working. Link to comment
Castillo Posted April 1, 2012 Share Posted April 1, 2012 You can do this: local somedata = { { "name", "TopAz" }, { "age", "1000" }, { "profession", "Programmer" }, { "language", "CSharp" } } for index, value in ipairs ( somedata ) do print ( value[ 1 ] .. " | " .. value[ 2 ] ) end Link to comment
TopAz Posted April 1, 2012 Author Share Posted April 1, 2012 You can do this: local somedata = { { "name", "TopAz" }, { "age", "1000" }, { "profession", "Programmer" }, { "language", "CSharp" } } for index, value in ipairs ( somedata ) do print ( value[ 1 ] .. " | " .. value[ 2 ] ) end Nice idea man. Thanks! Link to comment
TopAz Posted April 1, 2012 Author Share Posted April 1, 2012 Last question. How to make a function with optional parameter? Link to comment
Castillo Posted April 2, 2012 Share Posted April 2, 2012 What do you mean? just fill the arguments you want. Link to comment
TopAz Posted April 2, 2012 Author Share Posted April 2, 2012 But it's not working. function lame(great, lol="LOL", hey=2, now="bla") print(great .. " " .. lol .. " " .. hey .. " " .. now) end lame(great, nil, 3, nil) Link to comment
Castillo Posted April 2, 2012 Share Posted April 2, 2012 That's wrong, correct: function lame ( great, lol, hey, now ) print ( tostring ( great ) .. " " .. tostring ( lol ) .. " " .. tostring ( hey ) .. " " .. tostring ( now ) ) end lame("great", nil, 3, nil) Link to comment
TopAz Posted April 2, 2012 Author Share Posted April 2, 2012 That's wrong, correct: function lame ( great, lol, hey, now ) print ( tostring ( great ) .. " " .. tostring ( lol ) .. " " .. tostring ( hey ) .. " " .. tostring ( now ) ) end lame("great", nil, 3, nil) So, how to add default value? Link to comment
Castillo Posted April 2, 2012 Share Posted April 2, 2012 You can use "or". function lame ( great, lol, hey, now ) print ( great or "None" .." ".. lol or "None" .." ".. hey or "None" .." ".. now or "None" ) end lame("great", nil, 3, nil) Link to comment
TopAz Posted April 2, 2012 Author Share Posted April 2, 2012 You can use "or". function lame ( great, lol, hey, now ) print ( great or "None" .." ".. lol or "None" .." ".. hey or "None" .." ".. now or "None" ) end lame("great", nil, 3, nil) Thanks. Sorry for troubling you so long. Link to comment
Kenix Posted April 2, 2012 Share Posted April 2, 2012 So, how to add default value? In lua not have defaults arguments ( very bad ) I like it in c++ like languages. function fTest( n ) if type( n ) == 'number' then return n + 1 end return 1 end print( fTest( 1 ) ) -- 2 print( fTest( ) ) -- 1 function fTestNext( n ) local n = type( n ) == 'number' and n or 1 return n + 1 end print( fTestNext( 0 ) ) -- 1 print( fTestNext( ) ) -- 2 Ask questions. Link to comment
BinSlayer1 Posted April 2, 2012 Share Posted April 2, 2012 To implement default values for function parameters just check inside the function (in the header) if the parameter is nil If it is, initialize it in any way you want (this is a default value). If it's not nil, then your function will use the one supplied Link to comment
TopAz Posted April 2, 2012 Author Share Posted April 2, 2012 So, how to add default value? In lua not have defaults arguments ( very bad ) I like it in c++ like languages. function fTest( n ) if type( n ) == 'number' then return n + 1 end return 1 end print( fTest( 1 ) ) -- 2 print( fTest( ) ) -- 1 function fTestNext( n ) local n = type( n ) == 'number' and n or 1 return n + 1 end print( fTestNext( 0 ) ) -- 1 print( fTestNext( ) ) -- 2 Ask questions. Can you please explain me this line? local n = type( n ) == 'number' and n or 1 Link to comment
Kenix Posted April 2, 2012 Share Posted April 2, 2012 So, how to add default value? In lua not have defaults arguments ( very bad ) I like it in c++ like languages. function fTest( n ) if type( n ) == 'number' then return n + 1 end return 1 end print( fTest( 1 ) ) -- 2 print( fTest( ) ) -- 1 function fTestNext( n ) local n = type( n ) == 'number' and n or 1 return n + 1 end print( fTestNext( 0 ) ) -- 1 print( fTestNext( ) ) -- 2 Ask questions. Can you please explain me this line? local n = type( n ) == 'number' and n or 1 You define local variable n and you check variable n ( in arguments function ) ( is number ) and you get count this variable. If variable n not number then local variable n is 1. Link to comment
TopAz Posted April 2, 2012 Author Share Posted April 2, 2012 (edited) Thanks mate. I have another problem. How do I evacuate the mysql data table on my local table without needing to assign them manually? local root = getRootElement() local connectionHandle; local pldata = { {"id", "0"}, {"name", "N/A"}, {"age", "0"}, {"profession", "N/A"}, {"language", "N/A"} } addEventHandler("onPlayerJoin", root, function() spawnPlayer(source, 1959.55, -1714.46, 10) fadeCamera(source, true) setCameraTarget(source, source) end ) addEventHandler("onResourceStart", root, function() connectionHandle = dbConnect("mysql", "dbname=testgm;host=localhost", "root", "") local query = dbQuery(connectionHandle, "SELECT * FROM `user` WHERE `id` = 1") if query then for key, value in ipairs(dbPoll(query, -1)) do -- how to assign pldata in here automatically? end end end ) Edit: Here is the MySQL structure. Edited April 2, 2012 by Guest Link to comment
Kenix Posted April 2, 2012 Share Posted April 2, 2012 No problem. How do I evacuate the mysql data table on my local table without needing to assign them manually? Not understand. Link to comment
TopAz Posted April 2, 2012 Author Share Posted April 2, 2012 No problem.How do I evacuate the mysql data table on my local table without needing to assign them manually? Not understand. I mean how to assign those table with the mysql result table without manually assigning all the values? Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now