Ooh! Nice!"222"+(2+2)
The above is js btw. Because the first bit is a string it will instead of simply adding the numbers together make it 2224
someTable={"test","some other value" ,"the last value"}
for key, value in pairs(someTable) do
print("The key is: ",key," the value is ", value)
end
someOtherTable={"you didn't expect this", "did you?" }
function printTable(tableToPrint)
for key,value in pairs(tableToPrint)
print("The key is: ",key," the value is ", value)
end
end
printTable(someTable)
printTable(someOtherTable)
function printTable(tableToPrint)
function add(value1,value2)
combination=value1+value2
return combination
end
someNewValue=add(2,3)
print(someNewValue)
Right, OK, I think I followed most of that? I'm not very bright - I feel I should say this So when you write "function add(blah1,blah2)", what is that doing? That's not a mathematical addition is it? Is it adding variables to a generic function? And then you name the function "combination" by stating that it has the values established in the line above?2226 ok. Let me try to explain it
First, lets explain why you would want to use them. Lets say you have a table
Now, lets say you want to loop over it and print out the key and the value. You would use something like thisCode:someTable={"test","some other value" ,"the last value"}
It is at this moment that you discover that you also have another tableCode:for key, value in pairs(someTable) do print("The key is: ",key," the value is ", value) end
Just like before you want to loop over it but you recognize that copying the code isn't exactly a good way to program. This is where functions come in, as a function is basically a bit of code that you can run any time you want. You already know how to run functions, as this is the same as using for example print()Code:someOtherTable={"you didn't expect this", "did you?" }
So, lets write the code in such a way that it uses a function.
Code:function printTable(tableToPrint) for key,value in pairs(tableToPrint) print("The key is: ",key," the value is ", value) end end printTable(someTable) printTable(someOtherTable)
So, lets break it down
the word function tells our program that we want to create a function. We then give it a name, in this case printTable. After this come the variables that the function gets when we run it. You write these always inside the "()".Code:function printTable(tableToPrint)
This is the code that we want to run. Note that instead of someTable like before I now use tableToPrint. This is because tableToPrint contains the value that we passed to the function and thus it works with both "someTable" and "someOtherTable".Code:for key,value in pairs(tableToPrint) print("The key is: ",key," the value is ", value) end
We then tell our program that our function is done with the final end. Much like how you tell your program that the loop is done or the if statement is done.
After we made it we simply call it. Again, this is done just like every other function like for example print.
The return statement is a way to have our function return data. Just like math.random() would. Lets write a very basic function
And if we want to run it we simply do the followingCode:function add(value1,value2) combination=value1+value2 return combination end
Code:someNewValue=add(2,3) print(someNewValue)
Hmm good point, thank you chap - I'm just trying to get my head around the syntax and stuff that's used - I think you're right, I need to just take myself through some basic steps and learn as I go along. Last question I promise: does the grammar and syntax of the ComputerCraft OS differ from basic Lua, or can I have a go at programming in there, in-game?when I wrote function add(value1,value2) I used the comma to separate the 2 variables that his function will get.
When I did
combination = value1+value2
I added the values of value1 and value2 together and stored it inside the variable combination
Best way to learn programming in my eyes at least is to experiment yourself. Knowing the theory about something is nice but if you never used it it is kind of useless. This is especially true when it comes to programming.
There is no difference in the syntax. There are some differences between "normal" lua and CC lua though but that is mostly in that CC has more commands, for example the whole redstone api.2229 (I'm going to assume a 2228 at the start of yours @lenscas)
Hmm good point, thank you chap - I'm just trying to get my head around the syntax and stuff that's used - I think you're right, I need to just take myself through some basic steps and learn as I go along. Last question I promise: does the grammar and syntax of the ComputerCraft OS differ from basic Lua, or can I have a go at programming in there, in-game?
Ah great, OK! I'll do that then See, I have heard about the os.pullEvent() ability! Thanks Lens, I appreciate it!2230
There is no difference in the syntax. There are some differences between "normal" lua and CC lua though but that is mostly in that CC has more commands, for example the whole redstone api.
The biggest differences are that doing something like
while true do
{some code that takes no time to execute}
end
will eventually stop executing on CC. To prevent this from happening use sleep() it takes a number and it will stop for that many seconds. (decimals are allowed). The sleep function is also CC only.
Another difference is the fact that you have access to os.pullEvent() which is super useful when writing code for CC .
It basically stops the program from running until an "event" happened, this can be a change in redstone, a message over rednet or a lot of other things.
But even though these differences exist I still recommend to start with something like CC as programming is something that you learn by doing and Computercraft gives plenty of reasons to program
That syntax would also work the same way in Java, by the way. "222"+2+2, on the other hand, would come out to "22222"."222"+(2+2)
The above is js btw. Because the first bit is a string it will instead of simply adding the numbers together make it 2224
I kind of expected java to throw shit at you for trying to add an integer and string together. Didn't know it would act the same as JS. Which is still an horrible way to work in my eyes. Just separate the concatenation and adding into separate symbols, gives a lot less confusion and bugs2232
That syntax would also work the same way in Java, by the way. "222"+2+2, on the other hand, would come out to "22222".
*shrug* Well, Java does use + for string concatenation as well as addition; there's not much else to say. It also automatically .toString()'s anything you try to add to a string.2233
I kind of expected java to throw shit at you for trying to add an integer and string together. Didn't know it would act the same as JS. Which is still an horrible way to work in my eyes. Just separate the concatenation and adding into separate symbols, gives a lot less confusion and bugs