Page 1 of 1

Lua help

Posted: Sun Dec 10, 2006 6:53 pm
by xEleas
Tutorials are always helpfull, but nothing compares to live help from a real programmer! For those intressted in Lua, and have questions unanswered by frequent FAQ, google or tutorials, may post here. And developpers, or experienced Lua programmers could answer questions here if they like.
Please, if you ask a question related to a tutorial, quote the part related to your enquiree, and when you answer someone, quoting his question would be nice. Also, make sure your question wasen't answered before posting it, and please, Don't Spam
-------------------------------------------------------------------------------------

Question: When creating a table, we use, for example: "x = { sign = "plus", number = 123, label = "math" }.
print(x.sign) would give us "plus", print(x.number) would give us "123" and print(x.label) would give us "math".
But if we'd want to add a string or number to this table (x), would we have to re-write the whole table inclding our new object (for instance, value = 6)? Because writing simply x = { value = 6 } will erase anything added to that table earlier (sign, number and label) and add value = 6 thus remaining as the only object in the table.
We can construct tables containing other objects, such as the numbers and strings described above, e.g.

> x = { value = 123, text = "hello" }
> print(x.value)
123
> print(x.text)
hello

Question: What is the use of nesting?
Nesting quotes
Only double square brackets allow nesting:

> = [[one [[two [[three]] ]] ]]
one [[two [[three]] ]]

Posted: Sun Dec 10, 2006 7:09 pm
by Nitram
Tables and Nesting quotes are something completly different.

Tables are lists

Nesting quotes are strings.

[[ ]] is a quote of a string as " " is.

But if you want to have a string like this:

Code: Select all

I want "your" soul!
You can't write

Code: Select all

> = "I want "your" soul!"
Because the first quote before your would end the string quote.

You could make this working with:

Code: Select all

> = "I want \"your\" soul!"
or

Code: Select all

> = [[I want "your" soul!]]
So you can't just write " within a string that is markes with such quotes.

With [[ ]] its different.

You can realize such a string:

Code: Select all

I want [[your]] soul!
simply with

Code: Select all

> = [[I want [[your]] soul!]]
Because Nesting is possible with [[ ]]

Was is possible to understand this?

Posted: Sun Dec 10, 2006 7:15 pm
by xEleas
Yes, I understood what you meant. Thanks.

But about the first question, how can you add an object to a table?
Suppose:
priceof = { hat = "50 gold", sword = "100 gold", gleaves = "70 gold" }
> print(priceof.hat)
result: 50 gold
>print(priceof.sword)
result: 100 gold
>print(priceof.gleaves)
result:70 gold

now suppose I'd want to add bow = "50 gold", would I have to re-write:
priceof = { hat = "50 gold", sword = "100 gold", gleaves = "70 gold", bow = "50 gold" }

Or is there a way to add bow = "50 gold" without re-writting all the other objects in that table?

Posted: Sun Dec 10, 2006 7:18 pm
by Nitram

Code: Select all

> = priceof = { hat = "50 gold", sword = "100 gold", gleaves = "70 gold" }
> = print(priceof.hat)
50 gold
> = print(priceof[sword])
100 gold
> = print(priceof["gleaves"])
70 gold
> = priceof[bow] = "50 gold"
> = print(priceof.bow)
50 gold
> = print(priceof.hat)
50 gold
Got it?

Posted: Sun Dec 10, 2006 7:37 pm
by xEleas
I think I get what you mean, but using the little software Vilarion offered, I get bad results..

print(priceof.hat) works
print(priceof[hat]) doesn't work though
print(priceof["hat"]) works.
and
priceof[bow] = "50 gold" doesn't work either..

Edit: I worked it out myself.

priceof = { a = 1, b = 2, c = 3 }

print(priceof.a)
1

print(priceof.b)
2

print(priceof.c)
3

priceof.d = 4

print(priceof.d)
4

=]

Thanks though!

Posted: Sun Dec 10, 2006 8:19 pm
by Nitram
ah... you are right

string list index in [ ] allways with " "

priceof = { a = 1, b = 2, c = 3 }

print(priceof.a)
1

print(priceof.b)
2

print(priceof.c)
3

priceof.["d"] = 4

print(priceof.d)
4

should work too

Posted: Mon Dec 11, 2006 2:45 pm
by xEleas
Thanks ^^

Two new questions:

1- The use of fonctions isn't quite well explained in tutorials.. I didn't really catch on.. maybe I'm dumb.. But any help?
Tutorial wrote: Functions
In Lua, functions are assigned to variables, just like numbers and strings. Functions are created using the function keyword. Here we create a simple function which will print a friendly message.

> function foo() print("hello") end -- declare the function
> foo() -- call the function
hello
> print(foo) -- get the value of the variable "foo"
function: 0035D6E8

Notice we can print the value of the variable foo and it displays (like tables) that the variable is a function, and has unique identifier for that particular function. So, being a variable just like any other, we should be able to assign functions to variables, just like the other variables, and we can.
> x = function() print("hello") end
> x()
hello
> print(x)
function: 0035EA20

2 -
Code wrote: a = 1
b = 1
c = 1
In the following example, typing:
print(a == b)
would give us " true ". Which is normal
But typing:
print(a == b == c)
would give us "false".. Why? I can't understand that..

Thanks for the help ^^

Posted: Mon Dec 11, 2006 5:10 pm
by xEleas
Thanks to Vilarion:
code wrote:a = 1
b = 1
c = 1

a == b
True.
So the statement " a == b " is True.
so when we ask
a == b == c, means we're asking True == c, True == 1, which is false.