Jump to content

Get one item from table


Miika

Recommended Posts

Again.. I'm having problem: how to get one item from table?

If my table looks this:

teams= { 
    {"Team", 0, 255, 0}, 
    {"Team2", 255, 0, 0}, 
    {"Team3", 0, 0, 255}, -- I need to get all items from "Team3" and output to chatbox. 
    {"Team4", 0, 255, 255}, 
} 
function test() 
    outputChatBox(--[[Team3 here]], --[[Color numbers from table in here]]) 
end 
addCommandHandler("test", test)  

Is it possible?

Edited by Guest
Link to comment

I'm assuming you want to use this table to create teams, right? If so, you can structure it like this instead;

teams = { 
    [1] = {"Team", 0, 255, 0}, 
    [2] = {"Team2", 255, 0, 0}, 
    [3] = {"Team3", 0, 0, 255}, 
    [4] = {"Team4", 0, 255, 255} 
} 
  
createdTeams = {} 
  
for key, value in ipairs(teams) do 
    -- Example on how you fetch a separate value from the multi-dimensional array 
    outputChatBox("Team Name: "..tostring(teams[key][1])) 
    outputChatBox("Red: "..tostring(teams[key][2])) 
    outputChatBox("Green: "..tostring(teams[key][3])) 
    outputChatBox("Blue: "..tostring(teams[key][4])) 
    -- Create Teams (Server-Sided) 
    local theTeam = createTeam(tostring(teams[key][1]), tonumber(teams[key][2]), tonumber(teams[key][3]), tonumber(teams[key][4])) 
    table.insert(createdTeams, theTeam) -- You can save the team to a table so you can easily access it later on 
end 

Link to comment

I'm assuming you want to use this table to create teams, right? If so, you can structure it like this instead;

teams = { 
    [1] = {"Team", 0, 255, 0}, 
    [2] = {"Team2", 255, 0, 0}, 
    [3] = {"Team3", 0, 0, 255}, 
    [4] = {"Team4", 0, 255, 255} 
} 
  
createdTeams = {} 
  
for key, value in ipairs(teams) do 
    -- Example on how you fetch a separate value from the multi-dimensional array 
    outputChatBox("Team Name: "..tostring(teams[key][1])) 
    outputChatBox("Red: "..tostring(teams[key][2])) 
    outputChatBox("Green: "..tostring(teams[key][3])) 
    outputChatBox("Blue: "..tostring(teams[key][4])) 
    -- Create Teams (Server-Sided) 
    local theTeam = createTeam(tostring(teams[key][1]), tonumber(teams[key][2]), tonumber(teams[key][3]), tonumber(teams[key][4])) 
    table.insert(createdTeams, theTeam) -- You can save the team to a table so you can easily access it later on 
end 

---------------------------------------------------------------------------------------------------------------

try:

outputChatBox(tostring(teams[3][1]), teams[3][2],teams[3][3],teams[3][4]) 

Thanks! It works perfectly :)

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