bug-gnu-utils
[Top][All Lists]
Advanced

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

gawk switch statement weirdness (was: Yet another thread...)


From: Stepan Kasal
Subject: gawk switch statement weirdness (was: Yet another thread...)
Date: Wed, 13 Aug 2003 10:08:23 +0200

[This message has also been posted.]
Hi Kenny,

On Tue, 12 Aug 2003 15:49:03 GMT, Kenny McCormack wrote:
>       switch ("abc") {
>         case /c/: print "Yes, it's got a 'c' in it"
>       }
> [...]
> Note, BTW, that this implies that the interpreter knows to do
> a ~ test when the thing is an RE, rather than a normal == test.

yes, that's very weird aspect.  When an awk (gawk) builtin uses
a regexp, it's a parameter which has to be regexp; if it's not
regexp, it's first converted to a string and then converted to
regexp, according to well known rules.  So I can safely replace
regexp parameter /\/a\/b\/c\// by "a/b/c" which often makes the
code more readable.  The situation with the ~ and !~ operators
is exactly the same.

The only exception is when one uses /foo/ instead of $0 ~ /foo/.
Such a usage is discouradged in all places except in action patterns,
though.  This may be viewed as an inconsistency but it has too long
roots in the awk tradition.

So the `case xxx:' context brings another inconsistency and special
case, which one has to be aware of.  I think this pollutes the clear
style and consistency of the awk language.

Yet another problem with the switch statement:
though you can do the following in C
        switch (x) {
        case MAX_SIZE: ...
        case (MAX_SIZE - 1): ...
        }
it's not possible in gawk, as it doesn't recognize the above
as "constant expressions".
In this sense, we can say that the switch statement is less powerful
then its C counterpart.

All this suggest that the switch statement, in its current form,
should be removed from gawk.

Have a nice day,
        Stepan Kasal




reply via email to

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