bug-gawk
[Top][All Lists]
Advanced

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

Re: awk: switch - case statement (GNU Awk 5.0.1, API: 2.0 (GNU MPFR 4.0.


From: Peter Brooks
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:12:15 +0100

Thank you, that makes sense. I’ll try i.

Sent from my iPad

> On 27 Jun 2020, at 10:18, Manuel Collado <mcollado2011@gmail.com> wrote:
> 
> 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/null
>>> Hello
>>> fred
>> 
>> I'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



reply via email to

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