View previous topic :: View next topic |
Author |
Message |
Dr.Disrespect Grandmaster Cheater Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Sat Aug 11, 2018 11:11 am Post subject: [LUA]Questions about _G. |
|
|
Code: |
local x = "some string..."
_G[x] = 5print(_G["some string..."])
|
If I build this, the output is "5".
Code: |
local x = "some string..."
_G[x] = 9087print(_G["some string..."])
|
If I build this, the output is "9087".
Why? I don't understand the second line of the code, how come there is a number in front of "print"? Is that a function of some sort? If not, what is it? It doesn't make any sense to me....
Thanks.
_________________
**************
A simple example is better then ten links. |
|
Back to top |
|
|
TheyCallMeTim13 Wiki Contributor Reputation: 50
Joined: 24 Feb 2017 Posts: 976 Location: Pluto
|
Posted: Sat Aug 11, 2018 11:32 am Post subject: |
|
|
What are you trying to do exactly?
Code: | local x = "some string..."
_G[x] = 5print(_G["some string..."]) |
with this you set "_G[x]" to the value of 5, and the first time it's ran you'll just print "nil".
And with the other you do the same but set it to 9087 instead of 5. It's doing exactly what you told it to do.
If you change the code a bit:
Code: | local x = "some string..."
_G[x], s = 5, print(_G["some string..."])
print(_G[x])
print(s) |
The first time it's ran you'll get.
And the next time it's ran you'll get.
Because you just set every thing to 5, for whatever reason.
This "_G["some string..."]" is the exact same variable as "_G[x]" in your code.
SO this:
Code: | local x = "some string..."
_G[x] = 5print(_G["some string..."]) |
Is the same as this:
Code: | local x = "some string..."
_G[x] = 5print(_G[x]) |
_________________
Last edited by TheyCallMeTim13 on Sat Aug 11, 2018 11:42 am; edited 1 time in total |
|
Back to top |
|
|
ParkourPenguin I post too much Reputation: 140
Joined: 06 Jul 2014 Posts: 4289
|
Posted: Sat Aug 11, 2018 11:40 am Post subject: |
|
|
It's how Lua parses tokens. "9087print" isn't a valid number, so the parser splits it up and interprets "9087" as a number and "print" as an identifier. The identifier doesn't make sense if it was part of the assignment, so the parser will interpret that as two separate statements: an assignment and a function call.
Use Google for information on what _G is. (_ENV may also be relevant)
_________________
I don't know where I'm going, but I'll figure it out when I get there. |
|
Back to top |
|
|
Dr.Disrespect Grandmaster Cheater Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Sat Aug 11, 2018 11:55 am Post subject: |
|
|
@TheyCallMeTim13
@ParkourPenguin
Thank you both for the quick answer. I get it now. Isn't it a little weird that Lua parses tokens this way? xD
+rep if I can
_________________
**************
A simple example is better then ten links. |
|
Back to top |
|
|
TheyCallMeTim13 Wiki Contributor Reputation: 50
Joined: 24 Feb 2017 Posts: 976 Location: Pluto
|
Posted: Sat Aug 11, 2018 12:06 pm Post subject: |
|
|
I thinks it's why most languages (if not all, just not sure), don't allow variables to start with numbers.
So:
Isn't valid.
But this is:
_________________
|
|
Back to top |
|
|
ParkourPenguin I post too much Reputation: 140
Joined: 06 Jul 2014 Posts: 4289
|
Posted: Sat Aug 11, 2018 12:12 pm Post subject: |
|
|
It's a consequence of not requiring an explicit token to end statements (e.g. semicolon in C / C++). Lua could make a rule that disallows this, but if it's not hurting anything, why bother?
_________________
I don't know where I'm going, but I'll figure it out when I get there. |
|
Back to top |
|
|
|