On 12.10.2017 01:06, Ed Morton wrote:
> On 10/11/2017 3:56 PM, Janis Papanagnou wrote:
>> On 11.10.2017 22:10, Ed Morton wrote:
>>>> [...]
>>>
>>> switch statements are gawk-specific and don't actually add much value (I don't
>>> recall ever having used one in the past 25 years of using awk whereas I've
>>> used them thousands of times in shell, C, etc.).
>>
>> You should be aware of the fact that GNU awk's 'switch' statement (unlike C)
>> supports regexps; a useful property. And you can also compare strings (which
>> needs bulky if-cascades in C); another useful property (if compared to C).
>
> Yes, I'm aware, I just don't see the benefits as outweighing the loss in
> portability to other awks or simply the need for me (or anyone else reading
> the code) to have to be aware of the syntax and semantics of switch statements.
Portability can often be an issue, I agree. WRT the switch syntax, well, given
your statement it seems to be personal preference. I don't see much difference
in learning syntactical constructs of any programming languages, specifically
not in awk which has not a big set of control constructs. But YMMV, of course.
I used GNU awk's 'switch' in the past, and while I often missed and cursed C
for the inability to compare strings in 'switch' statements I appreciated
its availability in GNU awk all the more, and also its logical extension to
regexps.
Erm, in C there's also the 'break' statement, and if omitted in a 'switch'
branch the fall-through effect (that you observe in GNU awk as well) becomes
visible. It seems that the GNU awk people implemented 'switch' the same way
as in C so that folks who don't want new syntax and semantics here won't
have to adapt to something new. Actually, in GNU awk, only the possibility
to define /.../ and "..." values to compare against is an extension, and a
straightforward and obvious one I'd say.
To prevent the fall-through effect in both, C and GNU awk, you have to use
an explicit 'break' statement. Personally - having learned Pascal before C -
I was repelled by that ballast, but for most people comming from C language
family with its, erm, comparatively ugly syntax it should not be an issue;
they already know from either C, or C++, or Java, etc. etc. - and from gawk.
You also mention shell behaviour here; note that shell syntax is influenced
by Algol 68; Algol 'case' constructs are only marginally comparable, but
(as in Pascal) they don't require 'break's. In shell there's the ';;' symbol
to 'break' a branch; as a small syntactical token it is less bulky compared
to C's (and gawk's) 'break', but it's there. There's also a fall-through
token ';&' invented by Kornshell decades ago (probably also available in
bash meanwhile, haven't tried) to achieve what you get in C and GNU awk,
i.e. the behaviour you have been puzzled about.
(We've been digressing it seems. :-)
Janis