Logo Questions Linux Laravel Mysql Ubuntu Git Menu

Why is the order of evaluation of expressions used for concatenation undefined in Awk?



In GNU Awk User's Guide, I went through the section 6.2.2 String Concatenation and found interesting insights:

Because string concatenation does not have an explicit operator, it is often necessary to ensure that it happens at the right time by using parentheses to enclose the items to concatenate.

Then, I was quite surprised to read the following:

Parentheses should be used around concatenation in all but the most common contexts, such as on the righthand side of ‘=’. Be careful about the kinds of expressions used in string concatenation. In particular, the order of evaluation of expressions used for concatenation is undefined in the awk language. Consider this example:

   a = "don't"
   print (a " " (a = "panic"))

It is not defined whether the second assignment to a happens before or after the value of a is retrieved for producing the concatenated value. The result could be either ‘don't panic’, or ‘panic panic’.

In particular, in my GNU Awk 5.0.0 it performs like this, doing the replacement before printing the value:

$ gawk 'BEGIN {a = "dont"; print (a " " (a = "panic"))}'
dont panic

However, I wonder: why isn't the order of evaluation of expressions defined? What are the benefits of having "undefined" outputs that may vary depending on the version of Awk you are running?

like image 389
fedorqui 'SO stop harming' Avatar asked Jan 08 '21 12:01

fedorqui 'SO stop harming'

People also ask

How do you concatenate in awk?

It does not have a specific operator to represent it. Instead, concatenation is performed by writing expressions next to one another, with no operator. For example: $ awk '{ print "Field number one: " $1 }' mail-list -| Field number one: Amelia -| Field number one: Anthony …

Which append operator used in awk is?

AWK - String Concatenation Operator.

1 Answers

This particular example is about expressions with side-effects. Traditionally, in C and awk syntax (closely inspired by C), assignments are allowed inside expressions. How those expressions are then evaluated is up to the implementation.

Leaving something unspecified would make sure that people don't use potentially confusing or ambiguous language constructs. But that assumes they are aware of the lack of specification.

like image 159
Henk Langeveld Avatar answered Nov 15 '22 11:11

Henk Langeveld