Jump to content

[TUT] Lua Tables


Recommended Posts

However, for those who still did not understand the differences between pairs and ipairs;

local numerical = {"A", "B", "C"} 
for index, value in ipairs(numerical) do 
    print(index.." => "..value) -- 1 => A 
local alphabetical = {"A", ["two"] = "B", three = "C"} -- Alphabetical indexing can be done either with brackets or rawly (as I've done with three). Though it will be necessary to utilize brackets if you wish adding special characters to these. 
for key, value in pairs(alphabetical) do 
    print(key.." => "..value) -- 1 (numerical indexing) => A 

Also note that, ipairs will ignore alphabetical indexes. Whilst pairs will, as Walid stated above, gather both types, numerical and alphabetical ones.

Link to comment
  • 2 weeks later...

I'd just like to point out that 'pairs' don't loop in order of the table either, just because there are some scripts that want to loop the table in order.


local myTable = {  
    ["Index 1"] = "Some random information", 
    ["Another Key?"] = "Some random information ", 
    ["Should be index 3"] = "Some random information", 
    ["Master Race"] = "Some random information", 
    ["Madex"] = "Some random information", 
for index, value in pairs ( myTable ) do 
    print ( index..": "..value ); 
Expected printout: 
Index 1: Some random information 
Another Key?: Some random information 
Should be index 3: Some random information 
Master Race: Some random information 
Madex: Some random information 
-- There's a slight chance it could be in that order, but most likley it won't be 
-- There may be some algorithm that randomises them, but I don't know what it would be  
Real print-out (Example): 
Another Key?: Some random information 
Index 1: Some random information 
Madex: Some random information 
Master Race: Some random information 
Should be index 3: Some random information 

But if you do need to loop a table with non-numerical keys for whatever reason, you can use this function:

function foreachinorder(t, f, cmp) 
    local keys = {} 
    for k,_ in pairs(t) do 
        keys[#keys+1] = k 
    local data = { } 
    for _, k in ipairs ( keys ) do  
        table.insert ( data, { k, t[k] } ) 
    return data 

The only argument you need to worry about is t which is just the table you want to loop.

You'll need to do some experimenting to see how it works, because I'm not sure how to explain it.

Here's how I used it in my gamemode:

local jobRanks = { 
    ['fisherman'] = { 
        [0] = "Deck Hand", 
        [20]= "Net Baiter", 
        [75]= "Line Thrower", 
        [100]="Line Roller", 
        [140]="Boat Captain", 
        [200]="Experienced Fisherman", 
        [270]="Underwater Trap Setter" 
   -- Some other code... Skipping to the point 
    local isNext = false; 
    for _, v in ipairs ( foreachinorder ( jobRanks['fisherman'] ) ) do  
        if ( isNext ) then  
            data.nextRank = v[2]; 
            data.requiredCatches = v[1] - data.caughtFish; 
        if ( v[2]:lower() == data.jobRank:lower() ) then  
            isNext = true; 

Link to comment
  • 1 month later...
local playerName = {}
local playerSkin = {}
function player()
	local playerName2 = getPlayerName( source )
	table.insert(playerName, playerName2)
addEventHandler("onPlayerLogin", root, player)
function output()
	outputDebugString( "Working" )
addCommandHandler("table2", output)

output chatbox isn't working


Link to comment
  • Recently Browsing   0 members

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