Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Chapter 4: For Loops
- What is a for loop?
- It's exactly what it sounds like. It loops over and over and over.
- We just covered tables. Well loops are how you travel through those tables.
- There are 3 different types of for loops and each one has it's own special use.
- Keep in mind there are 2 more 'special' loops that won't be covered in this section.
- These 'special' loops will be covered later since they are not 'for loops'
- You know you're dealing with a for loop when you see the following word:
- [code=lua]for[/code]
- Simple so far huh? Let's keep going ;)
- To truly understand loops, you have to understand the table you are trying to loop through.
- I hope you paid attention to the previous chapter. The structure of the table you are looping through is crucial.
- To start off, we will begin with the generic loop that can be used for any table structure.
- The Generic Loop
- [code=lua]for i = 1, 10 do
- print(i)
- end[/code]
- Prints 1,2,3,4,5,6,7,8,9,10 to the screen.
- Dafuq, I thought you said we were going to loop through a table?!?
- Well it turns out this loop is sooo generic, that you actually can use it to simply.. loop however many times as you want.
- This loop has tons of uses, but the structure is always the same:
- [code=lua]for variable = startNumber, endNumber do
- -- code
- end[/code]
- The variable mentioned simply holds the current number inside the loop. In the earlier example, this was the letter 'i'.
- Each time we loop, the variable updates to the next number.
- Now you may or may not be thinking.. Does it always have to be 1 through 10? NO! It doesn't.
- [code=lua]for i = 5, 50, 5 do
- print(i)
- end[/code]
- Prints 5,10,15,20,25,30,35,40,45,50 to the screen.
- Hold on a sec buddy...you added an extra number. Why yes I did. If you didn't guess from the output, this third number specifies what to increment by.
- Usually it just increments by 1, however if we specify anything else, it will increment by that much.
- So aren't we supposed to be looping through tables or something? Yes we can with this loop.
- [code=lua]local myTable = {'a','b','c','d'}
- for i = 1, #myTable do
- print(myTable[i])
- end[/code]
- Prints a,b,c,d to the screen.
- What is #myTable? This is simply how you get the size of a table. Since we have 4 variables in 'myTable' when we use #myTable, it's returning 4.
- So basically our loop is: 'for i = 1, 4 do'. Cool huh?
- Now... what about 'myTable[i]'? Since 'i' holds the number we're on, it's simply: 'myTable[1],myTable[2]...'
- This is how we loop using The Generic Loop. The next loops are for tables only.
- The Pairs Loop
- What's a pairs? Hah good question. It's variable that has a 'key'.
- Okay... what's a 'key'. Alright buddy, let me just show you.
- [code=lua]local myTable = {user='Syntax',password='123456'}[code]
- Alright so 'user' would be the key and 'Syntax' would be the value. Same for password and 123456.
- If you don't specify a key, the index is a number (how we just accessed them in the last loop)
- WAIT! If they have a key, do they get an index number? No. This is why we cannot use The Generic Loop for key,value tables.
- However don't fret, it's the same exact way to access, just with a string!
- [code=lua]myTable['user'][/code]
- or
- [code=lua]myTable.user[/code]
- Either way will work. The second way is good if you don't have spaces or special characters.
- So this is the loop that makes these magical tables it's bitch.
- [code=lua]for k,v in pairs(myTable)
- print(k .. ': ' .. v)
- end[/code]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement