Is There A Difference Between Lua And C++

Both 'a' and 'beta' are strings, and could also be written as 'a' and 'beta'. In Lua, there's no difference between single-charcter and multiple-character values. The empty string is ' or '. The words true and false (without quotes) are reserved for boolean values in Lua. Two-dimensional table.

< Lua

About lua[edit]

Lua is a scripting language which can be used to analyze data and calculate expressions. It formats results which support procedural programming, object-oriented programming, functional programming, functionality of Scribunto.

Differences between lua and scribunto[edit]

Lua is a programming language that is available via the ScribuntoMediaWiki extension on the English Wikipedia. The Scribunto extension allows for embedding scripting languages in MediaWiki. Lua code can now be embedded into wiki templates by employing the '{{#invoke:}}' functionality of Scribunto. This extension supports Lua 5.1 as of July 2015.

Currently, the only supported scripting language is Lua.

Is There A Difference Between Lua And C++

A Scribunto module in itself is really a large function: it runs from top to bottom and is expected to return a value. It could return a table of strings, a table containing other tables, or even a single value. Normally, it returns a table of exported functions that can be used with {{#invoke: }}

Benefit of lua scripting[edit]

Templates and ParserFunctions were introduced to allow end-users of MediaWiki to replicate content easily and build tools using basic logic, effectively turning wikitext into a limited programming language.

However, complex templates have caused performance issues and bottlenecks.

This project aims to make it possible for MediaWiki end-users to use a proper scripting language that will be more powerful and efficient than ad-hoc ParserFunctions-based logic.

Getting started with lua on wikipedia[edit]

The software used by Wikipedia, called MediaWiki, has an extension that provides a version of Lua that can be used within Wikipedia pages. The extension is called Scribunto.

The Lua source code is stored in pages called modules. The module itself must return a Lua table containing the functions that may be called by {{#invoke:}}. Any functions that are not added to this table, whether local or global, will not be accessible by {{#invoke:}}, but globals might be accessible from other modules loaded using require().

It is generally a good style for the module to declare all functions and variables local.

These individual modules can be invoked following this format {{#invoke:<Module name> <Function name> (optional) param1 param2..}}). In the example below, we will be working with Lua on Wikipedia using module sandbox (contain the Lua code to be run) and user sandbox (contain the wiki-text that runs the code and displays the results).

Prerequisite[edit]

  • Create a Wikipedia account if you don't already have one – see:Wikipedia: Why create an account? Adding an email address allows you to reset your password if you forget it.
  • After you have logged in to your account, use the link at the top right of your page that has your username to create your user page. In the edit box, write a sentence, but don't add personal information. Preview your edit and save it.
  • Use the link at the top right of your page to create your sandbox. Write a brief note saying that this is your user sandbox, preview it and save it. Remember that what you type can potentially be seen by anybody, so be sensible!
  • Read about Scribunto, the Lua implementation embedded in Wikimedia software: Wikipedia: Lua
  • Create an empty module sandbox for yourself as a subfolder of Module: Sandbox For example, if your user name is 'Gechy', then create your module at Module:Sandbox/Gechy/ogechi. Note that any spaces will be converted to underscores _ in the page URL that you can see in your browser's address bar. This is called the URL-encoding.
  • In your module sandbox add a line of text starting with two hyphens: --
  • After the two hyphens, type your username followed by Introduction to Lua in Wikipedia. Text beginning with two hyphens is used in Lua to designate a comment.
  • Save your module sandbox.

Running lua code in wikipedia[edit]

A Wikipedia page can call a Lua module to do calculations, process text, format citations, fetch information from Wikidata, and many other jobs where a programming language is needed to get a result. A Lua module is used inside a Wikipedia page by using a call something like{{#invoke:RexxS carousel}}

  • In your user sandbox (not your module sandbox), leave a blank line after your brief note, then type this: Task 1

Putting around a phrase creates a level 2 Html heading. We can use that to break up our user sandbox into sections for different tasks.

  • On the line below that, type or copy {{#invoke:RexxS carousel}} and save it.

Please don't miss out saving your user sandbox each time you are instructed to.

You should see the filename of a JPG image. The line makes use of a module called Module: Rexxs. Modules can contain many functions and the line you have entered calls a function in that module called 'carousel'.

  • Add another new line in your user sandbox that reads [[File:{{#invoke:RexxS carousel}} thumb]] and save it.

This uses the standard Wikipedia image syntax to display the image. You can read a lot more about image syntax at Wikipedia: Extended image syntax.

  • Look at Module:Rexxs and examine the code. See if you can work out how the function picks a filename. You may find the documentation at http://www.lua.org/manual/5.3/ helps you to learn how Lua works.

You should be able to work out that the filenames of the images which can be returned are kept in a list. In Lua, lists are tables. The table is the only data structure used and all other types of data structures, arrays, sequences, objects, etc. are created from tables.

In the Lua Module, we can have as many functions as we want and this can be called by simply identifying the function name.

WikitextResult
{{#invoke:Example hello}}hello}}

In the example above the module used is called Module:Example and a Function name called hello is invoked see a snippet of the hello function from the Module:Example below:

The Module:Example used above contains up to five different functions(hello,hello_to,count_fruit,lucky and Name2) that can be called independently using {{#invoke:}}.

Lua concept[edit]

In Lua, there are no type definitions in the language. All values carry their own type. This means that all values can be stored in variables, passed as arguments to other functions, and returned as results. In Scribunto we make use of six out of the eight types available in the Lua programming language.

Nil[edit]

The type nil has one single value, nil, whose main property is to be different from any other value. It often represents the absence of a useful value.

Both nil and false make a condition false, they are collectively called false values. Any other value makes a condition true. If you try getting a variable that doesn't exist you will get nil.

Boolean[edit]

The type boolean has two values, false and true when converted to a string. And unlike many other languages, only false and nil are considered false for boolean conversion; the number 0 and the empty string are both considered true.

Number[edit]

The type number represents both integer numbers and real (floating-point) numbers, using two subtypes: integer and float. Lua has explicit rules about when each subtype is used, but it also converts between them automatically at run time.

In a conversion from integer to float, if the integer value has an exact representation as afloat, that is the result. Otherwise, the conversion gets the nearest higher or the nearest lower representable value. This kind of conversion never fails.

The conversion from float to integer checks whether the float has an exact representation as an integer (that is, the float has an integral value and it is in the range of integer representation). If it does, that representation is the result. Otherwise, the conversion fails.

String[edit]

The type string represents immutable sequences of bytes. Lua is 8-bit clean: strings can contain any 8-bit value, including embedded zeros ('0'). A string is converted to an integer or a float following its syntax and the rules of the Lua lexer. The string may have also leading and trailing whitespaces and a sign.

Function[edit]

Functions in Lua are first-class values. They may be created anonymously, passed as arguments, assigned to variables.

Functions are created using the function keyword, and called using parentheses. Syntactic sugar is available for named functions, local functions, and functions that act like member functions to a table.

Table[edit]

The Lua table implements associative arrays, that is, arrays that can be indexed not only with numbers but with any value (except nil). Tables can contain values of all types (except nil). To represent records, Lua uses the field name as an index. The Lua table type supports this representation by providing a.name as syntactic sugar for a[name].

Tables are created using curly braces. The empty table is {}. They store a set of key/value pairs. In a Key/Value pair you can store a value under a key and then later retrieve the value using that key.

Data type conversion[edit]

Lua will automatically convert string and number types to the correct format to perform calculations. This automatic conversion of types is called coercion.. Performing arithmetic in Lua to a string it first converts the string to a number else the operation will return an exception when string can't be converted. See example below:

The string 'John' cannot be converted to a number and so an exception occurs. Similarly, in comparison operators, data conversion plays a major role as shown in the example below:

In Lua (in)equality operators consider a number to be not equal to its string representation (or any non-number type in fact) as shown in the example above. It's advised to not rely on automatic coercion, explicitly define a type for performance-sensitive computation.

Basic operation in lua[edit]

The print function[edit]

We'll use the print() function to print out values or calculations on those values. The parentheses around the arguments are important and will cause an error if omitted.

Assignment[edit]

Lua allows for multiple assignments. We can assign strings to variables just like we can numbers:

Concatenate[edit]

The string concatenation operator in Lua is denoted by two dots ('.'). We can concatenate (join together) strings together using the . operator:

Comment[edit]

Is There A Difference Between Lua And C++ Language

A comment starts with a double hyphen (--) anywhere outside a string. Long comments are frequently used to disable code temporarily.

Other operations such as String manipulation, tables, numbers will be discussed intensively in Lua tutorials

References[edit]

Retrieved from 'https://www.mediawiki.org/w/index.php?title=Lua/Overview&oldid=4568372'
< Lua

About lua[edit]

Lua is a scripting language which can be used to analyze data and calculate expressions. It formats results which support procedural programming, object-oriented programming, functional programming, functionality of Scribunto.

Differences between lua and scribunto[edit]

Lua is a programming language that is available via the ScribuntoMediaWiki extension on the English Wikipedia. The Scribunto extension allows for embedding scripting languages in MediaWiki. Lua code can now be embedded into wiki templates by employing the '{{#invoke:}}' functionality of Scribunto. This extension supports Lua 5.1 as of July 2015.

Currently, the only supported scripting language is Lua.

A Scribunto module in itself is really a large function: it runs from top to bottom and is expected to return a value. It could return a table of strings, a table containing other tables, or even a single value. Normally, it returns a table of exported functions that can be used with {{#invoke: }}

Benefit of lua scripting[edit]

Templates and ParserFunctions were introduced to allow end-users of MediaWiki to replicate content easily and build tools using basic logic, effectively turning wikitext into a limited programming language.

However, complex templates have caused performance issues and bottlenecks.

This project aims to make it possible for MediaWiki end-users to use a proper scripting language that will be more powerful and efficient than ad-hoc ParserFunctions-based logic.

Getting started with lua on wikipedia[edit]

The software used by Wikipedia, called MediaWiki, has an extension that provides a version of Lua that can be used within Wikipedia pages. The extension is called Scribunto.

The Lua source code is stored in pages called modules. The module itself must return a Lua table containing the functions that may be called by {{#invoke:}}. Any functions that are not added to this table, whether local or global, will not be accessible by {{#invoke:}}, but globals might be accessible from other modules loaded using require().

It is generally a good style for the module to declare all functions and variables local.

These individual modules can be invoked following this format {{#invoke:<Module name> <Function name> (optional) param1 param2..}}). In the example below, we will be working with Lua on Wikipedia using module sandbox (contain the Lua code to be run) and user sandbox (contain the wiki-text that runs the code and displays the results).

Prerequisite[edit]

  • Create a Wikipedia account if you don't already have one – see:Wikipedia: Why create an account? Adding an email address allows you to reset your password if you forget it.
  • After you have logged in to your account, use the link at the top right of your page that has your username to create your user page. In the edit box, write a sentence, but don't add personal information. Preview your edit and save it.
  • Use the link at the top right of your page to create your sandbox. Write a brief note saying that this is your user sandbox, preview it and save it. Remember that what you type can potentially be seen by anybody, so be sensible!
  • Read about Scribunto, the Lua implementation embedded in Wikimedia software: Wikipedia: Lua
  • Create an empty module sandbox for yourself as a subfolder of Module: Sandbox For example, if your user name is 'Gechy', then create your module at Module:Sandbox/Gechy/ogechi. Note that any spaces will be converted to underscores _ in the page URL that you can see in your browser's address bar. This is called the URL-encoding.
  • In your module sandbox add a line of text starting with two hyphens: --
  • After the two hyphens, type your username followed by Introduction to Lua in Wikipedia. Text beginning with two hyphens is used in Lua to designate a comment.
  • Save your module sandbox.

Running lua code in wikipedia[edit]

A Wikipedia page can call a Lua module to do calculations, process text, format citations, fetch information from Wikidata, and many other jobs where a programming language is needed to get a result. A Lua module is used inside a Wikipedia page by using a call something like{{#invoke:RexxS carousel}}

  • In your user sandbox (not your module sandbox), leave a blank line after your brief note, then type this: Task 1

Putting around a phrase creates a level 2 Html heading. We can use that to break up our user sandbox into sections for different tasks.

  • On the line below that, type or copy {{#invoke:RexxS carousel}} and save it.

Please don't miss out saving your user sandbox each time you are instructed to.

You should see the filename of a JPG image. The line makes use of a module called Module: Rexxs. Modules can contain many functions and the line you have entered calls a function in that module called 'carousel'.

  • Add another new line in your user sandbox that reads [[File:{{#invoke:RexxS carousel}} thumb]] and save it.

This uses the standard Wikipedia image syntax to display the image. You can read a lot more about image syntax at Wikipedia: Extended image syntax.

  • Look at Module:Rexxs and examine the code. See if you can work out how the function picks a filename. You may find the documentation at http://www.lua.org/manual/5.3/ helps you to learn how Lua works.

You should be able to work out that the filenames of the images which can be returned are kept in a list. In Lua, lists are tables. The table is the only data structure used and all other types of data structures, arrays, sequences, objects, etc. are created from tables.

In the Lua Module, we can have as many functions as we want and this can be called by simply identifying the function name.

WikitextResult
{{#invoke:Example hello}}hello}}

In the example above the module used is called Module:Example and a Function name called hello is invoked see a snippet of the hello function from the Module:Example below:

The Module:Example used above contains up to five different functions(hello,hello_to,count_fruit,lucky and Name2) that can be called independently using {{#invoke:}}.

Lua concept[edit]

In Lua, there are no type definitions in the language. All values carry their own type. This means that all values can be stored in variables, passed as arguments to other functions, and returned as results. In Scribunto we make use of six out of the eight types available in the Lua programming language.

Nil[edit]

The type nil has one single value, nil, whose main property is to be different from any other value. It often represents the absence of a useful value.

Both nil and false make a condition false, they are collectively called false values. Any other value makes a condition true. If you try getting a variable that doesn't exist you will get nil.

Boolean[edit]

The type boolean has two values, false and true when converted to a string. And unlike many other languages, only false and nil are considered false for boolean conversion; the number 0 and the empty string are both considered true.

Number[edit]

The type number represents both integer numbers and real (floating-point) numbers, using two subtypes: integer and float. Lua has explicit rules about when each subtype is used, but it also converts between them automatically at run time.

In a conversion from integer to float, if the integer value has an exact representation as afloat, that is the result. Otherwise, the conversion gets the nearest higher or the nearest lower representable value. This kind of conversion never fails.

The conversion from float to integer checks whether the float has an exact representation as an integer (that is, the float has an integral value and it is in the range of integer representation). If it does, that representation is the result. Otherwise, the conversion fails.

String[edit]

The type string represents immutable sequences of bytes. Lua is 8-bit clean: strings can contain any 8-bit value, including embedded zeros ('0'). A string is converted to an integer or a float following its syntax and the rules of the Lua lexer. The string may have also leading and trailing whitespaces and a sign.

Function[edit]

Functions in Lua are first-class values. They may be created anonymously, passed as arguments, assigned to variables.

Functions are created using the function keyword, and called using parentheses. Syntactic sugar is available for named functions, local functions, and functions that act like member functions to a table.

Table[edit]

The Lua table implements associative arrays, that is, arrays that can be indexed not only with numbers but with any value (except nil). Tables can contain values of all types (except nil). To represent records, Lua uses the field name as an index. The Lua table type supports this representation by providing a.name as syntactic sugar for a[name].

Tables are created using curly braces. The empty table is {}. They store a set of key/value pairs. In a Key/Value pair you can store a value under a key and then later retrieve the value using that key.

Data type conversion[edit]

Lua will automatically convert string and number types to the correct format to perform calculations. This automatic conversion of types is called coercion.. Performing arithmetic in Lua to a string it first converts the string to a number else the operation will return an exception when string can't be converted. See example below:

The string 'John' cannot be converted to a number and so an exception occurs. Similarly, in comparison operators, data conversion plays a major role as shown in the example below:

In Lua (in)equality operators consider a number to be not equal to its string representation (or any non-number type in fact) as shown in the example above. It's advised to not rely on automatic coercion, explicitly define a type for performance-sensitive computation.

Basic operation in lua[edit]

The print function[edit]

We'll use the print() function to print out values or calculations on those values. The parentheses around the arguments are important and will cause an error if omitted.

Assignment[edit]

Lua allows for multiple assignments. We can assign strings to variables just like we can numbers:

Concatenate[edit]

The string concatenation operator in Lua is denoted by two dots ('.'). We can concatenate (join together) strings together using the . operator:

Comment[edit]

A comment starts with a double hyphen (--) anywhere outside a string. Long comments are frequently used to disable code temporarily.

Other operations such as String manipulation, tables, numbers will be discussed intensively in Lua tutorials

References[edit]

Retrieved from 'https://www.mediawiki.org/w/index.php?title=Lua/Overview&oldid=4568372'