Giter Club home page Giter Club logo

lua-cheatsheet's Introduction

Welcome to Lua-Cheatsheet



Comments :


Comments in lua are of two types:

  • single line comment

  • multi line comment

single line comment:

A single line comment in lua is given by adding -- to the begining of the line

For example:

-- This is a comment

It is not necessary to leave a space after -- to the make a comment

For example:

--This is also a comment

Lua also does not restrict the number of comments possible for example

-- this is comment 1
-- this is comment 2
-- this is comment 3

multi line comment :

A multiline comment is lua begins with --[[ and ends with ]]

For example:

--[[
    This 
    is 
    a 
    multiline
    comment
]]

Make sure there is no space between -- and [[ in the start of the multiline comment as doing so will cause Error

NOTE: Comments can be place wherever we want for example they can be placed after a meaningful statements

For example:

print("Hello World!") -- This statement will be ignored

Printing stuffs onto screen:

In lua we can print

  • texts

  • variables

printing texts onto screen:

To print text onto screen we need to simply use the print statement in the following format is print(<text to be printed>)

For example:

print("Hello World!")

Note: A string will always be enclosed be quotation marks

We are also not limited to the number of print statements we can have

For example:

print("Hello World!")

Note: A string will always be enclosed be quotation marks

We are also not limited to the number of print statements we can have

For example:

print("Hello World !")
print("My name is AGNEAY B NAIR")

To print multiple text using the same print statement we can use of the below given format

Format:

print("HELLO WORLD","I LOVE LUA")

the above print statement will display the two strings together by seperating them by a tab space

inorder to concatenate two strings using the print statement in lua use .. instead of , in the print statement

For example:

print("Hello world!".." I love lua !")

will concatenate both the strings

Variables and data types:

  • The undefined data types: The nil data type is undefined and empty

  • The number data type: number

  • string data type : all characters enclosed within quotes

  • boolean data type: true and false

  • tables data type: The equivalent of array or list present in different programming languages

    To create a local variable

    local name

   here name is the variable name

lua always explicitly places the nil data type to the variable . Hard to believe ? Let's try it out

local name
print(name)

This would return nil as the variable name stores nil

we can also implicitly give name the value nil as

For example:

local name = nil
print(name)

It is not necessary to define the value in the same line as that in which we define the variable

local name
name = nil
print(name)

we can also use print statement to display the value of expressions

For example:

local x = 3
print(x+8)

To define a global variable

GlobalVar = 3

Here the variable GlobalVar has the value 3 and is globally defined and can be imported in other lua files

inorder to define a global variable to lua more spefically to lua we could write

_G.GloablVar = 3

Here the prefix _G. will make it clear to lua that we intend to create a global variable and not a local variable

NOTE: we need not use _G. always it is completly optional

To define multiple local variable at the same time

local name,age,gender

now we have defined three variables at the same time

we could also give them values as we define them as

local namea,nameb,namec = "ram","lak","shah"

here we have created three variable having same data type , but this is not a necessesity ie we can define variables having different data type in the same line

local name,age,gender = "AGNEAY",18,"MALE"

Getting the length of the string

we can get the length of any string in lua with the help of #

For example:

local x = "AGNEAY B NAIR"
print(#x)

The above code will return the length of string, we can also do it this way

local x = #"AGNEAY B NAIR"
print(x)

To check the type of a given data type

we can simply use type() function provided by lua

For example:

local x = "AGNEAY B NAIR"
local y = 13
local z = true
print(type(x),type(y),type(z))

To convert a number to string:

we can use the tostring() function provided by lua

local x = 18
print(type(x))
local y = tostring(x)
print(type(y)

Escape sequence

Escape sequence meaning
\n newline character
\t tab space
\\ \

String functions

Some string functions are

local name = "Agneay"
print(String.upper(name))
print(String.lower(name))
print(String.len(name))

The String.upper() function will convert all the string letters to upper case and so is the case with String.lower() with the only difference being it will translate the string in lower case and String.len() will return the length of the string

lua-cheatsheet's People

Contributors

agneay avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.