Jump to content

Table Questions


K4stic

Recommended Posts

Hello MTAsa Community i have 2 Questions

1. if i have a table and i was add a line intro it this will remove it? testtable[name] = nil if No them how remove it because table.remove return me arithmetic error

local testtable = { } 
  
function ADDtoTable( name ) 
     testtable[name] = { "test" } 
end 
  
function RemovefromTable( name ) 
     testtable[name] = nil 
end 
  

2. if i have lot added to that table thinks how i can find if that table avaible like i want find if avaible the table testtable[name.."2"]

local testtable = { } 
  
function ADDtoTable( name ) 
     testtable[name] = { "test" } 
     testtable[name.."1"] = { "test" } 
     testtable[name.."2"] = { "test" } 
     testtable[name.."3"] = { "test" } 
end 
  

Thanks for your Time

Awaiting your Responce

Link to comment
  • Moderators

You mean removing it?

local index = 1 
while true do 
if testtable[name.. index] then 
testtable[name.. index]=nil 
else 
break 
end 
end 

Same way.

But this is better and faster.

function RemoveFromTable( name ) 
    if testtable[name] then 
            testtable[name] = nil 
    end 
end 
  
function RemoveVariableFromTable( name,variable ) 
    local table = testtable[name]  
    if table then 
        for i=1,#table do 
            if table[i]== variable then 
                table.remove(table,i) 
                    break 
            end 
        end 
    end 
end 
  
function ADDtoTable( name,variable ) 
    local table = testtable[name] 
    if table then 
        table[#table+1]=variable 
    else 
            testtable[name] = { variable } 
    end 
end 

Link to comment
  • Moderators

this is removing it:

testtable[name.. index]=nil

  
table.remove 

Removes the variable from the array position, so the array stays clean from empty places.

myTestTable={"a","b","c","d"} 
--is the same as: 
myTestTable={ 
[1]="a", 
[2]="b", 
[3]="c", 
[4]="d"} 

  
myTestTable={ 
[1]="a",-- [1] array location 1 
[2]="b", 
[3]="c", 
[4]="d"}-- [4] array location 4 
table.remove(myTestTable,2) 
  
--result: 
myTestTable={ 
[1]="a", 
[2]="c", 
[3]="d"} 

Since you don't have a clear array based on numbers, you can't use table.remove.

Or you have to do it like this:

    function RemoveFromTable( name ) 
        if testtable[name] then 
                testtable[name] = nil 
        end 
    end 
      
    function RemoveVariableFromTable( name,variable ) 
        local table = testtable[name] 
        if table then 
            for i=1,#table do 
                if table[i]== variable then 
                    table.remove(table,i) 
                        break 
                end 
            end 
        end 
    end 
      
    function ADDtoTable( name,variable ) 
        local table = testtable[name] 
        if table then 
            table[#table+1]=variable 
        else 
                testtable[name] = { variable } 
        end 
    end 

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