Groups | Search | Server Info | Login | Register


Groups > comp.lang.awk > #10016

Re: nested ternary discovery

From Janis Papanagnou <janis_papanagnou+ng@hotmail.com>
Newsgroups comp.lang.awk
Subject Re: nested ternary discovery
Date 2025-10-09 17:09 +0200
Organization A noiseless patient Spider
Message-ID <10c8j6q$2tqge$1@dont-email.me> (permalink)
References <10c866d$2lhr4$1@dont-email.me>

Show all headers | View raw


On 09.10.2025 13:27, Mike Sanders wrote:
> Just discovered this, pretty nifty!
> 
> nested ternary example:

(Let's call it "cascaded". Nested conditionals would in the general
case - where you'd have another level of conditionals between the
'?' and ':' - look even more complex and be much less readable, in
both variants, conditional statements and conditional expressions.)

> 
> sound = "moo"
> 
> mammal = (sound =="bark") ? "dog" :
>          (sound =="meow") ? "cat" :
>          (sound =="moo")  ? "cow" :
>          mammal  # keep original if no match

Hmm.. - what specifically are you astonished about? The conditional
expression per se, or that cascading is not explicitly forbidden?

That should be standard in all the "C"-like languages' with ternary
conditional expressions.

> 
> and for comparison the equivalent if/else block:
> 
> sound = "moo"
> 
> if (sound == "bark") {
>     mammal = "dog"
> } else if (sound == "meow") {
>     mammal = "cat"
> } else if (sound == "moo") {
>     mammal = "cow"
> } else {
>     mammal = mammal  # keep original if no match
> }
> 

"Ternaries" (conditional expressions) are certainly very useful!
And they can increase readability (per se, and in cascaded form).

But I'm not sure what the goal of your comparison was; if it's the
terse expression then you should rather compare the ternary with
the statement construct without the spurious braces and 'else' part

    if (sound == "bark")
        mammal = "dog"
    else if (sound == "meow")
        mammal = "cat"
    else if (sound == "moo")
        mammal = "cow"

and note that the statement-cascades need no final else block where
the nested ternary expression needs one to be complete.

For "comparison" in Awk there should probably also be shown the
awk-ish variant

  sound == "bark" { mammal = "dog" }
  sound == "meow" { mammal = "cat" }
  sound == "moo"  { mammal = "cow" }

Or, not exactly the same but often you compare against fields, here
an example illustrated for $0

  /^bark$/ { mammal = "dog" }
  /^meow$/ { mammal = "cat" }
  /^moo$/  { mammal = "cow" }

(Whether you want to use 'next' in the latter cases depends on the
actual conditions in your application cases.)

And if you're using GNU Awk there's the 'switch' statement available
to not repeat the variable you compare against

    switch (sound) {
    case "bark": mammal = "dog"; break
    ...
    }

I think "for comparison" all these options one has should be shown;
all have their own pros and cons.

(In case your astonishment is per se from the ternary already, note
also that "C"/Awk's way isn't that "nifty" if compared to some other
languages' conditional expressions.[*])

(That got longer than intended, but for a "comparison" necessary.)

Janis

[*] Compare that to Algol 68. There's no difference whether you have
conditionals in "statement" or "expression" constructs, and you can
also have it on both sides of assignments, and you can choose between
the abbreviated or verbose form.

  IF   sound = "bark" THEN mammal := "dog"
  ELIF sound = "meow" THEN mammal := "cat"
  ELIF sound = "moo"  THEN mammal := "cow"
  FI

  mammal := IF   sound = "bark" THEN "dog"
            ELIF sound = "meow" THEN "cat"
            ELIF sound = "moo"  THEN "cow"
            ELSE mammal
            FI

  ( sound = "bark" | mammal := "dog" |:
    sound = "meow" | mammal := "cat" |:
    sound = "moo"  | mammal := "cow" )

  mammal := ( sound = "bark" | "dog" |:
              sound = "meow" | "cat" |:
              sound = "moo"  | "cow" | mammal )

The expression variants need a final 'else' only if you want to keep
the previous variable contents if nothing matches. Otherwise, as is
often the case (if you want it empty) you'd of course simply write

  mammal := ( sound = "bark" | "dog" |:
              sound = "meow" | "cat" |:
              sound = "moo"  | "cow" )


And one example for conditionals on the left hand side

  ( amount < 0 | losses | gains ) +:= amount

and this is also possible in the syntactical variant with 'IF'.

For "nested" forms - note the distinction from "cascaded" on top
of the post - you can also use both syntactical variants mixed
for better readability; e.g. write the outer levels with 'IF' and
inner levels with parenthesis. For example

  IF ... THEN
     ( ... | ... |: ... | ... )
  ELIF ... THEN
     ( ... | ... |: ... |: ... | ... )
  ELSE
     ( ... | ... | ... )
  FI

(and compare that to a pure 'IF' based or a pure parenthesis based
use).

That's all "pretty nifty", isn't it? :-)

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