读书人

Programming in Lua(一)Getting Start

发布时间: 2012-11-03 10:57:42 作者: rapoo

Programming in Lua(1)Getting Started
Programming in Lua(1)Getting Started

1. Getting Started
I can run my first hello world application with command line
>lua hello.lua
print("hello, carl")

Another example just like the C programme
function fact(n)
if n==0 then
return 1
else
return n*fact(n-1)
end
end

print("enter a number:")
a = io.read("*number")
print(fact(a))

1.1. Chunks
A chunk is simply a sequence of statements.
Semiclolons separate two or more statements, but this is just a convention.

We can aslo type the command >lua to enter the interactive mode, ctrl + c to exit.

The first way to link a chunk
>lua -la -lb
a means a.lua file, and b means b.lua file.

The second way to link a chunk
--file 'lib1.lua', I put the fact(n) function in that lib file.
D:\work\lua>lua
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> dofile("lib1.lua")
> n = fact(3)
> print(n)

1.2. Global Variables
print(b) ----> nil
b=10
print(b) ----->10

1.3. Some Lexical Conventions
The following words are reserved; we cannot use them as identifiers:
and break do else ...

Lua is case-sensitive: and is a serverd word, but And and AND are two other identifiers.

A comment starts anywhere with
A double hyphen(--).
Block comments with --[[ ]]--.

print("test------") --test
--[[
test for fun
]]--
print("test end --")

1.4. The Stand-Alone Interpreter
..snip..

references:
http://www.lua.org/pil/


读书人网 >编程

热点推荐