|
From: | Manuel Collado |
Subject: | Re: awk: switch - case statement (GNU Awk 5.0.1, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.2.0) - Linux Ubuntu 20.04 |
Date: | Sat, 27 Jun 2020 11:18:45 +0200 |
User-agent: | Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.5.0 |
El 27/06/2020 a las 10:35, Peter Brooks escribió:
I'm finding myself baffled. I've never used the awk 'switch' statement before, so I may be doing something wrong, but it seems bizarre to me. Here's my test: BEGIN {fred="hello"; switch (fred){ case "jim": print "Jim"; case "hello": print "Hello"; case "fred" : print "fred"; } }The output is: $ gawk -f $(pwd)/case.awk </dev/nullHello fredI'm completely baffled. I'd expect the output to be only the 'Hello'!
From the gawk manual: "Control flow in the switch statement works as it does in C. Once a match to a given case is made, the case statement bodies execute until a break, continue, next, nextfile, or exit is encountered, or the end of the switch statement itself." So you need: BEGIN { fred="hello"; switch (fred){ case "jim": print "Jim"; break case "hello": print "Hello"; break case "fred" : print "fred"; break } } -- Manuel Collado - http://mcollado.z15.es
[Prev in Thread] | Current Thread | [Next in Thread] |