首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Programming in Lua(一)Getting Started

2012-11-03 
Programming in Lua(1)Getting StartedProgramming in Lua(1)Getting Started1. Getting StartedI can run

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/


热点排行