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 13:00:04 +0100

Thank you - it all works perfectly now.

I wonder why C and awk have such a peculiar default. Is there any use-case
where the dropping through makes sense?

I used case statements in Pascal quite a bit, and they worked as I'd
expect, and I never considered, for a moment, that it was an odd or
sub-optimal arrangement.

I can only think that it was, somehow, easier for the language designers,
who didn't have to put an implicit break before each 'case'.

On Sat, 27 Jun 2020 at 11:12, Peter Brooks <peter.h.m.brooks@gmail.com>
wrote:

> 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
>


-- 
Peter Brooks

Skype:  Fustbariclation
Twitter: Fustbariclation
Author Page: amazon.com/author/peter_brooks


reply via email to

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