lua-users home
lua-l archive

Re: Pass vars value not reference to table.insert

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Sorry, my question seems to be asked wrong. Yes david, you miss the point.
The issue is, that fetchRow returns a table at address: c12h3jg12 for example.
If I use print_r after 3 additions it results in this:
Table
 (
 [1] => Table
 (
 'id'=> 5
 'name'=>'huhu'
 )
 [2] => Table:c12h3jg12
 [3] => Table:c12h3jg12
 )
Means, all 3 inserted values have the same value like the last one inserted.
Seems like (even in your example) the local result has always the same address and only the address is added and not its content.
Look at the "soloution" I have right now, this one hasn't the pointer issue.
Thanks for trying to help.
Kind regards,
 Jan (DracoBlue)
David Burgess schrieb:
or try
rows={}
local result =fetchrow()
while result do
 rows[#rows+1] = result
 result =fetchrow()
end
Or am I missing the point of your question?
db
On 1/27/07, Jan Schütze <JanS@dracoblue.de> wrote:
Hello,
The following pseudo code won't add every 3 rows to the table rows,
because row is a variable so rows would contain one item, and 3 pointers
to it.
 rows={}
 local row = fetchRow()
 while (row) do
 table.insert(rows,row)
 row = fetchRow()
 end
Is there any way to add the 'row'-values content and not a pointer to it?
Currently I use:
 rows={}
 local row = fetchRow()
 while (row) do
 local row2={}
 for key,value in pairs(row) do
 row2[key]=value
 end
 table.insert(rows,row2)
 row = fetchRow()
 end
But I am sure this is easier in lua.
Kind regards,
 Jan (DracoBlue)
--------------------------
 http://dracoblue.net

AltStyle によって変換されたページ (->オリジナル) /