[comp.lang.pascal] CASE for Strings?

ce1zzes@prism.gatech.EDU (Eric Sheppard) (04/14/90)

I wish to perform functions according to commands parsed into a string array.
The CASE operator only works for simple variable types. Is there an elegant way
to perform the CASE for complex (string) variables? Desired sample construct:

case Command of
  '1STCOM' : FirstCommand;
  '2NDCOM' : SecondCommand;
   .
   .
end;

Eric, tinkerer-at-large
-- 
Eric Sheppard      Georgia Tech    |  "Of course the US Constitution isn't
Atlanta, GA                        | perfect; but it's a lot better than what
ARPA: ce1zzes@prism.gatech.edu     | we have now."
uucp: ...!{allegra,amd,hplabs,seismo,ut-ngp}!gatech!prism!ce1zzes

mlw@pyr.gatech.EDU (Michael Williams) (04/14/90)

In article <8001@hydra.gatech.EDU> ce1zzes@prism.gatech.EDU (Eric Sheppard) writes:
>I wish to perform functions according to commands parsed into a string array.
>The CASE operator only works for simple variable types. Is there an elegant way
>to perform the CASE for complex (string) variables? Desired sample construct:
>
>case Command of
>  '1STCOM' : FirstCommand;
>  '2NDCOM' : SecondCommand;
>   .
>   .
>end;

   The closest constuct I know of is :

   valid := '1STCOM 2NDCOM 3RDCOM 4THCOM 5THCOM';
   case pos(command,valid) of
      1 : FirstCommand;
      8 : SecondCommand;
     15 : ThirdCommand;
     22 : FourthCommand;
     29 : FifthCommand;
     else write('Invalid command!');
   end;

   This sounds like a scanner for a compiler, where you are checking for
keywords.  In this case, a 'perfect' hashing function would be preferable.

Hope this helps ...

-- 
Michael Williams
Georgia Institute of Technology, Atlanta Georgia, 30332
uucp: ...!{akgua,allegra,amd,hplabs,ihnp4,seismo,ut-ngp}!gatech!gitpyr!mlw
ARPA: mlw@pyr.gatech.edu

ce1zzes@prism.gatech.EDU (Eric Sheppard) (04/14/90)

In article <10010@pyr.gatech.EDU> mlw@pyr.UUCP (Michael Williams) writes:
>
>   This sounds like a scanner for a compiler, where you are checking for
>keywords.  In this case, a 'perfect' hashing function would be preferable.
>
>Hope this helps ...

Right you are. I'm working on a simple LOGO-like language interpreter/compiler,
for a programming course. Is there a better way to check for keywords/commands?

Eric, tinkerer-at-large
-- 
Eric Sheppard      Georgia Tech    |  "Of course the US Constitution isn't
Atlanta, GA                        | perfect; but it's a lot better than what
ARPA: ce1zzes@prism.gatech.edu     | we have now."
uucp: ...!{allegra,amd,hplabs,seismo,ut-ngp}!gatech!prism!ce1zzes

mathrich@mthvax.cs.miami.edu (Rich Winkel) (04/14/90)

In <8001@hydra.gatech.EDU> ce1zzes@prism.gatech.EDU (Eric Sheppard) writes:
>I wish to perform functions according to commands parsed into a string array.
>The CASE operator only works for simple variable types. Is there an elegant way
>to perform the CASE for complex (string) variables? Desired sample construct:

>case Command of
>  '1STCOM' : FirstCommand;
>  '2NDCOM' : SecondCommand;
>   .
>   .
>end;

case ((pos(' '+Command+' ',' 1STCOM 2NDCOM QUIT   3RDCOM ...')-1) DIV 7) of 
   0: firstcommand;
   1: secondcommand;

or something similar.  Not elegant, but usable.

Rich