bug-gawk
[Top][All Lists]
Advanced

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

awk: switch - case statement (GNU Awk 5.0.1, API: 2.0 (GNU MPFR 4.0.2, G


From: Peter Brooks
Subject: 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 09:35:08 +0100

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

I'm also a bit puzzled why, on linux, I need the $(pwd) before it finds a
file - on Macos, it looks in the local directory and finds it.

That's not all. A more representative program also behaves nothing at all
like what I'd expect:

BEGIN   {

        x=1;y=2;margin=1;

        x_max=42;y_max=55;

        x_min=0;y_min=0;

        top="top";

        right="right";

        bottom="bottom";

        left="left";

        over="over";

        status="top";

        print "x: " x " y: " y " status: " status;

        while ( status != "over")

                switch (status)

                    {

                    case "top" :++x;print "(" x ":" y ") " status;if ( x ==
> x_max - margin) status=right;           # top

                    case "right" :++y;print "(" x ":" y ") " status;if ( y
> == y_max - margin) status=bottom;    # right

                    case "bottom" :--x;print "(" x ":" y ") " status;if ( x
> == x_min + margin) status=left;         # bottom

                    case "left" :--y;print "(" x ":" y ") " status;if ( y
> == y_min + margin) status=over;           # left

                    }

        print status;

        }


Output:

x: 1 y: 2 status: top

(2:2) top

(2:3) top

(1:3) top

(1:2) left

(1:1) left

over



This is supposed to print out a list of vectors, from
(2:2)..(41:2)(41:3)..(41:54) and so on.

I am quite baffled. I've tried these on MacOS too (Catalina 10.15.5) - GNU
Awk 5.1.0, API: 3.0 (GNU MPFR 4.0.2, GNU MP 6.2.0)

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