tips
here
rlualogo.png

Here are some tips on using RiscLua for small housekeeping jobs on your hard-disc. An easy way to do something to the contents of a directory is to write your RiscLua program as a file of type Lua (i.e. filetype &18c), drag a copy of it into the directory and then run it by double-clicking it. file_18c.gif

So how does the program know where it is? Did you know that the expression

 arg[0] 
holds the pathname of the file containing the program? So we can get the pathname of the directory containing it by chopping off the leafname from the end of that, together with the fullstop that RISC OS uses as the directory separator symbol. The leafname cannot have any fullstops in it. The expression

 "%.[^%.]+$" 
is a pattern that matches text starting with a full stop (%.) followed by a nonempty string that does not contain fullstops [^%.]+ which continues till the end of the text ($). The % is needed because a fullstop by itself matches any single character. So this pattern should match just what we want to chop out. The expression

 (arg[0]):gsub ("%.[^%.]+$","") 
will have as value the pathname of the directory containing our program. The parentheses round arg[0] are needed because arg[0] is an expression (take the value of the table arg at index 0). gsub is the global substitution string method. The expression in front of the colon gives the string it is to be applied to. Its effect is to do a global substitution in the string, replacing each substring that matches the pattern given by its first argument by the value of its second argument (when this is a string). In our case not more than one substring can match, and the effect is to replace the leafname and directory separator by an empty string.