help-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Make awk more friendly to multiline function declaration


From: Manuel Collado
Subject: Re: Make awk more friendly to multiline function declaration
Date: Mon, 28 Mar 2022 11:12:06 +0200
User-agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.5.0

El 27/03/2022 a las 13:57, Peng Yu escribió:
I see the following error.

$ awk 'function f(x
, y) {
   return x+y
}
BEGIN { print f(1, 2) }'
awk: cmd. line:2: function f(x
awk: cmd. line:2:             ^ unexpected newline or end of string

If I move the comma before y to the line of x, the error disappears.

Adding a backslash after x also solves the problem, but it seems that this
requirement is artificial. Because the parentheses enclose the arguments x
and y, there is no way that people should mistake them as being separate
statements. Therefore, I think the grammar of awk should be made more
user-friendly, in this case, by removing the requirement of backslash after
"x" when there is a comma in the next line. Does this suggestion make sense?

I don't think so. An EOL means the end of a statement, except if there is a clear indication that it will be continued in the next line. In your example the backslash is not necessary. Just put the comma at the end of the first line instead of at the beginning of the second line:

$ awk 'function f(x,
 y) {
  return x+y
}
BEGIN { print f(1, 2) }'
3

Regards.
--
Manuel Collado - http://mcollado.z15.es



reply via email to

[Prev in Thread] Current Thread [Next in Thread]