On 11/6/2023 5:14 AM, Janis Papanagnou wrote:
switch (some_variable) [
case 42: ...
case "string": ...
case /pattern/: ...
Nitpick - it's
case /regexp/:
rather than:
case /pattern/
The word "pattern" is ambiguous and misused all over awk documentation.
You can make some argument for it in:
pattern { action }
since that includes `BEGIN`, integers, etc. I'd argue that should be:
condition { action }
but in the "case" statement above what goes inside `/.../` is simply and always a regexp.
[...]
One point you may want to consider is the trim() function; the
two substitutions can be combined in one
sub (/^[[:space:]]+(.*)[[:space:]]+$/, "&", str)
(but test that in your Awk versions before using it; "&" is an
old feature but off the top of my head I'm not sure whether the
subexpression with parenthesis /...(...).../ is generally
supported in other Awks).
You can write that, but it's not a capture group that can be
backreferenced from the replacement and if it was "&" wouldn't refer to
the string that matched ".*" anyway, it'd refer to the string that
matched the whole regexp.
You could use a capture group in GNU awk for gensub():
str = gensub (/^[[:space:]]+(.*)[[:space:]]+$/, "\\1", 1, str)
and in most awks you could do:
gsub (/^[[:space:]]+|[[:space:]]+$/, "", str)
but there are some out there that will fail to do both substitutions for
that case (tawk and nawk maybe?) and so you need:
sub(/^[[:space:]]+/, "", str)
sub(/[[:space:]]+$/, "", str)
[...]
One point you may want to consider is the trim() function; the
two substitutions can be combined in one
sub (/^[[:space:]]+(.*)[[:space:]]+$/, "&", str)
(but test that in your Awk versions before using it; "&" is an
old feature but off the top of my head I'm not sure whether the
subexpression with parenthesis /...(...).../ is generally
supported in other Awks).
You can write that, but it's not a capture group that can be
backreferenced from the replacement and if it was "&" wouldn't refer to
the string that matched ".*" anyway, it'd refer to the string that
matched the whole regexp.
Using my Awk it does what advertised. I merely didn't find it clearly documented whether the '&' is generally guaranteed to refer to the
grouping.
Ping Janis: Question... Are you interested in rewriting this
as a Gawk only implementation? Would be great for switch/case
statements IMO. If so, I'll add your version to the file at
my website.
[*] https://www.gnu.org/software/gawk/manual/gawk.html#Switch-Statement
The cppawk preprocessor supports a case macro which
compiles to the switch statement for Gawk, or to portable
Awk code.
The macro is documented in its own man page:
https://www.kylheku.com/cgit/cppawk/tree/cppawk-case.1
case is safer than switch because it doesn't have implicit
"fallthrough".
Each case must end with one of: cbreak, cfall or cret: break,
fallthrough or return.
On 07.11.2023 12:48, Ed Morton wrote:<snip>
and in most awks you could do:
gsub (/^[[:space:]]+|[[:space:]]+$/, "", str)
Yes, this is a sensible alternative.
but there are some out there that will fail to do both substitutions for
that case (tawk and nawk maybe?) and so you need:
Really? But why? - Alternatives in regexp is certainly an old feature
(at least since nawk in the 1980's).
On 11/7/2023 7:02 AM, Janis Papanagnou wrote:
On 07.11.2023 12:48, Ed Morton wrote:<snip>
and in most awks you could do:
gsub (/^[[:space:]]+|[[:space:]]+$/, "", str)
Yes, this is a sensible alternative.
but there are some out there that will fail to do both substitutions for >>> that case (tawk and nawk maybe?) and so you need:
Really? But why? - Alternatives in regexp is certainly an old feature
(at least since nawk in the 1980's).
In my opinion it's just a bug. It was demonstrated to me when I posted
an answer on Stack Overflow several years ago that I can't find right
now. I know it's not gawk and I'm about 99% sure it's neither BSD awk
nor /usr/xpg[46]/bin/awk so the only non-oawk awks I can imagine would
have this problem are nawk, tawk (I'm about 80% sure I remember tawk is
one that DOES have the problem), and/or busybox awk, none of which I
have access to, so if anyone has and could test them by running:
$ echo ' foo ' | awk '{gsub(/^ +| +$/,""); print "<" $0 ">"}' <foo>
and let us know which don't produce that output, that'd be great.
Ed.
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 1,064 |
Nodes: | 10 (0 / 10) |
Uptime: | 163:53:34 |
Calls: | 13,691 |
Calls today: | 1 |
Files: | 186,936 |
D/L today: |
9,181 files (2,736M bytes) |
Messages: | 2,411,516 |