lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sat, Aug 22, 2015 at 5:08 PM, Sean Conner <sean@conman.org> wrote:
> [2]     Python has an alternative format specification where you can specify
>         which parameter (either by name or position in the argument list) to
>         use.  This makes it a bit easier to translate messages.
>

This is actually supported by POSIX string formatting[1]:

printf("The %1$s has %2$d feet.\n", "cat", 4);
printf("There are %2$d feet on a %1$s.\n", "cat", 4);

Unfortunately Lua's string.format doesn't support it.[2]

[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/printf.html
- "Conversions can be applied to the nth argument after the format in
the argument list, rather than to the next unused argument. In this
case, the conversion specifier character % (see below) is replaced by
the sequence "%n$", where n is a decimal integer in the range
[1,{NL_ARGMAX}], giving the position of the argument in the argument
list."

[2] On the other hand, Lua supports named arguments:
> words={subject='cat', feet=4}
> 'The {subject} has {feet} feet'):gsub('%b{}', function(s) return words[s:sub(2,-2)] end)
The cat has 4 feet    2
(The extra "2" is the second return value from gsub. Also, this method
won't work with numeric keys - it'll look for the key "1" rather than
1 - but this is easily fixed.)

-- 
Sent from my Game Boy.