是否有更干净的方法来添加“else if” Awk 等中的条件赋值?
某些语言(例如 awk 脚本)允许条件赋值。例如,假设您有一个格式如下的列表文件:
<item name, no spaces> <price as float>
例如
Grape 4.99
JuicyFruitGum 0.45
Candles 5.99
,您想对超过 1 美元的所有内容征税...您可以使用 awk 脚本:
awk '{a=($2>1.00)?$2*1.06:$2; print a}' prices.data
...它使用条件赋值来缩短语法。
但假设您还想为所有超过 20 美元的商品提供 1 美元折扣,为超过 40 美元的商品提供 2 美元折扣。在像 c 这样的语言中,您通常会执行以下操作:
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;
}
... 好吧,我发现您可以将 awk 或其他脚本语言混入 COMPOUND 赋值中。例如:
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
我的问题是
a) 复合赋值(像这样)通常与支持条件赋值的脚本语言普遍兼容吗?
b) 是否有一种非拼凑的方法可以在 awk 脚本中进行多条件赋值?
澄清一下:我只讨论赋值的简写 (<...>?<...>:<...>;,而不是传统的条件赋值,我已经知道如何在 Awk 脚本中进行类似于 C 的复合赋值,作为旁注,至于为什么我可能使用速记,我认为它的优点是显而易见的 - 但就像正则表达式一样,您可能想要这样做。为了后代的利益,写下你令人困惑的语法的作用的一个很好的描述。
Certain languages like awk script allow for conditional assignments. For example, say you had a list file in the format:
<item name, no spaces> <price as float>
e.g.
Grape 4.99
JuicyFruitGum 0.45
Candles 5.99
And you wanted to tax everything over $1... you could use the 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 you wanted to also offer $1 off all items over $20 and $2 off items over $40. Well in a language like c you would typically 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 discovered you could kludge awk or other scripting languages into COMPOUND assignment. e.g.:
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 compound assignment (like this) generally universally compatible with scripting languages that support conditional assignment?
b) Is there a non-kludge way to do multi-conditional assignment in awk script?
To clarify: I am talking exclusively about the shorthand for assignment (<...>?<...>:<...>;, not traditional conditional assignment, which I already know how to do c-like compound assignment for in Awk script. As a side note, as to why I might use shorthand, I think the merit is obvious -- that it's short. But like regexes, you might want to write a good description of what your confusing syntax does for posterity's sake.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
a)
Bash、ksh 和 zsh 支持复合条件(三元)运算符(但 Bash 不支持浮点数):
PHP 的三元运算符语法类似。
Python 的情况截然不同:
Bash 等和 Ruby(也支持
?:
形式)支持这种样式(显示 Bash 的版本):可以以复合形式完成。除了分配之外,它还可以执行操作。
b)不,没有明确的
if
/else
(或者诉诸更多的kludginess创造力。a)
Bash, ksh and zsh support compound conditional (ternary) operators (Bash doesn't do floats, though):
PHP's ternary operator syntax is similar.
Python's is quite different:
Bash, et al, and Ruby (which also supports the
?:
form) support this style (showing Bash's version):which can be done in a compound form. This can perform actions in addition to assignments.
b) no, not without explicit
if
/else
(or resorting to even morekludginesscreativity.实际上,C 也有
?
运算符,因此您可以使用 C 做同样的(丑陋的)事情。但是,您实际上是在那里编写只写代码。如果你没有告诉我那个 awk 语句做了什么,就很难弄清楚。为什么不直接使用
if
呢?6 个月后,当您发现需要对其进行调整时,您会感谢自己。
Actually, C has the
?
operator too, so you could do the same (fugly) thing with C.However, you are essentially making write-only code there. If you hadn't told me what that awk statement did, it would be very difficult to figure it out. Why not just go ahead and use
if
?You'll thank yourself in 6 months when you discover a need to tweak it.
(a) 我想是的
(b)
是的 碰巧,你的“C”代码几乎是合法的 awk。下面稍微调整的版本作为 awk 程序工作得很好...
我把分号去掉了,但是如果你把它们留在里面,也不会发生什么坏事...
(a) I think so
(b) Yes
As it happens, your "C" code is almost legal awk. The following slightly tweaked version works just fine as an awk program...
I took the semicolons out, but nothing bad happens if you leave them in...