Jump to content



Photo

Lua Tutorial


  • Please log in to reply
10 replies to this topic

#1 Tchernoben

Tchernoben

    Casio Fan

  • Members
  • PipPip
  • 42 posts
  • Location:Nice
  • Interests:Calculators, Pc, mangas, warhammer.

  • Calculators:
    Classpad 300
    Ti 89 Titanium

Posted 15 May 2006 - 05:51 PM

Hello

Where shall I found out a lua tutorial in FRENCH plz ?
Did someone found some translation ?
As far as I am concerned, I only want to have some infos about Object and metatables.

thx for reading

#2 The_AFX_Master

The_AFX_Master

    Casio Overlord

  • [Legends]
  • PipPipPipPipPipPipPip
  • 519 posts
  • Gender:Male
  • Location:Black Mesa Research Facility (sector C)
  • Interests:BASIC +FORTRAN 90+ C++.....and HALF LIFE

  • Calculators:
    Casio Algebra FX 2.0 Plus, Casio fx 570 ms, Classpad 300, And a crowbar

Posted 25 May 2006 - 02:06 PM

Read this book, would help you with metatables Programming on LUA

#3 The_AFX_Master

The_AFX_Master

    Casio Overlord

  • [Legends]
  • PipPipPipPipPipPipPip
  • 519 posts
  • Gender:Male
  • Location:Black Mesa Research Facility (sector C)
  • Interests:BASIC +FORTRAN 90+ C++.....and HALF LIFE

  • Calculators:
    Casio Algebra FX 2.0 Plus, Casio fx 570 ms, Classpad 300, And a crowbar

Posted 26 May 2006 - 03:10 PM

The people interested on LUA is begining to raise...then
I would use this topic correctly, what about a fast start tutorial?.... SEE BELOW :D

1.- Basic things about LUA

Lua is and ADD-IN that you install into your calc using add-in installer (download at classpad.net)).. This new programminglanguage for your calc is the best way to do serious programming in your calc., forget basic...it?s worst!.. Also thanks to Orwell for the Lua developement

2.- Getting started
You must install the add-in. then tapping over the icon you in on the main interface. You see New file, Open File, Run script (that is Run file), and interactive

A basic Lua (composed by one unique script or file) program have 4 parts

a.-File header: contains the sentence require() to call external resources
b.-Variable initialization: On Lua you not need to declare all variables, but is a good programming rule to initialize some important variables
c.- Function declaration: You must write ALL the functions that you can use before the main body of the program
d.- Main body of the program

3.- Your first code :plol:

to be continued..... I will write a big tutorial here, every day a bit more :D ..

#4 Orwell

Orwell

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 777 posts
  • Gender:Male
  • Location:Paris - France

  • Calculators:
    Casio AFX 1.02 / Casio ClassPad 300

Posted 26 May 2006 - 04:00 PM

Great idea :)

Perhaps some other volunteers will participate/help in the redaction :D
I could help too, but I'm currently busy with this year's examinations :rolleyes:

#5 PAP

PAP

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 681 posts
  • Gender:Male
  • Location:Somewhere in Europe.
  • Interests:Computer Algebra, Numerical Analysis.

  • Calculators:
    ClassPad 300 (plus an old Casio model, with only a few Kb ram).

Posted 26 May 2006 - 11:06 PM

Great idea :) Perhaps some other volunteers will participate/help in the redaction :D
I could help too, but I'm currently busy with this year's examinations :rolleyes:

Great idea, indeed. I'm in as well, although I'm also busy these days.
I don't think we can use a "wiki" page in this forum, but, even in the form of an "always-growning" post, it will help novice CPLua users.

The people interested on LUA is begining to raise...then
I would use this topic correctly, what about a fast start tutorial?.... SEE BELOW :D

Just a small remark: a function can be declared within the "main program part". It is not necessary to declare all functions before the main program, although functions are usually defined before any other Lua statement. Strictly speaking, there is no "main" program, or inversely, everything is in the main program. But this may confuse the novice Lua programmer, so probably AFX_Master decided to skip all these details for the sake of simplicity...

#6 The_AFX_Master

The_AFX_Master

    Casio Overlord

  • [Legends]
  • PipPipPipPipPipPipPip
  • 519 posts
  • Gender:Male
  • Location:Black Mesa Research Facility (sector C)
  • Interests:BASIC +FORTRAN 90+ C++.....and HALF LIFE

  • Calculators:
    Casio Algebra FX 2.0 Plus, Casio fx 570 ms, Classpad 300, And a crowbar

Posted 28 May 2006 - 09:18 PM

Ok PAP, suggestions stored! :D

going on the tut....


I wouldn`t start this lesson with a "Hello World" example. Because on LUA, you can do a lot more without know hundreds of functions, or without reading alot of documentation.

The first thing to do is read the CPLua documentation, the readme that comes with the last CPLua version on the RAR file that you have downloaded. In this brief, but whole .txt you have the syntax of all the common functions of CPLua. This functions are for the Classpad version. Because Lua are a multiplatform language (it`s derived from C/C++ :D ), it have a lot of more proper functions that aren?t shown in the CPlua?s readme. Then you need the official LUA 5.1 documentation HERE in order to know more functions.

If you hav?nt programming knowledge, the work for you must be harder.. I would try to do this tut easy to understand... lets go with coding!
********************************************************************************
*

2- Your first program

The manner in that you can code on Lua is flexible, but i?m focusing this tutorial on a structured programming, with 3 basic parts

2.1.- Script header

At the beginning of a program, you can need some external resources, or function packages. to call this packages and make them accesible for your progs. This ids done with the require() function. By the moment only we are working with the standard CPLUA packages, to create your owns...waita a bit! :D and continue reading

The principal packages (i`ve used) are:

.- The "draw" package: allows you to perform any graphing activity on the classpad`s screen, you can draw squares, lines, circles, pictures, text, and a lot of basic drawin functions

.- The "string" Package:this package contains certain functions to manipulation of string. If you don`t know, what is a string, it?s a chain of charachters .. so simply.. in LUA you denote a string by enclosing it in " " . the entire text that you`re reading now is a big string with a lot of characthers. . now and the numbers?? this man forgot the numbers?.. no, i did`nt. Numbers are strings both, but LUA converts it into numbers to do mathematical operations with then.. for example, if you type print(10+1) you get on the screen 11, also if you type print("10"+1) (the string "10" and the number 1), then you get 11 again.. So powerful!

.- The "cas" package: This ables you to perform CAS operations inside Lua, as integration, differentiation and so on

2.2 Variable initialization

On Lua you not have to declare variables, You have no need of say to the calc "this is an integer", "This is a matrix, table,list.." and so on". if a variable doesn`t exist, they have the value nil, that is an empty value.. Note that you have no need to define the variables at the top of the script, you can initialize along the code, in any part. here you can initialize the PRINCIPAL OR MOST IMPORTANT variables of your prog, or the variables that must be constants over the code (it`s so clear)
There are two types of vars: locals and globals
.- A local variable exists only under in a confined space (as a function in example), when the program execution reaches this zone the variable will be deleted. Local variables need a explicit declaration
.-A global variable are on all the script (remember that scripts are pieces executable code. in one piece as a document in microsoft Word :D ), you can modify it along ALL the script, and it is accesable along the script

-- in CPLua, comments are denoted with double dash
-- this comments are`nt executable codes, the interpreter ignores them.

-- This is the example # 1:  VARIABLE INITIALIZING

require("cas")		 -- this packs aren?t needed in this code, but you can see how to call them
require("draw")
require("string")

-- globals:

var=0
var_1,var_2 = 0,123	-- You can initialize multiple vars separating by commas
a,b,c = 1,2,3

t=12
T=15	   -- Note that Lua are case sensitive, variables in uppercase are different 
			  -- than the lowercase ones
L={}	   -- declaring a simple empty table 

-- Locals

local h=0.5			 -- using decimals
local z=1e-5		   --scientific notation
local z,h=0.5, 1e-5  -- multiple definitions by commas

function func()	   --don`t pay attention to the function, only see that  
local var =12		 --the function are a "confined" space....
return
end							

--and the local variable "var" inside the function are different to the 
--global var declared at top
-- althought they have the save name, they are totally independent

An important thing is the math functions package. they have no need to use require(). use the functions that are described on the LUA 5.1 official documentation.
example x=math.sin(0) this returns x=0


Useful links: Lua 5.1 documentation, manual

#7 The_AFX_Master

The_AFX_Master

    Casio Overlord

  • [Legends]
  • PipPipPipPipPipPipPip
  • 519 posts
  • Gender:Male
  • Location:Black Mesa Research Facility (sector C)
  • Interests:BASIC +FORTRAN 90+ C++.....and HALF LIFE

  • Calculators:
    Casio Algebra FX 2.0 Plus, Casio fx 570 ms, Classpad 300, And a crowbar

Posted 10 September 2006 - 08:27 PM

3- Control structures on LUA

Now, we're to learn how to do some structures on CPLua

3.1 Conditionals:

- "If" statement

when you need to decide what to do when some condition is present, IF statement do the job. the syntax is the following:

if  condition then
-- code block when "condition" is true
else
-- this is optional, when  "condition" is false
end

How to write a condition, The condition usally is a relationship between variables using relational operators:

more than >
less Than <
More or equal than >= (NO =>)
less or equal to <=
Equal ==

when you evaluate these relational expressions, they return true or false.

There is an important point DO NOT CONFUSE = (ASSIGN EQUAL) WITH == (COMPARATIVE OR RELATIONAL EQUAL

example

a=2  -- assigns 2 as value of a
a==2  --returns true
a==3 -- returns false

2=1 -- error
2==1 -- False

You can use the logical operators AND, OR when multiple conditions need to be evaluated
then you must know, if a and b are relational expressions (c==4,height>6, and so )

when a is true and b is true, a AND b is true
when a is true and b is false a AND b is false
when a is false and b is true a AND b is false
when a is false and b is false a AND b is true



when a is true and b is true, a OR b is true
when a is true and b is false a OR b is true
when a is false and b is true a OR b is true
when a is false and b is false a OR b is false

[/b]
(do not confuse and with AND, AND is the operator)

:!: ON CPLUA, AND and OR ARE WRITTEN WITHOUT CAPS, I'M USING IT IN CAPS TO DIFFERENTIATE THE NORMAL WORDS!---> and, or

now, here we go to some examples:

a=3
b=4
c=5

if a==2 then
-- nothing, this condition is false
else
print("How to use else!")
end

If a==3 and b==4 then
print("when a is true and b is true,  a AND b is true")
end

if a==3 or c==6 then ---the first, true the second, false
print("when a is true and b is false  a OR b is true")
end

Now, suppose that you need to go across a matrix. how?, repeating is the solution:

3.2 Iterators:

3.2.1 FOR

FOR is an iterator that repeat in a finite number of steps. FOR modifies the value of a variable that we shall to call "counter", from a start to an end
You must use for when you know how many repetitions you want to do

the syntax is the following:
-- for counter=start, finish, step do		the step is optional, by default is 1
-- block code
--end

-- counting from 1 to 10
for c=1,10  do
print(c)
end

--Launch a rocket!
for c=10,0,-1 do
print(c)
end
print("loop ended")


3.2.2 REPEAT

Repeat is an iterator that performs loops until a condition becomes true

syntax:

-- Counting, but using repeat now
c=1
repeat
print(c)
c=c+1
until c==10

-- This loop will go until c equals 10, it's equivalent to
-- note that c is initialized OUT OF THE LOOP!  initializing into the loop will be a infinite loop 
-- because c will never equals 10

for c=1,10  do
print(c)
end
-- now you see, if you need to loop over a finite space, with known dimension, use for instead of repeat


#8 betoe

betoe

    UCF Spanish Translator

  • [Legends]
  • PipPipPipPipPipPipPip
  • 846 posts
  • Gender:Male
  • Location:Guadalajara/Mazatlan, Mexico.
  • Interests:Electronics, SW development, automotive. Swim->bike->run

  • Calculators:
    Algebra FX2.0 (R.I.P.), Classpad 300

Posted 10 September 2006 - 08:41 PM

go ahead with this, seems very nice. Finish it and it would be nice to put it as one new topic, make it sticky.


Also if someone thinks something isnt clear, post it here and we can edit it if need some change.

#9 The_AFX_Master

The_AFX_Master

    Casio Overlord

  • [Legends]
  • PipPipPipPipPipPipPip
  • 519 posts
  • Gender:Male
  • Location:Black Mesa Research Facility (sector C)
  • Interests:BASIC +FORTRAN 90+ C++.....and HALF LIFE

  • Calculators:
    Casio Algebra FX 2.0 Plus, Casio fx 570 ms, Classpad 300, And a crowbar

Posted 12 September 2006 - 01:33 PM

Well, going with the tut:

4-Data types on LUA

Lua, as other programming languages, have certain data types to fill the requirements of modern programming. in basics, Lua common data types are:

-Numbers
-Strings
-tables
-Functions

Now, a brief explanation about some types:
Numbers: The common values, to erase any variable you can assign the value nil.

-Strings: are chains of characters, to build one, you need to enclose it into ""
i.e: "this isa string"
numbers and strings with the same characters are different, so "1234" is different from 1234

-Tables. tables are THE data types on lua, they are universal arrays with properties that differs from the traditional concept of a matrix as array.

the basic constructor for a table is {}. You can build n-dimensional arrays with this technique. you need to think now that the basic list or table {} is the basic block to build any array on lua
For matrix manipulation, the most common task related to math/engineering programming, you can buid a matrix doing a "list of lists", you build first the main list (the matrix), then insert every row (lists into the mother list, the matrix)

some examples:

-- declaring a table:

table={}

-- suppose that i want to morph table into a matrix with 4 rows

table=[color=#FF0000]{[/color]{},{},{},{}[color=#FF0000]}[/color]
-- red : main list
-- black: sublists (rows in this particular case)
-- this matrix:

1 2 3
4 5 6
7 8 9

-- can be built as:

matrix={{1,2,3}{4,5,6}{7,8,9}}

now, how to call, or operate over a specific table value?, indexing is the answer. indexing is to give a position to any value referred to the table. on tables you can store any type of lua value (functions included!!!!)

in a ordinary matrix, the element 1,1 is the row 1, col 1. On Lua, tables can be indexed with numbers (as ordinary matrix, or Fortran basic arrays), or with strings. This is a very powerful tools in order that you can create packages using tables

if table is a simple table {}
to index via numbers: table[number]=value
to index via strings: table.string=value

Some examples

--creating a list of things using string indexing:

things={}

things.calc=3
things.numbers="this is a string"

--or using default number indexing

things={}

things[1]=3
things[2]="this is a string"

--you can add a inner list (as a matrix row)

things[3]={}

-- then you have that things look like: {3,"this is a string",{}}


#10 The_AFX_Master

The_AFX_Master

    Casio Overlord

  • [Legends]
  • PipPipPipPipPipPipPip
  • 519 posts
  • Gender:Male
  • Location:Black Mesa Research Facility (sector C)
  • Interests:BASIC +FORTRAN 90+ C++.....and HALF LIFE

  • Calculators:
    Casio Algebra FX 2.0 Plus, Casio fx 570 ms, Classpad 300, And a crowbar

Posted 13 September 2006 - 07:52 PM

Multi-dimensional tables in LUA.

Forget the matrix idea of arrays now!.. A matrix is a seriously capped thing, only reliable for maths. Arrays are more extensive and universal definition of "How to order variables with some common characteristic"

With LUA, table size is a "empiric" value, there is no rigurous table dimension protocol*. In a matrix, the dimension is known. is a fact, you can measure it.on LUA, surely you will need to handle te dimensions for your tables with control variables.

* putting # before a table variable, you get the dimension. but i think that is the quantity of non nil elements ( :!: to confirm)

The key is, multidimensional arrays are tables ({}) filled with tables ({{},{},{}}), that are filled with more tables {{12,{2,3,4}},{{a,b},{78,96}},{1,2}}, and so on to the infinity.

Now, i will show how to call values on large and complicated arrays, suppose that i will fill a "cubic" matrix, this is a matrix on three dimensions. imagine it as a ordinary matrix, that have lists instead of numbers!!

-- A cubic matrix:

cubic= {}  -- the first table constructor

--suppose the cubic matrix of 2x2x2  (a 2 x 2 matrix with 2 element lists instead each number)
-- for explain, i'm doing it by hand, but, in real practice, you need to use some FOR loops in order to put every sub-table and sub-sub-table (...) into the main table constructor {}

cubic={{},{}}  -- first step, a 2D matrix
cubic={{{}}, {{}}} -- Final step.

How to do it more efficient?. here, the real point of view
In the last post, we see how to call values via their index in a simple {} table. but, how i can extend this definition to multidimensional arrays?. so simple, concatenate it!
You have a table {}, to get the n element of the table, you call it using the number index [n]. now, what happen if the element n of the table is another table? (ie, a matrix row).. sure you need to can the element m of this table, then these element is: [n][m], if the element m is a table and you need to seek a element k into: [n][m][k]. see this example to get the idea

-- the 2x2x2 cubic matrix:

{1,2}  {3,4}
{5,6}  {7,8}

--now, to get elements:

cubic={{{1,2},{3,4}},{{5,6},{7,8}}}  --built by hand

print(cubic[1][2][1]) 
-- you'll get: 3, why?
-- first index: cubic[1], the first element of cubic: {{1,2},{3,4}}
-- second index: cubic[1][2] the second element of the first element of cubic: {3,4}
-- third index: cubic[1][2][1]= 3, first element of {3,4}
-- Easy! only expand your mind a bit

--The same with strings indexing   a.b.c   is the c element of the b element of the table a
you can mix these notations, if you have an array with caracteristics of some calcs

name, model number
you can write an array that order these things:

calcs={}

calcs.name={"Classpad","AFX","CFX"}
print(calcs.name[1])  -- mixing notations


-- you get: Classpad (string)

:!: Important Things to know:

1) On lua, you can't copy tables directly
In LUA, you can't assign the contents of a table, to another table independently. I will explain: When you do an assign with tables, really you don't make a copy of the table to transfer it to the variable that will receive the copy, see this code:
-- with numbers:
a=3
b=a
a=2

-- Here, First a equals 3, b is a copy of a, with their own name ( b ), and then i modify a, a equals 2. if you print the results, you will see that the modification of a, is totally independent from b, because this value was copied from the original. NOT REFERENCED (POINTED)TO THE ORIGINAL!

-- Initializing a table

tbl1={1,2,3}

tbl2=tbl1
-- if you print the elements of the tbl2, you will see that all remain equals as tbl1 elements.
-- now, you can modify the original table tbl1

tbl1={1,9,0}
-- automatically, you will see (print the results), that tbl2 equals the modified version of tbl1, why?

This is because when you try to assign the value of a table into another table on LUA, you get a pointer.
Pointers are variables that makes references to another variables in any time. when you modify the value of a variable, pointed by another variable.. the pointer gets the real value of the pointed variable.
If you want to copy a table (ie, to make a backup before operate it). you need to use the LUA function table.copy(). further details later, in the functions chapter.

2) Be careful with the indexing!, if you try to index a nil....

... you will get an error saying that you're triying to index a number, or a nil. example

array={}  -- initialize array

array[10]= "a string"  -- that's correct
array[10][1]="do no commit errors"  -- ERROR!, your'e trying to index a number
array[10]= {} -- first initialize array[10],then fill it
array[10][1]="do no commit errors"  --Thats the correct way!

-- indexing nil's

array={}  -- initialize array
array[10][1]="do no commit errors"  
-- ERROR, YOU MUST INITIALIZE EVERY SINGLE DIMENSION TABLE WITH THE TABLE CONSTRUCTOR {}
-- THE SAME WITH STRING INDEXING

Now, catch up your classpad and try some operations with tables, remember the table constructors {}, and how to index [][][][].....

Remember that you can assign values using index! this is quite useful when you need to initialize some large arrays

-- the 2x2x2 cubic matrix:

{1,2}  {3,4}
{5,6}  {7,8}

--now, to get elements:

cubic={{{1,2},{3,4}},{{5,6},{7,8}}}  --built by hand
-- replacing numbers...

cubic[1][2][1]= 5

-- you catch: 

{1,2}  {5,4}
{5,6}  {7,8}

cubic[1][2]={}

-- you catch:
{1,2}  {}
{5,6}  {7,8}
arrays could be shapeless!!!


#11 Orwell

Orwell

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 777 posts
  • Gender:Male
  • Location:Paris - France

  • Calculators:
    Casio AFX 1.02 / Casio ClassPad 300

Posted 13 September 2006 - 08:30 PM

More simply, a Lua table is a structure that can associate any kind of value to any kind of key ;) It's not a surprise to have a table inside another table then.

* putting # before a table variable, you get the dimension. but i think that is the quantity of non nil elements ( :!: to confirm)

According to Lua 5.1 Manual,

The length of a table t is defined to be any integer index n such that t[n] is not nil and t[n+1] is nil; moreover, if t1 is nil, n may be zero. For a regular array, with non-nil values from 1 to a given n, its length is exactly that n, the index of its last value. If the array has "holes" (that is, nil values between other non-nil values), then #t may be any of the indices that directly precedes a nil value (that is, it may consider any such nil value as the end of the array).






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users