Groups | Search | Server Info | Login | Register


Groups > comp.lang.awk > #10023

Re: nested ternary discovery

From Mike Sanders <porkchop@invalid.foo>
Newsgroups comp.lang.awk
Subject Re: nested ternary discovery
Date 2025-10-11 11:46 +0000
Organization A noiseless patient Spider
Message-ID <10cdg2t$nu0u$1@dont-email.me> (permalink)
References <10c866d$2lhr4$1@dont-email.me> <10c8j6q$2tqge$1@dont-email.me>

Show all headers | View raw


On Thu, 9 Oct 2025 17:09:13 +0200, Janis Papanagnou wrote:

> (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.)

'Cascaded' - nice & clear distinction.

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

Not astonished really. Actually I already knew ternary could be held
within ternary but, somehow or another I thought it must be balanced,
ternary in both branches (regardless of depth):

x = (y > z) ? ternary-again : ternary-too

vs a mix...

x = (y > z) ? ternary-here : (a != b) ? c : ternary-default

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

Yes, or put differently no final 'else'.
 
> 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" }

More good examples.

> 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
>     ...
>     }

Yeah in my mind, switch/case is the most useful.
 
> 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.[*])

Oh, I don't know, for shorter constructs, especially one liners, awk
is plenty good in my thinking at least.

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

Not at all, very happy you filled more detail. Your reply will be saved
to my notes.

> [*] 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).

Ah yes, I'm familiar with several of these 'forms'.

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

Very nifty in fact. I learned more than I posted. Thanks for sharing your knowledge,
much appreciated.

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