Jump to content

Problem on ipairs


WASSIm.

Recommended Posts

hi guys i have problem if use pairs its working but if use ipairs don't working on loop

premissons = {  
  
    ["Founder"] = { 
        ["info"] = true,  
        ["mote"] = true,  
        ["kick"] = true,  
        ["warn"] = true,  
        ["withdraw"] = true,  
        ["deposit"] = true,  
        ["color"] = true,  
        ["logs"] = true,  
        ["invite"] = true 
        }, 
         
    ["Leader"] = { 
        ["info"] = true,  
        ["mote"] = true,  
        ["kick"] = true,  
        ["warn"] = true,  
        ["withdraw"] = false,  
        ["deposit"] = true,  
        ["color"] = true,  
        ["logs"] = true,  
        ["invite"] = true 
        }, 
         
    ["Senior"] = { 
        ["info"] = false,  
        ["mote"] = true,  
        ["kick"] = true,  
        ["warn"] = true,  
        ["withdraw"] = false,  
        ["deposit"] = true,  
        ["color"] = false,  
        ["logs"] = false,  
        ["invite"] = true 
        }, 
         
    ["Regular"] = { 
        ["info"] = false,  
        ["mote"] = false,  
        ["kick"] = false,  
        ["warn"] = false,  
        ["withdraw"] = false,  
        ["deposit"] = true,  
        ["color"] = false,  
        ["logs"] = false,  
        ["invite"] = false 
        }, 
         
    ["New"] = { 
        ["info"] = false,  
        ["mote"] = false,  
        ["kick"] = false,  
        ["warn"] = false,  
        ["withdraw"] = false,  
        ["deposit"] = true,  
        ["color"] = false,  
        ["logs"] = false,  
        ["invite"] = false 
        }, 
         
    ["Trial"] = { 
        ["info"] = false,  
        ["mote"] = false,  
        ["kick"] = false,  
        ["warn"] = false,  
        ["withdraw"] = false,  
        ["deposit"] = true,  
        ["color"] = false,  
        ["logs"] = false,  
        ["invite"] = false 
        } 
    } 
     
    for rank, premisson in ipairs(premissons) do 
        outputChatBox(rank) 
        outputChatBox("aaa") 
    end 

Link to comment

Use something like this instead:

  
ranks = {"Founder", "Leader", "Senior", "Regular", "New", "Trial"} 
  
premissons = { 
  
    ["Founder"] = { 
        ["info"] = true, 
        ["mote"] = true, 
        ["kick"] = true, 
        ["warn"] = true, 
        ["withdraw"] = true, 
        ["deposit"] = true, 
        ["color"] = true, 
        ["logs"] = true, 
        ["invite"] = true 
        }, 
        
    ["Leader"] = { 
        ["info"] = true, 
        ["mote"] = true, 
        ["kick"] = true, 
        ["warn"] = true, 
        ["withdraw"] = false, 
        ["deposit"] = true, 
        ["color"] = true, 
        ["logs"] = true, 
        ["invite"] = true 
        }, 
        
    ["Senior"] = { 
        ["info"] = false, 
        ["mote"] = true, 
        ["kick"] = true, 
        ["warn"] = true, 
        ["withdraw"] = false, 
        ["deposit"] = true, 
        ["color"] = false, 
        ["logs"] = false, 
        ["invite"] = true 
        }, 
        
    ["Regular"] = { 
        ["info"] = false, 
        ["mote"] = false, 
        ["kick"] = false, 
        ["warn"] = false, 
        ["withdraw"] = false, 
        ["deposit"] = true, 
        ["color"] = false, 
        ["logs"] = false, 
        ["invite"] = false 
        }, 
        
    ["New"] = { 
        ["info"] = false, 
        ["mote"] = false, 
        ["kick"] = false, 
        ["warn"] = false, 
        ["withdraw"] = false, 
        ["deposit"] = true, 
        ["color"] = false, 
        ["logs"] = false, 
        ["invite"] = false 
        }, 
        
    ["Trial"] = { 
        ["info"] = false, 
        ["mote"] = false, 
        ["kick"] = false, 
        ["warn"] = false, 
        ["withdraw"] = false, 
        ["deposit"] = true, 
        ["color"] = false, 
        ["logs"] = false, 
        ["invite"] = false 
        } 
    } 
    
    for _, rank in ipairs(ranks) do 
        outputChatBox(rank) 
        for name,value in pairs(premissons[rank]) do 
            outputChatBox(name.." => "..tostring(value)) 
        end 
    end 
  

Link to comment

if you want a better explanation of what novo did, its "indexing tables", basically, any indexed table is that one that doesnt has any "name" definition or variable, its just like a table:

  
table = {name = "jose", jose = "etc"} 
  

Thats an unindexed table becouse it has variables, and you cant get numbers straight, therefore ipairs wont work.

In this case you call: table.name to get "jose".

  
table = {"jose", "etc"} 
  

Thats an indexed table, becouse it has not variables, just values. therefore ipairs will work.

In this case as you dont have variables,but you do have a key to call them: Their position.

table[1] will return "jose", as you are calling position one in the table.

-- Not so easy:

  
table = { 
{"Jose", "Juan", "etc"}, 
{"Juane", "etc", "Jase"} 
} 
  

Now, we have a complex table (just to say its harder we call it complex). (About ordering, just remember the 2nd table inside table "table" is the last value of table "table", so it actually ends like: {"juane", "etc", "Jase"}} )

Here, it is still an indexed table, and we can still call the values by key, but how?

For an unindexed table you would probably do: table.table1.name

Here, keys still work: table[1][1] will return Position 1 from a "supposed" table to be in position 1 of table "table", else if positon 1 from table is not a table it will return an error trying to index '?'. (nil value)

Also, we can manage to loop them!

  
table = { 
{"Jose", "Juan", "etc"}, 
{"Juane", "etc", "Jase"} 
} 
for i=1, #table[1] do 
outputChatBox(table[1][i]) 
end 
  

This will give: "Jose", "Juan", "etc"

Now what about a complex indexed/unindexed table?

  
table = { 
{"Jose", "Juan", "etc"}, 
{name = "Juane", etc = "etc", name2 = "Jase"} 
} 
for i=1, #table[1] do 
outputChatBox(table[1][i]) 
end 
for i,v in pairs(table[2]) do 
outputChatBox(i.." "..v) 
end 
  

This will output first the values before: "Jose", "Juan", "etc"

But we now made the second table unindexed, the first one is still indexed, so it works, but the second one it is not, so ipairs will no longer work. In this case, pairs will return the following values in a random position:

name Juan

etc etc

name2 Jase

So we can basically have huge combinations and huge tables, wich in the end, its amazing.

Hope this is usefull for you.

Greets

HyPeX

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