INSERTING LINE NUMBERS

In the example on this page, and in those on subsequent tutorial pages, reserved words are shown in green, and comments are shown in red. If your browser supports CSS3 then moving the mouse-pointer over certain parts of the program may pop up an explanatory tooltip.

As an initial example, here is a script for adding linenumbers to text. The linenumbers are formatted with leading zeroes to three places and followed by a colon.

#!lua
local fmt <const>, count = does for both assignments "%03d: %s" , 1
for linevariable: you choose its name in ioinput/output library.table memberlines ( arg[1]the textfile ) do
  print ( fmtvariable: you choose its name:formatstring method ( countvariable: you choose its name , linevariable: you choose its name ) )
  count + = 1
end -- for

The first line is a comment, which RiscLua ignores. StrongED, however, will not ignore it, and will use it to determine how to apply the script. The word local is a keyword indicating that the following words, fmt and count, name local rather than global variables - that is, names that you can choose. Expressions in angle brackets after the declaration of a local variable denote attributes. There are only two attributes to worry about: const which indicates that the variable cannot have its value changed, or close which indicates that its value will be closed when the variable goes out of scope (file-handles for example). Only one local variable in a definition list can have this attribute. Global variables have scope everywhere in the program unless they are overridden within a block by a local variable with the same name. Local variables have scope only within the block in which they are declared, and after their declaration. In this particular case the block is the whole program. The program would work just the same if the word local were omitted. However, it is good practice to use local variables whenever possible. The interpreter takes less time looking them up.

The symbol = indicates an assignment. The format string "%03d: %s" is assigned to the variable fmt and the number 1 to the variable count. The expression %03d is a numeric format specifier - it expects to be replaced by the textual form of a number - and the expression %s is a string format specifier - it expects to be filled in by a string.

Numbers and strings are distinct types. You cannot tell anything about type from the form of a variable's name. It is the value of the variable which carries a type, not the variable's name. The variable fmt has the same value throughout the program. It is constant. On the other hand, the variable count gets updated in line 5.

There is another local variable in the program. It has the name line. It is the iteration variable of the for-block. It is automatically local to the for-block, and needs no local-declaration. The built-in function io.lines fetches the lines of text one by one as strings from the file specified by its argument and assigns them as the values of line. Note that the body of the for-loop begins with the word do and ends with end. The built-in function print outputs its arguments as strings, separated by tab-characters if there is more than one, followed by a newline. The expression fmt:format is a method of the string fmt expecting as many arguments as there are format-specifiers in the string fmt whose values it uses for substitution into them.

The indentation of the two lines in the body of the for-loop is purely to help the eye.

Though this initial example is only 6 lines long, we have encountered the following notions:

comments, reserved words, variables both local and global,
attributes, built-in names, assignment, for-blocks,
format-specifiers, string methods