CheckWilliamOut wrote:
> Dear all,
>
> Why does Pascal limit functions you can perform on Strings and Chars.
> There are two functions
> in particular that only work with Intergers and other number types:
>
> The Case statment and the "and", "or" and "xor" operators. Check these
> illegal examples:
>
> ----------------------------------------------
>
> var
> This_word : string[30];
>
> Begin
> Case This_word of
> 'hello' : Write('Greetings');
> 'done' : Write('It is Finished');
> end;
> end.
I apologize beforehand, but I find this kind of use of 'CASE' sort of
disgusting. May be I'm a purist, but IMHO if ANY programming language
would sup****t this I would steer clear from that. Here I would
design an enumerated type to do the job. That will be easier to maintain
and expand, say, if you later decide to give the same response to
user inputs other than 'hello'. E.g. you may want to give the same
response to 'Hi', 'hello', 'howdy' etc.
>
> ----------------------------------------------
>
> var
> This_letter : char;
>
>
> begin
> If this_letter <> 'a' or 'b' then
> Writeln('Neither A nor B were detected');
> end.
>
Hopefully you are aware that this doesn't work the way a beginner
might think with integers either. E.g. consider the snippet
var
this_number:integer
begin
if this_number<> 1 or 2 then foo
end
I confess that I don't remember the relevant order of precedence
here, but this boolean statement is interpreted either as
this_number <> (1 or 2)
or
(this number <> 1) or 2
The first form is equivalent to
this_number <> 3
because (1 or 2) evaluates to 3.
The second form should really be illegal in a strongly typed
language like Pascal. Its C- counterparts would be legal but
produce somewhat unpredictable results: first the value of
"this_number <>1" would be computed (either true or false), and this
would then be "ORed" with 2. Any non-zero result is often interpreted as
"true", so this statement would always be true irrespective of the value
of "this_number".
> ----------------------------------------------
'or' simply isn't meant to do what you appear to want from
it. I always go with the solution using sets. Like
var
user_reply:char;
begin
repeat
foo;
writeln("Go again Y/N?");
repeat
user_reply:=readkey;
until user_reply in ['n','y','N','Y'];
until user_reply in ['n','N'];
end;
IMHO this is a relatively clean solution.
>
> Is there another way perform these operations on a string or
> character?
Check the on-line help for the functions 'Pos', 'Copy' and 'Substr' for
a solution of your problem. Read your text file line by line as others
have suggested.
>
> My purpose is to extract certain words from a text file, then convert
> them to something else and write the conversions in a new text file.
>
> For example: I want to open My_Text_File extract or search for all
> occurances of "one" then make the program convert it to "1" and then
> write the conversions (including any unconverted words) in
> a new text file.
Sorry, if I came thru as harsh. I do remember that this kind
of things appeared to be a bit unnatural at first.
By thinking about problems lik yours you gradually become familiar with
the tools at your disposal. Courage. Work on it, and this type of
thinking becomes a second nature to you.
Cheers,
Jyrki


|