Jump to content

Can someone please explain me this code?


TopAz

Recommended Posts

Posted
  
function addNum(...) 
    num = 0 
    for i, b in ipairs{...} do 
        num = num + b 
    end 
    return num 
end 
  

Thanks in advance.

Posted
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" } ) ) 

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

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.

http://vk.com/the_kenix

Вопросы задавайте на форуме, не пишите мне в личку.

Please don't pm me.

Posted

@Kenix: I only fixed his script, I was going to use "pairs" instead of "ipairs", but I didn't.

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

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 
  

Posted

That's because is not an indexed table.

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

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 

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

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

Posted

What do you mean? just fill the arguments you want.

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

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) 
  

Posted

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) 

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

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

Posted

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) 

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

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

Posted

It's ok :).

You're welcome.

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

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

http://vk.com/the_kenix

Вопросы задавайте на форуме, не пишите мне в личку.

Please don't pm me.

Posted

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

Posted
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 

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

http://vk.com/the_kenix

Вопросы задавайте на форуме, не пишите мне в личку.

Please don't pm me.

Posted (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.

datamysqlproblem.jpg

Edited by Guest
Posted

No problem.

How do I evacuate the mysql data table on my local table without needing to assign them manually?

Not understand.

http://vk.com/the_kenix

Вопросы задавайте на форуме, не пишите мне в личку.

Please don't pm me.

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

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