Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ MUSHclient
➜ Lua
➜ Can somebody help me about the string?
|
Can somebody help me about the string?
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
| Posted by
| Doghell
(3 posts) Bio
|
| Date
| Fri 28 Aug 2009 07:36 AM (UTC) Amended on Fri 28 Aug 2009 07:37 AM (UTC) by Doghell
|
| Message
| hello everyone:
i am a beginner of lua. last day i've read a article which mentions that "First, all strings in Lua are internalized;
this means that Lua keeps a single copy of any string. Whenever a new string
appears, Lua checks whether it already has a copy of that string and, if so,
reuses that copy."
[http://www.lua.org/gems/](here is the book).
So yep i just wanna know if i define like this:
>s1 = "hello"
>s2 = "hello"
do s1 and s2 use the same copy? I try to see the address of the two string in the memory, but i don't know how to do it?
(i don't speak english, so excuse me for my poor english,:))
thank you everybody! | | Top |
|
| Posted by
| Nick Gammon
Australia (23,173 posts) Bio
Forum Administrator |
| Date
| Reply #1 on Fri 28 Aug 2009 08:09 AM (UTC) |
| Message
| Why do you want to see the addresses? This is an internal thing that you don't need to know in a scripting language.
However I believe that s1 and s2 in your example will refer to the same string.
This make string assignment fast, eg.
s1 = "some very large string"
s2 = s1 -- copy very quickly, as it takes a copy of the pointer to the string
In your case, if you compare s1 and s2 they will compare equal. ie.
s1 = "hello"
s2 = "hello"
print (s1 == s2) -- true
You can also assign functions, eg.
f1 = function () print "hi there" end
f2 = f1
f2 () -- prints: "hi there"
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | | Top |
|
| Posted by
| Doghell
(3 posts) Bio
|
| Date
| Reply #2 on Fri 28 Aug 2009 09:15 AM (UTC) |
| Message
|
Nick Gammon said:
Why do you want to see the addresses? This is an internal thing that you don't need to know in a scripting language.
However I believe that s1 and s2 in your example will refer to the same string.
This make string assignment fast, eg.
s1 = "some very large string"
s2 = s1 -- copy very quickly, as it takes a copy of the pointer to the string
In your case, if you compare s1 and s2 they will compare equal. ie.
s1 = "hello"
s2 = "hello"
print (s1 == s2) -- true
You can also assign functions, eg.
f1 = function () print "hi there" end
f2 = f1
f2 () -- prints: "hi there"
Year,thank you very much,Nick.I just wanna test that if these two strings refer to the same string(this is why I'd like to see their addresses).When I define two tables,
t1 = {1,2,3} -- this is a table
t2 = {1,2,3} -- and this is another table
they are two different tables.
Just now I have read some words that prove you are right.
"For strings reuse is not necessary, because Lua
does the job for us: it always internalizes all strings it uses, therefore reusing
them whenever possible. For tables, however, reuse may be quite effective."
And thank you again! :) | | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
12,219 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top