[SOLVED] two more beginner lua questions

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
AaronV
Prole
Posts: 16
Joined: Sun Jul 08, 2012 4:11 am

[SOLVED] two more beginner lua questions

Post by AaronV »

#1 how do I do a for loop with a list of numbers i want to use?
i tried something like the following but I'm not sure what i did wrong

Code: Select all

for x in {42,20,6} do
 print x
end
should run three times printing 42, then 20, then 6

not sure I got the "print" part right but other than that...
is that not the code for a for loop over items in a list?



#2 how do I make and use the . operator? i want to work with variables like S.Length.
is this different than S[Length]? Is S[n] for integers only, like arrays?
Whats the difference between S = { this=5 } and S={}; S.this = 5. Can I even do either of those?



I'm sorry to ask such novice questions over the forums, but I can't seem to find any good answers.
Last edited by AaronV on Tue Jul 17, 2012 6:28 am, edited 1 time in total.
What have I gotten myself into?
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: two more beginner lua questions

Post by Santos »

#1:

Code: Select all

for index, value in ipairs({42, 20, 6}) do
    print(value)
end
index and value can be any two names you want. A common idiom in Lua is to use the name _ for values which won't be used.

Also, you if you call a function with a single literal table like in the code above, you can remove the opening and closing parentheses, if you find that easier to read.

Code: Select all

for _, x in ipairs{42, 20, 6} do
    print(x)
end
#2:

S[something] works for strings as well as integers. Be sure to use quotes when using strings. Using the . is simply "syntactic sugar". For example...

Code: Select all

S = {}
S["Length"] = 1000   -- Notice the quotes! Also, because this is preceded by "--", this is a comment.
print( S.Length ) -- This will output "1000".
print( S["Length"] ) -- This will also output "1000". This line is exactly equivalent to the above line.
You might want to check out some Lua tutorials. EDIT: Like Qcode said below this post, this forum is for all questions. I hope this doesn't seem like "read tutorials instead of asking questions!", I think asking questions is perhaps the best way to learn, I just wanted to share some resources which might be able to answer your questions quicker. Just... just thought I'd say this, just in case. :D

Hope this helps! ^^

EDIT: OOPS! :oops: Thanks again Robin, sorry about that, I will try to take more care with my answers.
Last edited by Santos on Mon Jul 16, 2012 3:48 pm, edited 2 times in total.
AaronV
Prole
Posts: 16
Joined: Sun Jul 08, 2012 4:11 am

Re: two more beginner lua questions

Post by AaronV »

okay that helps a bunch but I have a quick follow up question...

what would index be? what would I want to use it as? is it the list?

and i can say

S = {}
S.this = 5

right? cuz that would be awesome.


normally i test these things but I can't just hit compile.
i dunno, maybe i shouldn't rely on forums for easy questions. :roll:
What have I gotten myself into?
User avatar
Qcode
Party member
Posts: 170
Joined: Tue Jan 10, 2012 1:35 am

Re: two more beginner lua questions

Post by Qcode »

This is what the support forum is for, helping with questions, no matter what the difficulty is. Yes the s = {} s.this = 5 works.
Edit: Just saw your comment about the index. The value is whatever it returns as. In Santos's example thats a number though it could be a string or other values. The index will return whatever number this value is. Again in Santos's example the index of 42 would be 1 as its the first value. 20 would be 2 and 6 would be 3.
AaronV
Prole
Posts: 16
Joined: Sun Jul 08, 2012 4:11 am

Re: two more beginner lua questions

Post by AaronV »

so its kind of as if

Code: Select all

for i = 1, 3, 1 do
 ...
end
has an index of 1 and it's in ipairs{1,2,3}?
is that the right way to think about it?

so could i just say 42 instead of index/_?

just like the starting position? and if I wanted to use just the second one, how would i do that?
like if i didn't know what it was, i guess i would have to create a table (how else would i not know what it was) and then use table[2]...

i dunno, i've read a lot of tutorials on tables but i'm still kind of lost on the whole thing. which pretty much is the jist of lua.
What have I gotten myself into?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: two more beginner lua questions

Post by Robin »

Santos, you forgot the "do" for every for loop.
AaronV wrote:so its kind of as if

Code: Select all

for i = 1, 3, 1 do
 ...
end
has an index of 1 and it's in ipairs{1,2,3}?
It has the same effect as

Code: Select all

for i, _ in ipairs{1,2,3} do
 ...
end
if that's what you want to know.
AaronV wrote:so could i just say 42 instead of index/_?
No. 42 is not a variable name.
Help us help you: attach a .love.
AaronV
Prole
Posts: 16
Joined: Sun Jul 08, 2012 4:11 am

Re: two more beginner lua questions

Post by AaronV »

oh my god now i'm even more confused.
sorry sorry sorry i'm super bad at programming i guess!

Code: Select all

a = 1
b = 2
for a,b in ipairs{1.0,2.0,3.0} do
 print (a)
 print (b)
end
would print out
1, 1.0 or
1.0, 1.0 or
1.0, 2?

also does anyone know of an IDE with a debug button for LUA?
What have I gotten myself into?
User avatar
josefnpat
Inner party member
Posts: 955
Joined: Wed Oct 05, 2011 1:36 am
Location: your basement
Contact:

Re: two more beginner lua questions

Post by josefnpat »

AaronV wrote:oh my god now i'm even more confused.
sorry sorry sorry i'm super bad at programming i guess!

Code: Select all

a = 1
b = 2
for a,b in ipairs{1.0,2.0,3.0} do
 print (a)
 print (b)
end
would print out
1, 1.0 or
1.0, 1.0 or
1.0, 2?

also does anyone know of an IDE with a debug button for LUA?
This is what happens when I run your code... (hint hint)

Code: Select all

[~]$ lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> a = 1
> b = 2
> for a,v in ipairs{1.0,2.0,3.0} do
>> print(a)
>> print(b)
>> end
1
2
2
2
3
2
If you want to know what happens here, I can explain it:

basically, you assign b = 2 in a loop that traverses over 1,2 and 3. Here is your output annotated.

1 -- current index from your ipairs
2 -- b
2 --current index from your ipairs
2 -- b
3 -- current index from your ipairs
2 -- b
Missing Sentinel Software | Twitter

FORCIBLY IGNORED.
<leafo> when in doubt delete all of your code
<bartbes> git rm -r *
<bartbes> git commit -m "Fixed all bugs"
<bartbes> git push
AaronV
Prole
Posts: 16
Joined: Sun Jul 08, 2012 4:11 am

Re: two more beginner lua questions

Post by AaronV »

your code says for a,v.
mine said for a,b.

does that mean that mine will only loop twice?

starting number in table, variable representing number in table

right?

or is it starting location in table?
What have I gotten myself into?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: two more beginner lua questions

Post by Robin »

No. Names are names, they don't mean anything specific.
i stands for index.
v stands for value.
You can use any name you want.

If you try this:

Code: Select all

for i, v in ipairs{"x", "y", "z"} do
   print(i, v)
end
It will print:

Code: Select all

1   x
2   y
3   z
Do you understand it now?
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 3 guests