[comp.lang.pascal] wish i could figure this out for myself

tpehrson@javelin.sim.es.com (Tim Clinkenpeel) (05/03/91)

a routine i have makes quite a few checks to see if a given character is in a
set of characters.  i am unable to determine the proper format and data types
to accomplish this.  here's an example (not working example) of what i'm trying
to do:

procedure imstumped;
 type cantsee = set of char;
 begin
  cantsee:='SWU&#:$';
  if givenkey in cantsee then ...

this is highly abbreviated from my original code just for the sake of example.
-- 
   /=============================/ 
  / tpehrson@javelin.sim.es.com /      i will work for food 
 /=====aka: tim clinkenpeel====/ 
[DISCLAIMER: send SASE to: disclaimer, 84151-0521]

granoff@vaxwrk.enet.dec.com (Mark H. Granoff) (05/03/91)

In article <1991May3.140719.5931@javelin.sim.es.com>,
 tpehrson@javelin.sim.es.com (Tim Clinkenpeel) writes:
>
>a routine i have makes quite a few checks to see if a given character is in a
>set of characters.  ...
>
>procedure imstumped;
> type cantsee = set of char;
> begin
>  cantsee:='SWU&#:$';
>  if givenkey in cantsee then ...
>

Close...  Try something like this:

FUNCTION ValidChar(Ch:Char):Boolean;
  CONST
    ValidChars : SET OF Char = ['A'..'Z','a'..'z','0'..'9'];
  BEGIN
    ValidChar := Ch IN ValidChars;
  END;

Then you can say, in your code:

  IF ValidChar(Ch) THEN
	...
  ELSE
	...

Hope this help.

---------------------------------------------------------------------------
Mark H. Granoff   |    Enterprise Integration Services/Engineering VAXworks
---------------------------------------------------------------------------
Digital Equipment Corporation | Internet: granoff@vaxwrk.enet.dec.com
129 Parker Street             | Usenet  : ...!decwrl!vaxwrk.enet!granoff
PKO2-1/M21                    | AT&T    : +1 508 493 4512
Maynard, MA 01754             | FAX     : +1 508 493 2240
---------------------------------------------------------------------------
Opinions herein are my own and do not necessarily reflect those of Digital.
---------------------------------------------------------------------------

tpehrson@javelin.sim.es.com (Tim Clinkenpeel) (05/04/91)

granoff@vaxwrk.enet.dec.com (Mark H. Granoff) writes:
>Close...  Try something like this:

>FUNCTION ValidChar(Ch:Char):Boolean;
>  CONST
>    ValidChars : SET OF Char = ['A'..'Z','a'..'z','0'..'9'];
>  BEGIN
>    ValidChar := Ch IN ValidChars;
>  END;

yeah, i had considered doing it this way but i thought it was sloppy.
besides, i really like to know how to do such a thing exclusively.

in any event, thanks for your help.  i may have to resort to using the above 
method (if nothing else turns up here on c.s.p)
-- 
   /=============================/ 
  / tpehrson@javelin.sim.es.com /      i will work for food 
 /=====aka: tim clinkenpeel====/ 
[DISCLAIMER: send SASE to: disclaimer, 84151-0521]

smith@ctron.com (Larry Smith) (05/04/91)

In article <1991May3.170232.7710@javelin.sim.es.com> tpehrson@javelin.sim.es.com writes:
>granoff@vaxwrk.enet.dec.com (Mark H. Granoff) writes:
>>Close...  Try something like this:
...perfectly serviceable piece of code demonstrating textbook use
of charsets deleted for brevity...
>yeah, i had considered doing it this way but i thought it was sloppy.
>besides, i really like to know how to do such a thing exclusively.

Just what do you think you are trying to *do*?  The suggested code
is perfectly straightforward, utterly standard Pascal.
How do you come to the "sloppy" conclusion?

Your non-working example seemed to show you were not clear on the
distinction between the SET OF CHAR and STRING types.  The suggested
code fragment demonstrated exactly what you wanted to do, and with
excellent style.  Just what are you looking for?

Larry Smith
smith@ctron.com

-- 

Larry Smith
smith@ctron.com
The usual disclaimer stuff...

ricki@john.informatik.rwth-aachen.de (Richard Breuer) (05/06/91)

tpehrson@javelin.sim.es.com (Tim Clinkenpeel) writes:

>granoff@vaxwrk.enet.dec.com (Mark H. Granoff) writes:
>>Close...  Try something like this:

>>FUNCTION ValidChar(Ch:Char):Boolean;
>>  CONST
>>    ValidChars : SET OF Char = ['A'..'Z','a'..'z','0'..'9'];
>>  BEGIN
>>    ValidChar := Ch IN ValidChars;
>>  END;

>yeah, i had considered doing it this way but i thought it was sloppy.
>besides, i really like to know how to do such a thing exclusively.

>in any event, thanks for your help.  i may have to resort to using the above 
>method (if nothing else turns up here on c.s.p)
>-- 
>   /=============================/ 
>  / tpehrson@javelin.sim.es.com /      i will work for food 
> /=====aka: tim clinkenpeel====/ 
>[DISCLAIMER: send SASE to: disclaimer, 84151-0521]

In fact there might be one better (that means faster) solution. I did not
test whether it is so, so do it yourself, if you wish. Here it is:

FUNCTION ValidChar(Ch:Char):Boolean;
  CONST
    ValidChars = 'ABCDEFHIJKLMNO'; (* and so on... *)
  BEGIN
    ValidChar := Pos(Ch,ValidChars)>0;
  END;

Ricki.

milne@ics.uci.edu (Alastair Milne) (05/08/91)

In <ricki.673528313@rwthi3> ricki@john.informatik.rwth-aachen.de (Richard Breuer) writes:
>FUNCTION ValidChar(Ch:Char):Boolean;
>  CONST
>    ValidChars = 'ABCDEFHIJKLMNO'; (* and so on... *)
>  BEGIN
>    ValidChar := Pos(Ch,ValidChars)>0;
>  END;

  If it is faster to
  - push a string parameter (actually 2 of them)
  - call a function
  - scan the string doing substring checks
  - return the result

  than to do set inclusion, which should be done with a bitmask,
  then I will be quite surprised.

  Sets are one of Pascal's greatest features (greater, of course,
  in dialects that let you have nice big ones).  Use them in good health.


  Alastair Milne