Ternary operator in lua

In Java, I very often have to do the following, to mark the plural when needed:

System.out.printf("Delivered: %d item%s", n, n > 1 ? "s", "");

In Lua, there is no ternary operator, so this kind of elegant syntax might be compromised in this situation. Fortunately, there is an as nice workaround:

print(string.format("Delivered: %d item%s", n > 1 and "s" or ""))

Thanks to hisham.hm

Avatar
Lydéric Landry
écrivain et éditeur

Après plusieurs vies, j'écris maintenant