Is there a cleaner way to add "else if" to conditional assignment in Awk, etc.?

Some languages, such as awk script, allow conditional assignments. For example, let's say that you have a list file in the format:

<item name, no spaces> <price as float> 

eg.

 Grape 4.99 JuicyFruitGum 0.45 Candles 5.99 

And you wanted to tax everything over $ 1 ... you could use an awk script:

 awk '{a=($2>1.00)?$2*1.06:$2; print a}' prices.data 

... which uses conditional assignment to shorten the syntax.

But say that you also wanted to offer $ 1 for all items worth more than $ 20 and $ 2 apiece for more than $ 40. Well, in a language like c, you usually do something like:

 if (price > 40.00) { price-=2; price *= 1.06; } else if ( price > 20.00 && price <= 40.00 ) { price--; price *= 1.06; } else if ( price > 1.00 ) { price*=1.06; } 

... well, I found that you can clone awk or other scripting languages ​​into a COMPOUND assignment. eg:.

 awk '{a=($2>1.00)?($2>20.00)?($2-1)*1.06:($2>40.00)?($2-2)*1.06:$2*1.06:$2; print a}' prices.data 

My questions are that

a) is a complex assignment (such as this) universally compatible with scripting languages ​​that support conditional assignment?
b) Is there a non-kludge method for performing multiconventional assignment in an awk script?

To clarify: I am talking exclusively about the abbreviation for assignment (<...>? <...>: <...>; rather than the traditional conditional assignment, which I already know how to make a c-like connection assignment in Awk script. As a side note on why I can use the short form, I think the dignity is obvious - it's short, but like regular expressions, you might want to write a good description of what your obscure syntax does for posterity.

+4
source share
3 answers

a)

Bash, ksh and zsh support conditional (triple) operators (Bash does not perform float, though):

 for i in {3..5}; do for j in {2..6}; do for k in {2..4}; do (( a = i > j ? i > k ? j > k ? i * j : i * k : j * k : 0 )) echo $a done; done; done 

The syntax for the ternary PHP operator is similar.

Python is completely different:

 a = b if c else d if e else f if g else h 

Bash, et al, and Ruby (which also supports the form ?: Support this style (showing the version of Bash):

 [[ condition ]] && do_if_true || do_if_false 

what can be done in complex form. This can perform actions in addition to appointments.

b) no, not without explicit if / else (or resorting to even greater creativity kludginess .

+2
source

In fact, C also has an operator ? , so you can do the same (with an error) with C.

However, you are essentially writing code to write. If you had not told me that you made this expression, it would be very difficult to understand this. Why not just go and use if ?

You will thank yourself after 6 months, when you find the need to configure it.

0
source

(a) I think so

(b) Yes


As it happens, your "C" code is almost legal awk. The next slightly modified version works great as an awk program ...

 /./ { price = $1 if (price > 40.00) { price -= 2 price *= 1.06 } else if ( price > 20.00 && price <= 40.00 ) { price-- price *= 1.06 } else if ( price > 1.00 ) { price *= 1.06 } printf("%6.2f\n", price) } 

I took semicolons, but nothing bad will happen if you leave them in ...

0
source

All Articles