Hello, Sorry if this is quiet simple, I couldnt find it on the wiki anywhere.
Basically i have a table:
[CODE] 1:
{
name = davidakadavid
group = moderator
}
2:
{
name = Rover
group = moderator
}
[/CODE]
I now need to add that to a dlistview so this is what im trying:
[CODE]for i=0, table.Count(ulxranks) do
statsList:AddLine(i, ulxranks[i].name, ulxranks[i].group, "0")
end[/CODE]
Now obviously, I know thats incorrect but my question is how can i access group and name from my table while looping through? Im really sorry if this could be found elsewhere, i did look.
Thanks in advance!
The only incorrect part about your code is that you start your loop at 0, while your table starts at 1, so every single time the first iteration of the loop would fail.
To fix it, change the "for i=0" part to "for i=1"
or add unnecessary "if ( ulxranks[i] ) then /*code*/ end" checks.
However I'd have just used for k-v loop, like this:
[code]
for id, group in pairs(ulxranks) do
statsList:AddLine(i, group.name, group.group, "0")
end
[/code]
This would ensure to iterate over every table in your table, regardless of whether the key is a number or whether it starts with a 0.
Also, instead of table.Count, you can just use the # or length operator.
[QUOTE=code_gs;49701167]Also, instead of table.Count, you can just use the # or length operator.[/QUOTE]
I have to point out (and you should too when you post these things) that that would only work ( properly ) if the table has keys starting with 1 and there are no key gaps ( for example 1, 2, 4, 5 ) in the table ( like the OPs table is )
[QUOTE=Robotboy655;49701182]I have to point out (and you should too when you post these things) that that would only work ( properly ) if the table has keys starting with 1 and there are no key gaps ( for example 1, 2, 4, 5 ) in the table ( like the OPs table is )[/QUOTE]
Hi robotboy, Thanks alot for the help. However when i copy your loop and use it in my addon, The loop doesnt run? its like its being totally skipped?
[CODE]for i, group in pairs(ulxranks) do
statsList:AddLine(i, group.name, group.group, "0")
end[/CODE]
Did i do something wrong?
EDIT:
Nevermind, It just magically started to work
Run this and see console output:
[code]
PrintTable(ulxranks)
for i, group in pairs(ulxranks) do
print( i,group)
statsList:AddLine(i, group.name, group.group, "0")
end[/code]
You should see double printout of the data from ulxranks.
Sorry, you need to Log In to post a reply to this thread.