function arg[0] table copied to temptable problem.

Discuss any general programming issues here
Post Reply
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

function arg[0] table copied to temptable problem.

Post by Bugala »

I first thought i was hunting a bug, but after 2 hours of bug hunting, im not sure anymore i am fighting agaisnt a bug, it might be that i just dont know how hollywood works in this case.


Code goes about like this:

Function (...)
if arg.n > 0 then at.temptable=arg[0]

stuff happpening to at.temptable...

EndFunction


Idea is that Function either receives no args, or it will receive Table as its Arg[0].

if this table is sent, then arg[0] is copied to at.temptable.

Then during the program Function is being called mostly without Table on arg[0] and at.temptable is being modified during function calls.



Overall the idea is, that i would have that original Table safe in its original state. And whenever I am needing it, i would copy it to that at.temptable using arg[0] method.

Then when that at.temptable is being modified during the program, i can always call that function again with that table as arg[0] and get the original version back to use.


To my understanding Hollywood normally works like this:

Table1 = table2
-modify table1-
Table1 = table2

And the end result is that Table1 is exact copy of Table2

However, in my program when using arg[0], it seems instead of resulting in Table1 being table2 it results in it being Modified Table2 (through modifying table1)


So how does it go with that Arg[0] thing.

If i:

Function(Table2)

table1 = arg[0]

Then should it COPY table2 to table1?

or does it maybe send REFERENCE of table2 to table1? (Which would result in that what ever i do to table1, would also affect original table2?)

For that is only explanation i can currently find for the odd behavior of my program.


And if it indeed is sending reference. Then how can i copy Table2s info to table1 thorugh arg so that when i modify table1, it wouldnt modify table2?
jalih
Posts: 276
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: function arg[0] table copied to temptable problem.

Post by jalih »

Bugala wrote:If i:

Function(Table2)

table1 = arg[0]

Then should it COPY table2 to table1?
Tables are reference types, just use CopyTable() to make independent copy of table.

so, in your case, it goes something like:

Code: Select all

table1 = CopyTable(arg[0])
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: function arg[0] table copied to temptable problem.

Post by Bugala »

Thanks jalih.

Didnt know they were references always.

Also didint know about that CopyTable() command, it solves my problem perfectly.
Post Reply