table instances

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
the underminer
Posts: 123
Joined: Mon Oct 03, 2005 4:25 am
Location: Netherlands

table instances

Post by the underminer »

They really piss me off, those instances.
When I create the table

Code: Select all

Row = {}
block1 = {2,0,1}
Row[1] = block1
Row[2] = block1
Row[1][1] = 15
then, Row[2][1] becomes 15 too. That's really annoying. Anyone knows a way to avoid this?
Behold! The Underminer got hold of a PSP
be2003
Posts: 144
Joined: Thu Apr 20, 2006 2:46 pm

Post by be2003 »

idk must be a bug or something.
- be2003
blog
the underminer
Posts: 123
Joined: Mon Oct 03, 2005 4:25 am
Location: Netherlands

Post by the underminer »

I guess so. I'm sure I didn't make a mistake. It's a real nuisance. Shine, or whoever, can you fix this?
Behold! The Underminer got hold of a PSP
romero126
Posts: 200
Joined: Sat Dec 24, 2005 2:42 pm

Post by romero126 »

tostring(block[1])

use that..
the underminer
Posts: 123
Joined: Mon Oct 03, 2005 4:25 am
Location: Netherlands

Post by the underminer »

romero126 wrote:tostring(block[1])

use that..
I think I don't get your hint romero... I changed my code to:

Code: Select all

Row = {}
block1 = {2,0,1}
Row[1] = block1
Row[2] = tostring(block1)
Row[1][1] = 15 
print(tostring(block1))
print(Row[2][1])
Output =
table: 0x1007eb98
error: source1.lua:7: attempt to index field `?' (a string value)
Behold! The Underminer got hold of a PSP
PeterM
Posts: 125
Joined: Sat Dec 31, 2005 7:25 pm
Location: Edinburgh, UK
Contact:

Post by PeterM »

Take this with a huge rock of salt since I'm not a Lua programmer.

Does lua handle non-POD (Plain Old Data) types by reference? If so...

Code: Select all

Row[1] = block1
Row[2] = block1
...would make both rows share the same block1. So you'd have to make a copy somehow.
romero126
Posts: 200
Joined: Sat Dec 24, 2005 2:42 pm

Post by romero126 »

I used it as a refrence.. not as actual code.
the reason why it has this problem is because its relational variables. Not direct data based variables.

Tables, functions, threads, and (full) userdata values are objects: variables do not actually contain these values, only references to them. Assignment, parameter passing, and function returns always manipulate references to such values; these operations do not imply any kind of copy.
http://www.lua.org/manual/5.0/manual.html#5.4 for more information. learn to research

There are always work arrounds but it still is retarded and half assed. If you really want good code, work with metatables. or Redesign your code so it works logically where all variables are pointers to actual data and not the other way arround.
the underminer
Posts: 123
Joined: Mon Oct 03, 2005 4:25 am
Location: Netherlands

Post by the underminer »

ok i'll read some on metatables since i never used any
Behold! The Underminer got hold of a PSP
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Re: table instances

Post by Shine »

the underminer wrote:then, Row[2][1] becomes 15 too. That's really annoying. Anyone knows a way to avoid this?
You can copy the table:

Code: Select all

function copyTable(old)
	local new = {}
	table.foreach(old, function (item) table.insert(new, item) end)
	return new
end

Row = {}
block1 = {2,0,1}
Row[1] = copyTable(block1)
Row[2] = copyTable(block1)
Row[1][1] = 15
KawaGeo
Posts: 191
Joined: Sat Aug 27, 2005 6:52 am
Location: Calif Mountains

Post by KawaGeo »

table.foreach() function is now deprecated. Use for i,v in ipairs(t) do ..., instead. See Lua 5.1 ref manual.

Code: Select all

function CopyTable(old)
  local new = {}
  -- for i, item in ipairs(old) do table.insert(new, item) end
  for i, item in ipairs(old) do new[i] = item end
  return new
end
Last edited by KawaGeo on Sat Aug 19, 2006 12:27 am, edited 1 time in total.
Geo Massar
Retired Engineer
the underminer
Posts: 123
Joined: Mon Oct 03, 2005 4:25 am
Location: Netherlands

Post by the underminer »

works like a dream. thnx
Behold! The Underminer got hold of a PSP
Post Reply