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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s