Groups | Search | Server Info | Login | Register


Groups > comp.lang.awk > #10017

Re: nested ternary discovery

From Kaz Kylheku <643-408-1753@kylheku.com>
Newsgroups comp.lang.awk
Subject Re: nested ternary discovery
Date 2025-10-09 16:59 +0000
Organization A noiseless patient Spider
Message-ID <20251009094546.554@kylheku.com> (permalink)
References <10c866d$2lhr4$1@dont-email.me>

Show all headers | View raw


On 2025-10-09, Mike Sanders <porkchop@invalid.foo> wrote:
> Just discovered this, pretty nifty!
>
> nested ternary example:
>
> sound = "moo"
>
> mammal = (sound =="bark") ? "dog" :
>          (sound =="meow") ? "cat" :
>          (sound =="moo")  ? "cow" :
>          mammal  # keep original if no match

In GNU Awk you have a switch statement.

In cppawk, there is a case statement which compiles to GNU Awk switch or
portably.


$ cat case.awk
#include <case.h>

{
  case ($0) {
    of ("woof", "bark")
      print "dog"
      cbreak
    matching (/mee*oo*w/, /purr*/)
      print "cat"
      cbreak
    of ("moo")
      print "cow"
      cbreak
    otherwise
      print "unknown beast"
      cbreak
  }
}
$ cppawk -f case.awk
bark
dog
woof
dog
meeeeow
cat
moo
cow
purrrrrrr
cat

Cases use the "of" or "matching" keywords for exact strings or regexes. Each can
have multiple arguments.

Each case must end with cbreak (intentional termios/curses pun),  cfall
for fall-thrugh or cret(arg)/creturn(arg) to return out of the function.

There is no implicit fallthrough.

Code generated for gawk (default):

$ cppawk --prepro-only -f case.awk
# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "<stdin>"
# 1 "case.awk"
# 1 "/home/kaz/bin/cppawk-include/case.h" 1
# 32 "/home/kaz/bin/cppawk-include/case.h"
# 1 "/home/kaz/bin/cppawk-include/case-priv.h" 1
# 32 "/home/kaz/bin/cppawk-include/case-priv.h"
# 1 "/home/kaz/bin/cppawk-include/narg-priv.h" 1
# 32 "/home/kaz/bin/cppawk-include/narg-priv.h"
# 1 "/home/kaz/bin/cppawk-include/base.h" 1
# 33 "/home/kaz/bin/cppawk-include/narg-priv.h" 2
# 33 "/home/kaz/bin/cppawk-include/case-priv.h" 2
# 33 "/home/kaz/bin/cppawk-include/case.h" 2
# 2 "case.awk" 2

{
  switch ($0) {
    case "woof": case "bark": {{{
      print "dog"
      break; }}}
    case/mee*oo*w/: case /purr*/: {{{
      print "cat"
      break; }}}
    case "moo": {{{
      print "cow"
      break; }}}
    default: {{{
      print "unknown beast"
      break; }}}
  }
}

Code generated for awk with no switch, like mawk:

$ cppawk --awk=mawk --prepro-only -f case.awk
# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "<stdin>"
# 1 "case.awk"
# 1 "/home/kaz/bin/cppawk-include/case.h" 1
# 32 "/home/kaz/bin/cppawk-include/case.h"
# 1 "/home/kaz/bin/cppawk-include/case-priv.h" 1
# 32 "/home/kaz/bin/cppawk-include/case-priv.h"
# 1 "/home/kaz/bin/cppawk-include/narg-priv.h" 1
# 32 "/home/kaz/bin/cppawk-include/narg-priv.h"
# 1 "/home/kaz/bin/cppawk-include/base.h" 1
# 33 "/home/kaz/bin/cppawk-include/narg-priv.h" 2
# 33 "/home/kaz/bin/cppawk-include/case-priv.h" 2
# 33 "/home/kaz/bin/cppawk-include/case.h" 2
# 2 "case.awk" 2

{
  for ((__once = 1) && (__pass = 0) || (__val = $0); __once; __once = 0) {
    if (__pass || ((__val == ("woof")) || (__val == ("bark"))) && (__pass = 1)) {{{
      print "dog"
      break; }}}
    if (__pass || ((__val ~ (/mee*oo*w/)) || (__val ~ (/purr*/))) && (__pass = 1)) {{{
      print "cat"
      break; }}}
    if (__pass || ((__val == ("moo"))) && (__pass = 1)) {{{
      print "cow"
      break; }}}
    if (__pass = 1) {{{
      print "unknown beast"
      break; }}}
  }
}

Back to comp.lang.awk | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

nested ternary discovery Mike Sanders <porkchop@invalid.foo> - 2025-10-09 11:27 +0000
  Re: nested ternary discovery Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-10-09 17:09 +0200
    Re: nested ternary discovery Mike Sanders <porkchop@invalid.foo> - 2025-10-10 19:38 +0000
    Re: nested ternary discovery Mike Sanders <porkchop@invalid.foo> - 2025-10-11 11:46 +0000
      Re: nested ternary discovery Kaz Kylheku <643-408-1753@kylheku.com> - 2025-10-11 16:58 +0000
        Re: nested ternary discovery Mike Sanders <porkchop@invalid.foo> - 2025-10-12 03:47 +0000
      Re: nested ternary discovery Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-10-12 01:54 +0200
        Re: nested ternary discovery Mike Sanders <porkchop@invalid.foo> - 2025-10-12 03:49 +0000
  Re: nested ternary discovery Kaz Kylheku <643-408-1753@kylheku.com> - 2025-10-09 16:59 +0000
  Re: nested ternary discovery mack@the-knife.org (Mack The Knife) - 2025-10-10 08:10 +0000
    Re: nested ternary discovery Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-10-10 13:06 +0200
    Re: nested ternary discovery Kaz Kylheku <643-408-1753@kylheku.com> - 2025-10-10 17:04 +0000
      Re: nested ternary discovery mack@the-knife.org (Mack The Knife) - 2025-10-11 17:44 +0000
        Re: nested ternary discovery Kaz Kylheku <643-408-1753@kylheku.com> - 2025-10-11 18:46 +0000
          Re: nested ternary discovery mack@the-knife.org (Mack The Knife) - 2025-10-13 08:07 +0000
    Re: nested ternary discovery Mike Sanders <porkchop@invalid.foo> - 2025-10-10 19:41 +0000
    Re: nested ternary discovery Mike Sanders <porkchop@invalid.foo> - 2025-10-11 11:55 +0000
      Re: nested ternary discovery Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-10-12 01:45 +0200
      Re: nested ternary discovery dave_thompson_2@comcast.net - 2025-10-12 20:58 -0400
        Re: nested ternary discovery Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-10-13 04:30 +0200

csiph-web