Comma quibbling: Difference between revisions
Content deleted Content added
→{{header|C#}}: Marked incorrect. |
Add AWK and UNIX Shell. |
||
Line 15: | Line 15: | ||
Note: Assume words are non-empty strings of uppercase characters for this task. |
Note: Assume words are non-empty strings of uppercase characters for this task. |
||
=={{header|AWK}}== |
|||
<lang awk>function quibble(a, n, i, s) { |
|||
for (i = 1; i < n - 1; i++) s = s a[i] ", " |
|||
i = n - 1; if (i > 0) s = s a[i] " and " |
|||
if (n > 0) s = s a[n] |
|||
return "{" s "}" |
|||
} |
|||
BEGIN { |
|||
print quibble(a, 0) |
|||
n = split("ABC", b); print quibble(b, n) |
|||
n = split("ABC DEF", c); print quibble(c, n) |
|||
n = split("ABC DEF G H", d); print quibble(d, n) |
|||
}</lang> |
|||
{{out}} |
|||
<pre>{} |
|||
{ABC} |
|||
{ABC and DEF} |
|||
{ABC, DEF, G and H}</pre> |
|||
=={{header|C#}}== |
=={{header|C#}}== |
||
Line 298: | Line 318: | ||
{ABC, DEF, G and H} |
{ABC, DEF, G and H} |
||
</pre> |
</pre> |
||
=={{header|UNIX Shell}}== |
|||
{{trans|AWK}} |
|||
<lang bash>quibble() { |
|||
# Here awk(1) is easier than sed(1). |
|||
awk 'BEGIN { |
|||
for (i = 1; i < ARGC - 2; i++) s = s ARGV[i] ", " |
|||
i = ARGC - 2; if (i > 0) s = s ARGV[i] " and " |
|||
i = ARGC - 1; if (i > 0) s = s ARGV[i] |
|||
printf "{%s}\n", s |
|||
exit 0 |
|||
}' "$@" |
|||
} |
|||
quibble |
|||
quibble ABC |
|||
quibble ABC DEF |
|||
quibble ABC DEF G H</lang> |
|||
{{out}} |
|||
<pre>{} |
|||
{ABC} |
|||
{ABC and DEF} |
|||
{ABC, DEF, G and H}</pre> |