lua-users home
lua-l archive

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


Of course a and b will be different.
What Nick Trout wrote means, Lua uses the same storage for identical strings, so it does not waste memory for several identical strings. Although the variables may therefore refer to the same memory location, they are still independant.
----- Original Message -----
Sent: Thursday, May 08, 2003 10:17 AM
Subject: SV: Deep Copy prototype

> >Thanks for the clarification. I meant to use:
> >
> >if T == "string" then return string.gsub( A, "(.*)", "%1")
>
> Which still does not do what you want. It is not possible to produce 2
> different copies of the same string in Lua.
>

I have some thoughts/wonderings regarding this..
What happens if we change the contents of the returned string, which
then would be a reference to original ( if I've not come to the wrong
conclusion ).

Ex ( not usefull as is, but to illustrate the idea ):
===8<---
do
a = "test string"
b = string.gsub( a, "(.*), "%1" ) -- if this is the whole string, why not
simply use "b = a" ?
b = string.gsub( b, "t", "p" ) -- replace all t's with p's ( was my
intention any way )
-- now I would expect b to be "pesp spring" and a to still be "test string"
end
--->8===

I'm tinking about what Nick Trout wrote:

NT>Both variables reference the same string object as long as the strings
NT>they are supposed to represent are the same value. If you assign a new
NT>value to a variable it will not change both values.

I should've run this simple test myself, but I haven't Lua installed here
atm..

Thank you,
Andreas