Jump to content

K, v in pairs?


Lloyd Logan

Recommended Posts

Posted

Hey,

In alot of the community scripts and WIKI examples, There's a line "for k, v in ipairs" what does this mean? and string.byte, what does this mean?

full :

function startup() 
    local file = xmlLoadFile("pns.xml") 
    for k, v in ipairs(xmlNodeGetChildren(file)) do 
        local pos = split(xmlNodeGetAttribute(v,"pos"),string.byte(",")) 
        local marker = createMarker(pos[1],pos[2],pos[3],"cylinder") 
        createBlipAttachedTo(marker,63,2,255,0,0,255,0,250) 
        setElementData(marker,"pnsMarker",true) 
        setGarageOpen(tonumber(xmlNodeGetAttribute(v,"garage")),true) 
    end 
    xmlUnloadFile(file) 
end 
addEventHandler("onResourceStart",getResourceRootElement(),startup) 

Thanks Lloyd

If you need an Intermediate scripter feel free to PM me as I will accept "almost" any job, STATUS: UNAVAILABLE

SCOTLAND, my hometown, and the Home of GTA!

Posted

It is a loop through a table

"for" every "k" (key/index) and ", v" (value) "in ipairs(tableName)" (valid k and v on the same row of the table tableName) "do" something

Currently working as a paid scripter. Contact me for details.

Posted
It is a loop through a table

"for" every "k" (key/index) and ", v" (value) "in ipairs(tableName)" (valid k and v on the same row of the table tableName) "do" something

Ahh, may be a bit much but could you give me an example :/

If you need an Intermediate scripter feel free to PM me as I will accept "almost" any job, STATUS: UNAVAILABLE

SCOTLAND, my hometown, and the Home of GTA!

Posted
table1 = { 
[1] = {"This is the first row"}, 
[2] = {"This is the second row"} 
} 
for k, v in ipairs(table1) do 
outputChatBox("This is the row's index: "..k..", and this is the value of that row: "..v ) 
end 

it should output on the chat box:"

This is the row's index: 1, and this is the value of that row: This is the first row

This is the row's index: 2, and this is the value of that row: This is the second row

"

Currently working as a paid scripter. Contact me for details.

Posted
table1 = { 
[1] = {"This is the first row"}, 
[2] = {"This is the second row"} 
} 
for k, v in ipairs(table1) do 
outputChatBox("This is the row's index: "..k..", and this is the value of that row: "..v ) 
end 

it should output on the chat box:"

This is the row's index: 1, and this is the value of that row: This is the first row

This is the row's index: 2, and this is the value of that row: This is the second row

"

Thank you very much! I understand this quite a bit more!

If you need an Intermediate scripter feel free to PM me as I will accept "almost" any job, STATUS: UNAVAILABLE

SCOTLAND, my hometown, and the Home of GTA!

Posted

Maybe you already know this, but "k" and "v" can be anything, doesn't has to be these two letters.

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
Maybe you already know this, but "k" and "v" can be anything, doesn't has to be these two letters.

:( I didn't, Thanks!

If you need an Intermediate scripter feel free to PM me as I will accept "almost" any job, STATUS: UNAVAILABLE

SCOTLAND, my hometown, and the Home of GTA!

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