Jump to content

Can someone please explain me this code?


TopAz

Recommended Posts

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,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

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

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
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
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
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
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
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
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

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.

datamysqlproblem.jpg

Edited by Guest
Link to comment
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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...