Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 27 |
Nodes: | 6 (0 / 6) |
Uptime: | 38:01:35 |
Calls: | 631 |
Calls today: | 2 |
Files: | 1,187 |
D/L today: |
22 files (29,767K bytes) |
Messages: | 173,681 |
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
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
}
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
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 article <10c866d$2lhr4$1@dont-email.me>,
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
This is where associative arrays shine:
BEGIN {
animal["bark"] = "dog"
animal["meow"] = "cat"
animal["moo"] = "cow"
# etc. ...
}
{ print animal[$1] }
Much simpler, and each additional sound/animal pair requires
only one additional line of code.
{ print animal[$1] }
Much simpler, and each additional sound/animal pair requires
only one additional line of code.
(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.)
[...]
This is where associative arrays shine:
BEGIN {
annimal["bark"] = "dog"
annimal["meow"] = "cat"
annimal["moo"] = "cow"
# etc. ...
}
{ print animal[$1] }
Much simpler, and each additional sound/animal pair requires
only one additional line of code.
(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.)
Hmm.. - what specifically are you astonished about? The conditional expression per se, or that cascading is not explicitly forbidden?
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" }
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.)
[*] 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? :-)
This is where associative arrays shine:
BEGIN {
annimal["bark"] = "dog"
annimal["meow"] = "cat"
annimal["moo"] = "cow"
# etc. ...
}
{ print animal[$1] }
Much simpler, and each additional sound/animal pair requires
only one additional line of code.
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.
On 2025-10-10, Mack The Knife <mack@the-knife.org> wrote:
{ print animal[$1] }
Much simpler, and each additional sound/animal pair requires
only one additional line of code.
Without function indirection (Gawk extension), or an eval feature
(doesn't exist in any Awk I know), you cannot dispatch actions, only >substitute values for keys.
In article <20251010100336.109@kylheku.com>,
Kaz Kylheku <643-408-1753@kylheku.com> wrote:
On 2025-10-10, Mack The Knife <mack@the-knife.org> wrote:
{ print animal[$1] }
Much simpler, and each additional sound/animal pair requires
only one additional line of code.
Without function indirection (Gawk extension), or an eval feature
(doesn't exist in any Awk I know), you cannot dispatch actions, only >>substitute values for keys.
So what? The OP wasn't looking for a way to dispatch actions.
On 10 Oct 2025 08:10:56 GMT, Mack The Knife wrote:
This is where associative arrays shine:
BEGIN {
annimal["bark"] = "dog"
annimal["meow"] = "cat"
annimal["moo"] = "cow"
# etc. ...
}
{ print animal[$1] }
Much simpler, and each additional sound/animal pair requires
only one additional line of code.
I dont know, I'm torn here...
Associative arrays, as I'm sure you'd agree, are a quintessential
awk feature & really do make coding chores so much easier. And yet
for a short logic block, I think ternary has it beat:
scale = (unit == "T") ? 1e12 :
(unit == "B") ? 1e9 :
(unit == "M") ? 1e6 :
(unit == "K") ? 1e3 : 1
Nice, compact, even symmetrical looking. Doubly so one liners.
[...]
Oh, I don't know, for shorter constructs, especially one liners, awk
is plenty good in my thinking at least.
On 2025-10-11, Mike Sanders <porkchop@invalid.foo> wrote:
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.
It is nesting, but it is right associative. This is nesting:
if (c1) {
} else {
if (c2) {
} else {
if (c3) {
}
}
}
Its still nesting when we remove the extra braces around the
alternative ifs:
if (c1) {
} else
if (c2) {
} else
if (c3) {
}
The c3 if is a constituent expression (i.e. nested within)
the c2 if, which is nested within the c1 if.
Because the nesting is right-leaning we can think of it as linear.
and write it that way/
if (c1) {
} else if (c2) {
} else if (c3) {
}
Same like Lisp lists where we have a right-leaning structure
.
a .
b .
c nil
coresponding to the notation
(a . (b . (c . nil)))
which is preferentially spelled without the dots and extra
parenteheses as:
(a b c)
and so then we call that a list; and not a nested list.Only this kind of object is called a nested list:
(a (b c) d)
The cell structure is nested, and so is the explicit dot notation
for it. The list isn't nested, because the concept "list" is the name
for a right-leaning nesting of the cell structure, viewed linearly,
with an accompanying flat notation.
Actually, the existence of (uninitialized usable) associative arrays
is what contributes significantly to writing powerful one-liners (or
also many more complex things in a terse way) in Awk.
scale = (unit == "T") ? 1e12 :
(unit == "B") ? 1e9 :
(unit == "M") ? 1e6 :
(unit == "K") ? 1e3 : 1
On Sat, 11 Oct 2025 11:55:57 -0000 (UTC), Mike Sanders
<porkchop@invalid.foo> wrote:
scale = (unit == "T") ? 1e12 :
(unit == "B") ? 1e9 :
(unit == "M") ? 1e6 :
(unit == "K") ? 1e3 : 1
scale = 1000 ^ index("KMBT",unit)
On 2025-10-11, Mack The Knife <mack@the-knife.org> wrote:
In article <20251010100336.109@kylheku.com>,
Kaz Kylheku <643-408-1753@kylheku.com> wrote:
On 2025-10-10, Mack The Knife <mack@the-knife.org> wrote:
{ print animal[$1] }
Much simpler, and each additional sound/animal pair requires
only one additional line of code.
Without function indirection (Gawk extension), or an eval feature >>>(doesn't exist in any Awk I know), you cannot dispatch actions, only >>>substitute values for keys.
So what? The OP wasn't looking for a way to dispatch actions.
Nested/cascaded ternary will do that:
cond1 ? increment_this++
: cond2 ? call_this()
: foo[this] = that
It's just not shown in the minimal example where it yields
string literal values.