CheckWilliamOut schreef:
> 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.
>
> ----------------------------------------------
>
> var
> This_letter : char;
>
>
> begin
> If this_letter <> 'a' or 'b' then
> Writeln('Neither A nor B were detected');
> end.
>
> ----------------------------------------------
>
> Is there another way perform these operations on a string or
> character?
>
> 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.
>
> The problem is, I'm having trouble extracting only the words I want--
> and getting the program to ignore spaces, commas, quotation marks,
> carriage returns, and every other symbol and punctuation mark on the
> keyboard.
>
> First I tried using
>
> repeat
> Read(text_file, data);
> Write(tem****ary_file, data);
> until data = ' ';
>
> Then I close the tem****ary file, and re-open it for reading, read the
> characters--or the word--that has been stored there, then match it and
> convert.
>
> However that method is causing an infinite loop and there is no way a
> "While not EOF" command can be used successfully to avoid the infinite
> loop. This also causes problems because I want the computer to
> recognize:
>
I don't see the problem.
If you open two files (as text) you can continue reading whole lines
from the first one until it reaches the eof state
Read a complete line into a string variabele using readln instead of read.
Replace any word you want, and write the altered line to the destination
file.
var sourcefile,targetfile:text;
var s:string;
......
While not eof (sourcefile) do
begin
readln (sourcefile,s);
processtheline;
writeln (targetfile,s)
end;
> then
>
> as being = to
>
> Then
> then.
> then,
> then!
> "then"
> then."
> then?
>
> and any other combination you can think of
>
> Also when it comes to the end of a file, it doesn't find anymore
> spaces and that is what is causing the infinit loop.
>
> Ideally I want to be able to define the "until" part as:
>
> until data <> 'a'..'z' or 'A'..'Z';
>
> Sadly you can only do that with integers!
>
> Perhaps in the above example if I change each letter to it's numeric
> equivalent?
>
> CheckWilliamOut
>
You are probably looking for the POS function. It searches for a
substring in a string.
e.g.
const s:string='hello world hello computer';
var position:integer;
begin
repeat
position:= pos('hello',s);
if position>0 then
begin
delete(s,position,length('hello') ) ;
insert('goodbye',s,position);
end;
until position=0;
writeln(s);
end.
-- Femme


|