프로그래밍 | 2012. 1. 16. 01:32
TestTable = { t1 = { a = 10, b = 20, } }TestTable 을 변수에 할당하는 경우 변수의 참조값이 테이블로 지정되기 때문에 값이 변경되면
test1 = TestTable test2 = TestTable print("Source A:" .. TestTable.t1.a) test1.t1.a = 20 print("Test1 A:" .. test1.t1.a) print("Test2 A:" .. test2.t1.a)
function TableCopy(t) local t2 = {} for k,v in pairs(t) do if type(v) == "table" then t2[k] = TableCopy(v) else t2[k] = v end end return t2 end함수 이름은 어찌되었든.. 내부적으로 테이블을 생성해서 값을 복사하여 새로 생성한 테이블을 반환합니다
test1 = TableCopy(TestTable) test2 = TableCopy(TestTable) print("Source A:" .. TestTable.t1.a) test1.t1.a = 20 print("Test1 A:" .. test1.t1.a) print("Test2 A:" .. test2.t1.a)같은 로직을 TableCopy를 이용해서 작성하면 test2에 영향없이 test1의 값에만 변경이 있음을 확인할 수 있습니다.
Office Add-in 만들기. (0) | 2012.02.08 |
---|---|
Lua 테이블 내용 출력하기 (0) | 2012.01.28 |
Cafe24 가상 호스팅 - 1(선택) (0) | 2011.12.02 |
Lua의 주석처리 (0) | 2011.11.27 |
[ERROR] Could not find Git Path (0) | 2011.04.18 |
Recent Comments