Jump to content

# [TUT] Lua Tables

## Recommended Posts

Good job (y)

##### Link to comment
Good job (y)

Thanks. Hope it helps.

Good Job dude

##### Link to comment
Good Job dude

Thank you everyone.

##### Link to comment
Good Job dude

Thank you everyone.

Everyone? Its Only Me lol anyway np

##### Link to comment
Good Job dude

Thank you everyone.

Everyone? Its Only Me lol anyway np

Yeh i know i mean you and SkatCh anyways enjoy.

##### Link to comment

Cool dude but what's difference between pairs and ipairs?

##### Link to comment

Thanks

So whenever i make an array i must use ipairs .

##### Link to comment

Not always, ipairs is used for numeric indexed tables.

Like this one:

```myTable = { "A", "B", "C" }
```

##### Link to comment
Cool dude but what's difference between pairs and ipairs?

Simply :

* ipairs:

loop only number indexes "start from 1 index" and only in order.

* pairs:

Loop all.

for more information check Solidsnake14 link

##### Link to comment

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
end

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
end

```

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

What's more, 'pairs' is a little bit faster than 'ipairs'. It was proved in some thread few mouths ago.

##### Link to comment

the speed difference between ipairs and pairs is negligible "really really small". i already tested it pairs was faster than ipairs by 0.0000000339seconds. only when you have tables nearing 15k keyvalues.

##### Link to comment

Oke so ipairs ignores alphabetical indexing while pairs does both..

Thanks <3

##### Link to comment
Oke so ipairs ignores alphabetical indexing while pairs does both..

Thanks <3

Exactly

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

Example:

```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 );
end

--[[
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
end
table.sort(keys,cmp)
local data = { }
for _, k in ipairs ( keys ) do
table.insert ( data, { k, t[k] } )
end
return data
end
```

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;
break;
end

if ( v[2]:lower() == data.jobRank:lower() ) then
isNext = true;
end
end

```

##### Link to comment

Yes ipairs gives the numerical indexes in order. Buti pprefer using pairs when the order of index is not needed like

```weapons = {
["weapon"] = "model id"
}
```

In such table i perfer pairs

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

output chatbox isn't working

Why?

##### Link to comment
```function output()
for _, name in ipairs(playerName) do
outputChatBox(name)
outputDebugString( "Working" )
end
end
addCommandHandler("table2", output)```

##### Link to comment

@Gravestone use for i=1,x do! (much faster)

```function output()
for i=1, #playerName do
outputChatBox(playerName[i])
outputDebugString( "Working" )
end
end
addCommandHandler("table2", output)```

@External Your code working fine

Edited by Walid
• ### Recently Browsing   0 members

• No registered users viewing this page.
×

• #### Social Media

×
• Create New...