Mombu the Programming Forum sponsored links

Go Back   Mombu the Programming Forum > Programming > switch statement in Gawk 3.1.3 (Was: Re: Gawk 3.1.3 now available)
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 18th May 00:28
peter s tillier
External User
 
Posts: 1
Default switch statement in Gawk 3.1.3 (Was: Re: Gawk 3.1.3 now available)


Is this a "home-brew" Kenny?


[...]

--
Peter S Tillier
"Who needs perl when you can write dc, sokoban,
arkanoid and an unlambda interpreter in sed?"
  Reply With Quote


  sponsored links


2 18th May 00:28
gazelle
External User
 
Posts: 1
Default switch statement in Gawk 3.1.3 (Was: Re: Gawk 3.1.3 now available)


Actually, no. It is a pretty well-known language/company, based on the
west coast of North America (not California).


But it is pretty clever, isn't it? Thinking about how "switch" would be
implemented, there doesn't really seem to be any good reason (besides
tradition) why the case labels would have to be constants (as opposed to
being arbitrary expressions).
  Reply With Quote
3 18th May 00:29
ted davis
External User
 
Posts: 1
Default switch statement in Gawk 3.1.3 (Was: Re: Gawk 3.1.3 now available)


I see no reason why it couldn't have both, other than the example
offered by Perl of what can happen to a simple language when somebody
gets carried away. However, I also see no reason why something so
fundamentally different should have the same name as a traditional
construct. An expression based switch shouldn't be called 'switch' in
a C based context - it should have a different name, perhaps
'firstrue'.


T.E.D. (tdavis@gearbox.maem.umr.edu - e-mail must contain "T.E.D." or my .sig in the body)
  Reply With Quote
4 18th May 00:29
peter s tillier
External User
 
Posts: 1
Default switch statement in Gawk 3.1.3 (Was: Re: Gawk 3.1.3 now available)


As Kenny says, it is sensible to consider the possible use of an
arbitrary expression as a case "label" at this point. It would be
inappropriate to do so later once the c-switch becomes established, as I
sure it will: it provides additional benefits already, especially where
the case label is an RE.

If it's OK for the case label to be an RE, why not an arbitrary
expression as has been suggested?


But that's the nature of computer programs in any event. Most forms of
case construction can be ordered to ensure that the programmer's logical
requirements are met.

In Pascal [mine's a little rusty ;-)] you might write something like
this:

if j in [ ' ', '{', '}' ] then
case j of
' ':
...
'{':
...
'}':
...
end (* case *)
else
errout("Error in case selector ...");

or

if j in [ ' ', '{', '}' ] then
case j of
'}':
...
' ':
...
'{':
...
end (* case *)
else
errout("Error in case selector ...");

where the ordering of code associated with the case labels may make the
program differently depending upon that precise ordering - Pascal
doesn't force you to place the case labels in a particular order (at
least none of the implementations that I have used over the years do.)

--
Peter S Tillier
"Who needs perl when you can write dc, sokoban,
arkanoid and an unlambda interpreter in sed?"
  Reply With Quote
5 18th May 00:29
peter s tillier
External User
 
Posts: 1
Default switch statement in Gawk 3.1.3 (Was: Re: Gawk 3.1.3 now available)


OTOH that doesn't mean that the PL/I select was such a bad idea. As you
say Mike Cowlishaw decided to put something like it into REXX. I like
it a lot.

--
Peter S Tillier
"Who needs perl when you can write dc, sokoban,
arkanoid and an unlambda interpreter in sed?"
  Reply With Quote
6 18th May 00:29
gazelle
External User
 
Posts: 1
Default switch statement in Gawk 3.1.3 (Was: Re: Gawk 3.1.3 now available)


Actually, my testing indicates that it is not OK for a case label to be an RE.
It doesn't generate a syntax error, but it doesn't seem to work either.

I.e., case /foo/:
compiles, but doesn't "work". case (/foo/):
generates a syntax error. I'm guessing that there is some obscure reason
why /foo/ can be parsed as an arithmetic expression - note that there are
some edge cases (that have shown up in various threads in this NG over
the years) where the "/" in an intended RE gets interpreted as arithmetic
division.
  Reply With Quote
7 18th May 00:29
peter s tillier
External User
 
Posts: 1
Default switch statement in Gawk 3.1.3 (Was: Re: Gawk 3.1.3 now available)


No offence, but, are you sure that you config'd in the compile support
for the switch "configure --enable-switch"? And anything else like the
strftime support too?

--
Peter S Tillier
"Who needs perl when you can write dc, sokoban,
arkanoid and an unlambda interpreter in sed?"
  Reply With Quote
8 18th May 00:30
peter s tillier
External User
 
Posts: 1
Default switch statement in Gawk 3.1.3 (Was: Re: Gawk 3.1.3 now available)


Kenny,
I recompiled the gawk-3.1.3-2 sources under Cygwin and tried out the
example from p104, section 6.4.5 "The switch Statement" this example
works fine for me and appears to support case labels of literals or
arbitrary regular expressions.

Also quoting Kenny from an earlier message:

|- Agreed. In fact, in a certain other language (that no one here has
|- heard of, so I won't mention it), there is a SWITCH statement that
|- works as |- a nice alternative to multiple if-then-else. It works
|- like this:
|-
|- switch 1
|- case expr1:
|- ...
|- break
|- case expr2:
|- ...
|- break
|- ...
|- endswitch
|-
|- And the result is that the code associated with the first exprN that
|- is true (i.e., returns 1) gets executed. Clever, eh? I had hoped
|- that Gawk's new switch statement would work like this, but alas, no
|- joy.
|-
|- Observe:
|-
|- {
|- switch (1) {
|- case p("foo"): print "Good Food!";break
|- case p("bar"): print "Bart...";break
|- }
|- }
|- function p(s) {
|- print "here we are in p(), with arg:",s
|- return s == "bar"
|- }
|-
|- which, alas, generates:
|-
|- %) ./gawk -f ***
|- gawk: ***:3: case p("foo"): print "Good Food!";break
|- gawk: ***:3: ^ syntax error
|- gawk: ***:4: case p("bar"): print "Bart...";break
|- gawk: ***:4: ^ syntax error
|- gawk: ***:6: }
|- gawk: ***:6: ^ syntax error
|- %)

? did you try semicolons before the "print"", say:
case p("bar"):; print "..."; break

|-
|- The point seems to be that the case args have to be constants.
|- Pity.
|-

But not so because the example from the manual pages does work and has
arbitrary REs as case labels.

|- Interestingly enough:
|-
|- case /foo/:
|-
|- doesn't generate a syntax error, but doesn't work. |-
|- >A natural extension to this would be whilecase(), a while loop
|- >which has pattern/action pairs, and special patterns called BEGIN
|- >(run before the loop is entered) and END (run after the loop ends),
|- >meaning the normal awk processing could be written as this awk
|- >program:
|-
|- Yes, indeed. I've had to change:
|-
|- /foo/ { do_something() }
|-
|- to:
|-
|- { while (/foo/) do_something() }
|-
|- for just this reason..

--
Peter S Tillier
"Who needs perl when you can write dc, sokoban,
arkanoid and an unlambda interpreter in sed?"
  Reply With Quote
9 18th May 00:30
don
External User
 
Posts: 1
Default Gawk 3.1.3 now available


The C switch statement allows the compiler to optimise the dispatch; if
you have constant, scalar target values, you can do things like table
lookups to choose your target. Anything more complicated starts to look
a lot like a series of chained if / else, with every condition being
tried until you find one that evaluates true.

C switch does give you the ability to fall through, which if / else if
doesn't; frankly, I've not missed that ability with awk. At worst, you
end up duplicating odd bits of code here and there.

Other than that, C switch is largely a performance hack. If you have
lots of targets, the compiler doesn't need to sequentially try each
condition, and conditional branches can be expensive. If gawk's switch
isn't able to do a large switch faster than the equivalent sized if /
else if / ... / else block, I don't see a hell of a lot of point.

-- don
  Reply With Quote
10 18th May 00:30
peter s tillier
External User
 
Posts: 1
Default Gawk 3.1.3 now available


Yes, but it can only optimize the dispatch when cases aren't allowed to drop through, surely.

I still think that the use of the term "c-switch" in this context is a
misnomer: it's a much more sensible and, if it were possible to use
arbitrary expressions as case labels, alongside literals and arbitrary
RE expressions, a much more useful construction than a real "c-switch".

A plea to rename it the "gawk-switch" and let's all draw a veil over the
"c-switch".

--
Peter S Tillier
"Who needs perl when you can write dc, sokoban,
arkanoid and an unlambda interpreter in sed?"
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666