{"id":26011347,"url":"https://github.com/agneay/lua-cheatsheet","last_synced_at":"2025-06-25T02:07:58.332Z","repository":{"id":232398739,"uuid":"784245310","full_name":"agneay/Lua-Cheatsheet","owner":"agneay","description":"A simple cheat sheet for lua programming language","archived":false,"fork":false,"pushed_at":"2024-04-13T08:10:53.000Z","size":9,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-25T02:07:58.090Z","etag":null,"topics":["cheatsheet","lua","lua-cheatsheet"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/agneay.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-04-09T13:26:00.000Z","updated_at":"2024-04-09T13:37:22.000Z","dependencies_parsed_at":"2024-04-13T09:28:27.019Z","dependency_job_id":"e47c5ed4-ad09-439d-8236-e38fd1235526","html_url":"https://github.com/agneay/Lua-Cheatsheet","commit_stats":null,"previous_names":["agneay/lua-cheatsheet"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/agneay/Lua-Cheatsheet","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agneay%2FLua-Cheatsheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agneay%2FLua-Cheatsheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agneay%2FLua-Cheatsheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agneay%2FLua-Cheatsheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/agneay","download_url":"https://codeload.github.com/agneay/Lua-Cheatsheet/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agneay%2FLua-Cheatsheet/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261789289,"owners_count":23209776,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cheatsheet","lua","lua-cheatsheet"],"created_at":"2025-03-05T23:18:25.966Z","updated_at":"2025-06-25T02:07:58.307Z","avatar_url":"https://github.com/agneay.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Welcome to Lua-Cheatsheet\r\n\r\n---\r\n\r\n---\r\n\r\n## Comments :\r\n\r\n---\r\n\r\nComments in lua are of two types:\r\n\r\n- single line comment\r\n\r\n- multi line comment\r\n\r\n### single line comment:\r\n\r\nA single line comment in lua is given by adding `--` to the begining of the line\r\n\r\nFor example:\r\n\r\n```lua\r\n-- This is a comment\r\n```\r\n\r\nIt is not necessary to leave a space after `--` to the make a comment\r\n\r\nFor example:\r\n\r\n```lua\r\n--This is also a comment\r\n```\r\n\r\nLua also does not restrict the number of comments possible for example\r\n\r\n```lua\r\n-- this is comment 1\r\n-- this is comment 2\r\n-- this is comment 3\r\n```\r\n\r\n### multi line comment :\r\n\r\nA  multiline comment is lua begins with `--[[` and ends with `]]`\r\n\r\nFor example:\r\n\r\n```lua\r\n--[[\r\n    This \r\n    is \r\n    a \r\n    multiline\r\n    comment\r\n]]\r\n```\r\n\r\n\u003e Make sure there is no space between `--` and `[[` in the start of the multiline comment as doing so will cause `Error`\r\n\r\n\u003e NOTE: Comments can be place wherever we want for example they can be placed after a meaningful statements \r\n\u003e \r\n\u003e For example:\r\n\u003e \r\n\u003e ```lua\r\n\u003e print(\"Hello World!\") -- This statement will be ignored\r\n\u003e ```\r\n\r\n## Printing stuffs onto screen:\r\n\r\nIn lua we can print\r\n\r\n- texts\r\n\r\n- variables\r\n\r\n### printing texts onto screen:\r\n\r\nTo print text onto screen we need to simply use the print statement in the following format is `print(\u003ctext to be printed\u003e)`\r\n\r\nFor example:\r\n\r\n```lua\r\nprint(\"Hello World!\")\r\n```\r\n\r\n\u003e Note: A string will always be enclosed be quotation marks\r\n\r\nWe are also not limited to the number of print statements we can have\r\n\r\nFor example:\r\n```lua\r\nprint(\"Hello World!\")\r\n```\r\n\r\n\u003e Note: A string will always be enclosed be quotation marks\r\n\r\nWe are also not limited to the number of print statements we can have\r\n\r\nFor example:\r\n\r\n```lua\r\nprint(\"Hello World !\")\r\nprint(\"My name is AGNEAY B NAIR\")\r\n```\r\n\r\nTo print multiple  text using the same print statement we can use of the below given format\r\n\r\nFormat:\r\n\r\n```lua\r\nprint(\"HELLO WORLD\",\"I LOVE LUA\")\r\n```\r\n\r\nthe above print statement will display the two strings together by seperating them by a `tab` space\r\n\r\ninorder to concatenate two strings using the `print` statement in lua use `..` instead of `, ` in the print statement\r\n\r\nFor example:\r\n\r\n```lua\r\nprint(\"Hello world!\"..\" I love lua !\")\r\n```\r\n\r\nwill concatenate both the strings\r\n\r\n## Variables and data types:\r\n\r\n- The undefined data types: The `nil` data type is undefined and empty\r\n\r\n- The number data type: `number`\r\n\r\n- `string` data type : all characters enclosed within quotes\r\n\r\n- `boolean` data type: `true` and `false`\r\n\r\n- `tables` data type: The equivalent of array or list present in different programming languages\r\n  \r\n  ## To create a local variable\r\n  \r\n  ```lua\r\n  local name\r\n  ```\r\n\r\n   here `name` is the variable name\r\n\r\nlua always explicitly places the nil data type to the variable . Hard to believe ? Let's try it out\r\n\r\n```lua\r\nlocal name\r\nprint(name)\r\n```\r\n\r\nThis would return nil as the variable name stores nil\r\n\r\nwe can also implicitly give `name` the value nil as\r\n\r\nFor example:\r\n\r\n```lua\r\nlocal name = nil\r\nprint(name)\r\n```\r\n\r\nIt is not necessary to define the value in the same line as that in which we define the variable\r\n\r\n```lua\r\nlocal name\r\nname = nil\r\nprint(name)\r\n```\r\n\r\nwe can also use print statement to display  the value of expressions\r\n\r\nFor example:\r\n\r\n```lua\r\nlocal x = 3\r\nprint(x+8)\r\n```\r\n\r\n## To define a global variable\r\n\r\n```lua\r\nGlobalVar = 3\r\n```\r\n\r\nHere the variable `GlobalVar` has the value 3 and is globally defined and can be imported in other lua files\r\n\r\ninorder to define a global variable to lua more spefically to lua we could write\r\n\r\n```lua\r\n_G.GloablVar = 3\r\n```\r\n\r\nHere the prefix _G. will make it clear to lua that we intend to create a global variable and not a local variable\r\n\r\n\u003e NOTE: we need not use _G. always it is completly optional \r\n\r\n## To define multiple local variable at the same time\r\n\r\n```lua\r\nlocal name,age,gender\r\n```\r\n\r\nnow we have defined three variables at the same time\r\n\r\nwe could also give them values as we define them as\r\n\r\n```lua\r\nlocal namea,nameb,namec = \"ram\",\"lak\",\"shah\"\r\n```\r\n\r\nhere 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\r\n\r\n```lua\r\nlocal name,age,gender = \"AGNEAY\",18,\"MALE\"\r\n```\r\n\r\n## Getting the length of the string\r\n\r\nwe can get the length of any string in lua with the help of `#`\r\n\r\nFor example:\r\n\r\n```lua\r\nlocal x = \"AGNEAY B NAIR\"\r\nprint(#x)\r\n```\r\n\r\nThe above code will return the length of string, we can also do it this way\r\n\r\n```lua\r\nlocal x = #\"AGNEAY B NAIR\"\r\nprint(x)\r\n```\r\n\r\n## To check the type of a given data type\r\n\r\nwe can simply use `type()` function provided by lua\r\n\r\nFor example:\r\n\r\n```lua\r\nlocal x = \"AGNEAY B NAIR\"\r\nlocal y = 13\r\nlocal z = true\r\nprint(type(x),type(y),type(z))\r\n```\r\n\r\n## To convert a number to string:\r\n\r\nwe can use the `tostring()` function provided by lua\r\n\r\n```lua\r\nlocal x = 18\r\nprint(type(x))\r\nlocal y = tostring(x)\r\nprint(type(y)\r\n```\r\n\r\n## Escape sequence\r\n\r\n| Escape sequence | meaning           |\r\n|:---------------:|:-----------------:|\r\n| \\n              | newline character |\r\n| \\t              | tab space         |\r\n| \\\\\\             | \\                 |\r\n\r\n\r\n## String functions\r\nSome string functions are \r\n```lua\r\nlocal name = \"Agneay\"\r\nprint(String.upper(name))\r\nprint(String.lower(name))\r\nprint(String.len(name))\r\n```\r\n\r\nThe 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\r\n\r\n\r\n\r\n\r\n\r\n\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fagneay%2Flua-cheatsheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fagneay%2Flua-cheatsheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fagneay%2Flua-cheatsheet/lists"}