
It was the fact that the cake wrapper was on the bed, and not on the bedside table that was the clue that triggered me to realizing that something wasn't quite where I had left it. And examining the wrapper and finding it empty and clear, I realised that something else - the remaining slice of cake - was missing completely.
Which is why in my Lua coding example today I wrote that gypsy comprises Greyhound, Staffordshire Bull Terrier, and Ginger Cake.
gypsy = {"Greyhound","Staffordshire Bull Terrier","Ginger Cake"}
But that was yesterday and - well - she's our dog, so Lisa gave her some dogfood and the gravy she loves this morning:
ourdog = gypsy
ourdog[3] = "dogfood and gravy"
Now it appears that I have just changed the copy that I made of the table called "gypsy", doesn't it? And that if I were to print out the original table, it would still contain the Ginger Cake? Well - that turns out not to be the case:
print (table.concat(gypsy," and "))
gave me:
Greyhound and Staffordshire Bull Terrier and dogfood and gravy
and looking at the two tables:
print (gypsy, ourdog)
tells me that they're one and the same:
table: 0x9e34fd8 table: 0x9e34fd8
In Lua, when you copy a table, you're really just copying the reference - in other words you're giving it another name. And if you then alter it under one name, you'll alter it under the second name too.
As a second example, I asked one of my delegates today what he called his mother. "Mum" he replied. And I asked him what his father called his mother. "Margaret" he replied (not really, I'm protecting names). "Now - if your father poured a tin of pink paint over Margaret, would you say 'Mum, you're pink'" I asked. "Yes" he replied ...
Python behaves in the same way as Lua, as does version 5 of PHP ... version 4 of PHP and Perl both replicate the whole of a list / array, and you would still have seen the ginger cake when you printed out what Gypsy's made of! The Lua / Python / PHP5 behavior also has its equivalent in C with the copying of a pointer. (written 2009-11-09, updated 2009-11-10)
Associated topics are indexed under
U105 - Lua - Tables and the table library. [2703] Lua Metatables - (2010-04-02)
[2699] Lua tables - they are everything - (2010-03-30)
[2346] The indexed and hashed parts of a Lua table - (2009-08-10)
[1742] Lua - Table elements v table as a whole - (2008-08-07)
[1697] Sorting in lua - specifying your own sort routine - (2008-07-05)
Z203 - Cat and Dog [3606] Gypsy and Billy enjoy the snow - (2012-02-10)
[3561] Happy Christmas ... a Christmas morning walk in Melksham - (2011-12-25)
[3553] Changes to morning routines - (2011-12-16)
[3541] I loves Melksham - (2011-12-06)
[3537] Letting the foster parents know ... too little or too much? - (2011-12-04)
[3533] No message - just doggy pictures of the new doggy - (2011-11-30)
[3530] A typical weekend?? - (2011-11-28)
[3338] Return trip - Dogs Trust, Newbury - (2011-06-27)
[3324] Rabbit Shelter - (2011-06-12)
[3096] Contrast in pictures - (2010-12-20)
[2897] The Land of the Black Labrador - (2010-08-01)
[2787] Sleeping in on Sunday - (2010-05-30)
[2689] Can my dog eat potatoes? Doggie Dietary Research, and political sleaze! - (2010-03-23)
[2555] Bookkeeping - (2009-12-29)
[2496] A better design of mouth - (2009-11-07)
[2456] Bright Eyes - (2009-10-16)
[2451] Gypsy says - (2009-10-12)
[2450] Family Gathering at 404, The Spa - (2009-10-11)
[2295] The dog is not in trouble - (2009-07-17)
[2271] Dogs Trust, Dog Show, Newbury - (2009-07-06)
[2252] Leaping dog, Leaping horse, copyright of old masters - (2009-06-20)
[2167] Watching the tele - (2009-05-10)
[2140] Beware - giving copyright away when you upload a picture - Dogs Trust - (2009-04-25)
[2138] Ski bore ... train bore ... dog bore - (2009-04-24)
[2132] Learning about Lurchers - (2009-04-18)
[2029] Also for the eyes of the cat - (2009-02-07)
[771] From cat breeder to Cobol to Perl - (2006-06-21)
[744] We can offer a room, but we can't operate on a dog - (2006-06-05)
[547] Taking the dog for a walk - (2005-12-29)
[96] Variable Scope - (2004-10-22)
[62] Technical Open House - (2004-09-25)
Some other Articles
Melksham manslaughter case concludes - sort ofMonday night, Tuesday nightSimplesDynamically formatting your results (Lua)ourdog is Greyhound, Staffie and Ginger CakeRemembrance Sunday 2009, MelkshamBrown horses in MelkshamPlanning the future of WiltshireMaking Linux Politically correct