[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: |
david kerns |
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 05:18:46 -0700 |
awk works that way because C does.
C does because it's a glorified 'goto'. consider this:
switch (i) {
case 1:
case 2:
case 3:
case 5:
case 7: p = true; break;
default: p = false;
}
if there was an "auto break" after each case you'd have the code in case 7
repeated four more times.
On Sat, Jun 27, 2020 at 5:00 AM Peter Brooks <peter.h.m.brooks@gmail.com>
wrote:
> 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
>
- 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, Peter Brooks, 2020/06/27
- 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, Manuel Collado, 2020/06/27
- 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, Peter Brooks, 2020/06/27
- 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, Peter Brooks, 2020/06/27
- 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,
david kerns <=
- 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, Peter Brooks, 2020/06/27