Functions and Tables, Beginner projects to understand components

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Functions and Tables, Beginner projects to understand components

Bill Kelsoe
Hello
My basic understanding of functions is that they do something with values stored in container variables. I want a full list of these functions to read up what are they and what they do. 

For example I guess print is a function. io.read waits user input until Enter is hit on keyboard. Like this. function math does arithmetic using operators like +, -,*,/ and I guess others which I dont know about too. io.write takes the value stored in a variable where io runs it or evaluates it using a function and shoots it on screen post processing. 

where can I read these functions in entirety and examples of their use? Same thing for tables. I know to start a table I use {} but what are its uses in the context of practicality in a program except data entry. 

Also the arbitrary so called type userdata which briefly I skimmed I dont know what is it used for or examples of its use.

Is there a source for all chapters in PIL where the mentioned definitions (array,value,function,metatable,etc..) are merged to form chunks explained head to toe? To the novice people like me? Beginners?

Many thanks
Adel
Reply | Threaded
Open this post in threaded view
|

Re: Functions and Tables, Beginner projects to understand components

Hugo Musso Gualandi


> My basic understanding of functions is that they do something with values stored in container variables.

Functions are a way to give a name to a piece of code that you want to be able to call later more than once.

> I want a full list of these functions to read up what are they and what they do.

The Lua Reference Manual has a description of every function that is provided by the standard library.
 
But you shouldn't need to know this to use Lua. The most important thing is knowing how to create your own functions.

> For example I guess print is a function.

Yes.

> io.read waits user input until Enter is hit on keyboard.

That isn't quite what happens. io.read reads from the standard input stream, and from the point of view of Lua that is just a stream of characters.

What is causing the pause until you press enter is your terminal emulator. Due to line buffering, it only sends the data you type to the Lua program after you press Enter. (This let's you use backspace to fix mistakes as you type)

> function math

Actually, "math" is just a table of functions for organizing common math function. Run type(math) and type(math.sin) in you Lua REPL to check that out.

> io.write takes the value stored in a variable where io runs it or evaluates it using a function and shoots it on screen post processing. 

Functions receive values as input.

When you type something like f(1+2) in Lua, Lua will first evaluate the 1+2 expression and then pass the 3 value to the f function, as a parameter.

Similarly, if you call f(blah), Lua first evaluates the blah expression (reading the value stored in the blah variable) and then passes that value to f. The f function never gets to see the Lua expression or variable names that produced the values it us receiving. It only gets the value and that's it.

> Same thing for tables. I know to start a table I use {} but what are its uses in the context of practicality in a program except data entry.

The Programming in Lua book should have some examples.

> Also the arbitrary so called type userdata which briefly I skimmed I dont know what is it used for or examples of its use.

Userdata is most useful if you are scripting. If you have Lua code that manipulates values originally created in a different language (like C) then those values will be exposed to Lua through user data.

If you are just starting out you probably won't need to worry about user data for now.

> Is there a source for all chapters in PIL where the mentioned definitions (array,value,function,metatable,etc..) are merged to form chunks explained head to toe? To the novice people like me? Beginners

It sounds like you are asking for the Reference Manual.

But I'm not sure its an ideal starting point. It is very dense, and is most useful when you need to search for the precise definition of some Lua construct.

Reply | Threaded
Open this post in threaded view
|

Re: Functions and Tables, Beginner projects to understand components

szbnwer@gmail.com
In reply to this post by Bill Kelsoe
hi there! :)

ive started with pil just as well, but maybe others can give you
anything better or friendlier for beginners, but pil is kinda much
popular, but anyway, check out here the 1st chapter:
http://lua-users.org/wiki/LuaDirectory

you can buy a newer version of pil (than what you can find on lua.org)
for newer versions of lua, however you can follow up differences
between versions here:
https://www.lua.org/manual/5.1/manual.html#7

the refman is the best resource after youve already got the basics
(maybe even before, its so much enlightening, and (one of?) the best
refman(s) ive ever seen):
https://www.lua.org/manual/5.1/ (or whichever version you are using)

for learning userdata others can give anything better than i could,
cuz i use luajit (check it out! :) where is an ffi that can give
anything that userdata can give.
if you want newer features from lua than what 5.1 have* or want to
embed or modify lua, then maybe the original (puc) lua is what you
want, but otherwise luajit can be a very adorable tool for you. mainly
cuz of its speed, however there are more to discover there, and for
getting the best of its huge power, there are some differences in
coding habits, but anything that runs under puc lua 5.1 that will run
under luajit just as well...
[*]: luajit have some partial forward compatibility options that are
needed to be enabled in compilation time if you wanna use those.

look around on lua.org and lua-users.org they are well organized, and
they are great entrances for kinda much anything you can wish for. lua
is minimalist as a language (basically its tools are limited to c99),
but you can find kinda much anything in external modules.

a nice ide, that can help you much:
https://github.com/pkulchenko/ZeroBraneStudio

and dont forget to read always the mailing list, there are so much
great ppl and their great stuffs all around here, theres always much
that one can learn here! :)

good luck, all the bests, and have fun around, cuz you are on the
right place! :D

Reply | Threaded
Open this post in threaded view
|

Re: Functions and Tables, Beginner projects to understand components

Russell Haley
In reply to this post by Bill Kelsoe
Sorry for the top post.

I cannot recommend the Programming in Lua book enough. Learning from the Internet left holes in my understanding of Lua. When I finally read the book, everything was illuminated. ‎It's the best $30 you can spend if you want to learn Lua. 

PIL is easy to read, concise, relevant to beginners and complete.

Russ

Sent from my BlackBerry 10 smartphone on the Virgin Mobile network.
From: Bill Kelsoe
Sent: Wednesday, August 15, 2018 9:44 AM
To: Lua mailing list
Reply To: Lua mailing list
Subject: Functions and Tables, Beginner projects to understand components

Hello
My basic understanding of functions is that they do something with values stored in container variables. I want a full list of these functions to read up what are they and what they do. 

For example I guess print is a function. io.read waits user input until Enter is hit on keyboard. Like this. function math does arithmetic using operators like +, -,*,/ and I guess others which I dont know about too. io.write takes the value stored in a variable where io runs it or evaluates it using a function and shoots it on screen post processing. 

where can I read these functions in entirety and examples of their use? Same thing for tables. I know to start a table I use {} but what are its uses in the context of practicality in a program except data entry. 

Also the arbitrary so called type userdata which briefly I skimmed I dont know what is it used for or examples of its use.

Is there a source for all chapters in PIL where the mentioned definitions (array,value,function,metatable,etc..) are merged to form chunks explained head to toe? To the novice people like me? Beginners?

Many thanks
Adel