Path: csiph.com!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Kaz Kylheku <643-408-1753@kylheku.com> Newsgroups: comp.lang.awk Subject: Re: nested ternary discovery Date: Thu, 9 Oct 2025 16:59:11 -0000 (UTC) Organization: A noiseless patient Spider Lines: 134 Message-ID: <20251009094546.554@kylheku.com> References: <10c866d$2lhr4$1@dont-email.me> Injection-Date: Thu, 09 Oct 2025 16:59:12 +0000 (UTC) Injection-Info: dont-email.me; posting-host="f43092806b629e499ecdf9d5aa338fec"; logging-data="3192806"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+qSEgpp+g3XmKPC1cinXgXyKzifvEV9fQ=" User-Agent: slrn/pre1.0.4-9 (Linux) Cancel-Lock: sha1:inm/KDdsZWBSS0kZ7CuzuKgOQ20= Xref: csiph.com comp.lang.awk:10017 On 2025-10-09, Mike Sanders 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 ($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 "" # 1 "" # 1 "" # 1 "/usr/include/stdc-predef.h" 1 3 4 # 1 "" 2 # 1 "" # 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 "" # 1 "" # 1 "" # 1 "/usr/include/stdc-predef.h" 1 3 4 # 1 "" 2 # 1 "" # 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; }}} } }