Jump to content

Question


dugasz1

Recommended Posts

Hi!

    asdd = { 
        {"egy", "ketto", "három"}, 
        {"egy1", "ketto1", "három1"}, 
        {"egy1", "ketto1", "három1"}, 
    } 
function wasd2 () 
  
     
    local asddCopy = asdd 
     
    outputChatBox(asdd[2][3])-- Output: teszt 
     
    asdd[2][3] = "teszt" 
     
    outputChatBox(asdd[2][3].."|"..asddCopy[2][3])-- Output: teszt|teszt 
end 
addCommandHandler("teszt3", wasd2) 

Now i noticed that if i copy a table and i overwrite a value in the new table(asddCopy) , the first table(asdd) value is change too. Why? And how can i solve to not change the first table's(asdd) value?

Link to comment

That is becouse you are indexing the table (pointing that table to the copy table)..

To avoid this you can do it by copying the table

  
    asdd = { 
        {"egy", "ketto", "három"}, 
        {"egy1", "ketto1", "három1"}, 
        {"egy1", "ketto1", "három1"}, 
    } 
function wasd2 () 
  
    
    local asddCopy = deepcopy(asdd) 
    
    outputChatBox(asdd[2][3])-- Output: teszt 
    
    asdd[2][3] = "teszt" 
    
    outputChatBox(asdd[2][3].."|"..asddCopy[2][3])-- Output: teszt|teszt 
end 
addCommandHandler("teszt3", wasd2) 
  
function deepcopy(orig) 
    local orig_type = type(orig) 
    local copy 
    if orig_type == 'table' then 
        copy = {} 
        for orig_key, orig_value in next, orig, nil do 
            copy[deepcopy(orig_key)] = deepcopy(orig_value) 
        end 
        setmetatable(copy, deepcopy(getmetatable(orig))) 
    else -- number, string, boolean, etc 
        copy = orig 
    end 
    return copy 
end 
  

Link to comment

Ohh end one other question. Can i index/link a value to another value in a table?

asdd = { 
    {1, 2, 3}, --if v[1][1] value change 
    {4, 5, 6}, 
    {7,8, --[[asdd[1][1] + 1]]}, -- v[3][3] value change to but if in inverse way don't 
} 

Someting like this. I hope, i could explain my problem clearly.

Link to comment
Ohh end one other question. Can i index/link a value to another value in a table?
asdd = { 
    {1, 2, 3}, --if v[1][1] value change 
    {4, 5, 6}, 
    {7,8, --[[asdd[1][1] + 1]]}, -- v[3][3] value change to but if in inverse way don't 
} 

Someting like this. I hope, i could explain my problem clearly.

Sorry I did not understand :o

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