Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Pascal Borland > Re: String and ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 13 Topic 1025 of 1118
Post > Topic >>

Re: String and Char handling limitations:

by Jyrki Lahtonen <lahtonen@[EMAIL PROTECTED] > Apr 26, 2007 at 11:10 PM

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
 




 13 Posts in Topic:
String and Char handling limitations:
CheckWilliamOut <wdupl  2007-04-25 15:13:54 
Re: String and Char handling limitations:
Wolf Behrenhoff <NoSpa  2007-04-26 12:07:07 
Re: String and Char handling limitations:
Wolf Behrenhoff <NoSpa  2007-04-26 12:19:16 
Re: String and Char handling limitations:
Femme Verbeek <fv@[EMA  2007-04-26 18:28:22 
Re: String and Char handling limitations:
Jyrki Lahtonen <lahton  2007-04-26 23:10:07 
Re: String and Char handling limitations:
Marco van de Voort <ma  2007-04-27 21:44:33 
Re: String and Char handling limitations:
CheckWilliamOut <wdupl  2007-05-05 03:31:57 
Re: String and Char handling limitations:
"Jochen" <jo  2007-05-05 16:16:09 
Re: String and Char handling limitations:
Jyrki Lahtonen <lahton  2007-05-07 23:44:27 
Re: String and Char handling limitations:
Marco van de Voort <ma  2007-05-07 20:50:56 
Re: String and Char handling limitations:
Jyrki Lahtonen <lahton  2007-05-08 00:10:34 
Re: String and Char handling limitations:
"Jochen" <jo  2007-05-08 01:24:33 
Re: String and Char handling limitations:
Tony Stanford <tonysta  2007-05-08 14:42:41 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Jul 23 16:04:47 CDT 2008.