bash function printf(1) takes a format string and a list of arguments
C function printf(3) takes a format string and a list of arguments
unlike C argument lists, bash argument lists have a known size, likewise
lua argument lists.
it would make sense for lua to loop the format string just like bash.
(but maybe, unlike bash, enforce multiples.)
I get why someone might want that in the shell because loops can be a pain to code, especially over multiple-variable lists. Being able to format a table as simply as this has some merit:$ item_properties = (color red size L collar Roman)$ printf "%10s | %s" $item_propertiescolor | red
size | L
collar | Roman
item_properties = {color = 3, size = 45, collar = 2};for k, v in pairs(item_properties) doio.write(string.format("%10s | %d\n", k, v));endThat outputs just what we want:color | 3
size | 45
collar | 12
I get why someone might want that in the shell because loops can be a pain to code, especially over multiple-variable lists. Being able to format a table as simply as this has some merit:
$ item_properties = (color red size L collar Roman)$ printf "%10s | %s" $item_propertiescolor | red
size | L
collar | Roman
But it has its down sides, including the loss of type validation of the arguments (not that shell printf type-validates the arguments even when it's not repeating the format):
$ item_properties=(color 3 size 45 collar Roman)$ printf "%10s | %d\n" $item_propertiescolor | 3
size | 45
collar | 0
No other formatting system would let that go without complaint.
Plus, you're not likely to use string.format() like that in Lua because, like in C, you can't pass it multiple arguments via a list variable. This won't work:
item_properties = {"color", 3};string.format("%10s | %d", item_properties);
You'd have to do this, even when the number of arguments IS the same as the format string:
string.format("%10s | %d", item_properties[1], item_properties[2]);
I made a `lua-fmt` module before (`luarocks install lua-fmt`), you could:
```lua
print(fmt("{1.1:10} | {1.2}", item_properties)) --> outputs: color | 3
```
You could even:
```lua
local obj = {color = "red", num = 3}
print(fmt("{color:10} | {num}", obj)) --> outputs: read | 3
```
Using a barebones fmt module to output JSON seems like a mistake.
Instead you should build the appropriate Lua table and pass that
to a JSON library.
That way you essentially get syntax checking at Lua load time
(because the Lua table syntax is checked)
and you can't make mistakes such as poorly formatting numbers or
not escaping strings.
--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/e3c33503-850b-4966-bd62-efc33b88dfe3n%40googlegroups.com.