Jump to content

table looping problem


xTravax

Recommended Posts

hey

so i got a table which looks kinda like this

  
table = { 
{"playerSerial1","somedata","someotherdata"}, 
{"playerSerial2","somedata2","someotherdata2"}, 
{"playerSerial3","somedata3","someotherdata3} 
} 
  
  
 

so if i want to check players serial and if he has some data i'd do

  
for i,v in pairs(table) do 
   if v[1] == serial then  
        return true 
   else  
        return false 
   end 
end 
  

however if let's say our specific player's serial is on index 2 in table, it won't ever be looped

only one serial will be looped, others will be ignored

i have those multiple datas in table and i want to get data from specific serial in table

please help

Link to comment

try this:

for i,v in pairs(table) do 
  local type = getElementsByType("player") 
    for index, player in pairs(type) do 
     local serial = getPlayerSerial (player) 
      if v[1] == serial then 
        return v[2] or v[3] -- data here etc..... 
   else 
        return false 
   end 
end 

Link to comment

Walid, your code does same thing as mine.

edit: your code would still have a bug which i also have

looping through table returns only one index of table, and doesn't check all of them

so if one serial isnt same with the serial that im comparing it to, it will return false and won't check other table indexes

Edited by Guest
Link to comment

above thing is the full code

(if you put this loop in a function, and send serial to function while there are multiple serials on few indexes, if only ONE index serial isn't the one we're comparing it to, it will return false and SKIP other indexes for checking if serial is in table)

Link to comment

Try to change your table like this :

table = { 
[1] = {"playerSerial1","somedata","someotherdata"}, 
[2] = {"playerSerial2","somedata2","someotherdata2"}, 
[3] = {"playerSerial3","somedata3","someotherdata3"} 
} 
  

Edited by Guest
Link to comment
how do i look if my serial is in table if table looks that way? (sorry i never used table with [] which contains table inside table so im not sure how to do this)

it can be like this :

for i=1,#table do 
        if  table[i][1] ==  serial then  
--etc ... 
end 

Link to comment

This is much better and easier

local Table = { 
["Serial1"] = {data1="test1",data="test2"}, 
["Serial2"] = {data1="test1",data="test2"}, 
} 
  
print(Table["Serial1"].data1) -- test1 
print(Table["Serial2"].data2) -- test2 
  
local MySerial = "Serial2" 
for serial,datas in pairs(Table) do 
    if serial == MySerial then 
        print(datas.data1) -- test1 
        for k,data in pairs(datas) do 
        print(data) -- test1 then test2 
        end 
    end 
end 

Link to comment
sTable = { 
    ["playerSerial1"] = {"somedata", "someotherdata"}, 
    ["playerSerial2"] = {"somedata2", "someotherdata2"}, 
    ["playerSerial3"] = {"somedata3", "someotherdata3"} 
} 
  
if sTable[serial] then 
    -- print(sTable[serial][1]) 
    -- print(sTable[serial][2]) 
    return true 
else 
    return false 
end 

Also don't use table as variable name because it can conflict with functions such as table.insert and table.remove.

Link to comment

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