find replace
hi i have 2 txt files in 1 i have stored numbers
numbers.txt
ex.
10
20
30
others i have stored
word.txt
10,hello
20,welcme
30,text ......
i need to auto replace numbers with words ..
plise help.
find replace
hi i have 2 txt files in 1 i have stored numbers
numbers.txt
ex.
10
20
30
others i have stored
word.txt
10,hello
20,welcme
30,text ......
i need to auto replace numbers with words ..
plise help.
A simple, robust approach is to load the mapping file into a name/value table, then stream or iterate the numbers file and replace each number line with the mapped word (preserving order and duplicates). This satisfies 's "auto load and change" requirement. 's parsing idea and 's compare-and-replace suggestion point in the right direction; the snippet below shows a compact Delphi/Pascal implementation using TStringList.Values for the lookup.
procedure ConvertNumbersToWords(const NumbersFile, MapFile, OutFile: string);
var
Map, Numbers: TStringList;
i, p: Integer;
line, key, val: string;
begin
Map := TStringList.Create;
Numbers := TStringList.Create;
try
Map.LoadFromFile(MapFile); // expects lines like "10,hello"
Numbers.LoadFromFile(NumbersFile);
// normalize mapping: "10,hello" -> "10=hello"
for i := Map.Count - 1 downto 0 do
begin
line := Trim(Map[i]);
if line = '' then Map.Delete(i)
else
begin
p := Pos(',', line);
if p > 0 then
Map[i] := Trim(Copy(line,1,p-1)) + '=' + Trim(Copy(line,p+1,MaxInt))
else
Map.Delete(i); // skip malformed lines
end;
end;
// replace each number with the mapped word (fallback: keep original)
for i := 0 to Numbers.Count - 1 do
begin
key := Trim(Numbers[i]);
val := Map.Values[key];
if val <> '' then Numbers[i] := val else Numbers[i] := key;
end;
Numbers.SaveToFile(OutFile);
finally
Map.Free;
Numbers.Free;
end;
end; Notes and troubleshooting: trim whitespace on both files; use the first comma as separator so extra commas in words are preserved after the first; missing mappings fall back to the original number (changeable to an empty string if preferred); for very large files prefer TStreamReader/TStreamWriter to avoid high memory use; consider encoding (ANSI vs UTF-8) when saving in modern Delphi. This implementation provides an automatic, repeatable replacement flow that complements the earlier examples from and the advice from .
Jump to Post— FlamingClaw 98so the word.txt's contets need to change?
like
hello,10
welcme,20
text,30or the two files' contents
number.txt's contents:
10,hello
20,welcme
30,text ......word.txt's contents:
10
20
30what do you say?
Jump to Post— FlamingClaw 98I have got an idea.... :)
program solution; uses crt; const numtxt = 'C:\number.txt'; {assign f1} wortxt = 'C:\words.txt'; {assign f2} temptxt = 'C:\temp.txt'; {assign f3} var f1,f2,f3:text; numstr,wordstr,tempstr:string;{one line max 255 char} i:byte; {max 255 line!} c:set of char; begin clrscr; c:=['0','1','2','3','4','5','6','7','8','9']; numstr:=''; tempstr:=''; assign(f1,numtxt); assign(f2,wortxt); …
so the word.txt's contets need to change?
like
hello,10
welcme,20
text,30
or the two files' contents
number.txt's contents:
10,hello
20,welcme
30,text ......
word.txt's contents:
10
20
30
what do you say?
You should read every number from the first file and compare each of it with the first one's of the second file.
When you find a mach, you do a replace.
I am not sure where do you have your problem: in understanding what to do or in writing a code.
Hi
i need to change only numbers .
numbers.txt
ex.
10
20
30
(find)from base word.txt
10,hello
20,welcme
30,text ..
-------------------------------------------------------
(change) save (only.words.txt)
in
hello
welcme
text
-------------------------------------------------------------
(numbers.txt)some tame will by
ex.
10
10
30
20
30
30
30
----------------------------------------------------------------
(change) save (only.words.txt)
in
hello
hello
text
welcme
text
text
text
--------------------------------------------------------
but i need to load and ( auto) cange on match..(find)from base word.txt
plise small example of code ......
Big Thanks for help
hire is example but i need auto load and cange ...
any sugestion.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
Tfrmdelphidlgs = class(TForm)
btnfindreplace: TButton;
btnclear: TButton;
memtext: TMemo;
procedure btnfindreplaceClick(Sender: TObject);
procedure btnclearClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmdelphidlgs: Tfrmdelphidlgs;
implementation
{$R *.dfm}
procedure Tfrmdelphidlgs.btnfindreplaceClick(Sender: TObject);
var texttosearch, strtofind, replacestr : string;
position : integer;
begin
texttosearch := memtext.text;
strtofind := inputbox('Find ', 'Enter the string to find', '');
if strtofind <> '' then // if the user provided a string to find
begin
// get the repplacement string
replacestr := inputbox('Replace with','Enter the replacemente string','');
// get the postition of the string to find in the text
position := pos(strtofind, texttosearch);
if position > 0 then //if the string was found
begin
//delete the string searched for
delete( texttosearch,position, length(strtofind));
// insert the replacement string at that position
insert(replacestr, texttosearch,position);
memtext.Text := texttosearch;
messagedlg('String replaced', mtinformation, [mbok], 0 );
end // if posiiton...
else
messagedlg(strtofind +' does not appear in the text.',mtinformation,[mbok],0);
end // if string to find
else
messagedlg('No search string provided', mtinformation, [mbok], 0 );
end;
procedure Tfrmdelphidlgs.btnclearClick(Sender: TObject);
begin
memtext.Clear ;
memtext.setfocus;
end;
end. I have got an idea.... :)
program solution;
uses crt;
const numtxt = 'C:\number.txt'; {assign f1}
wortxt = 'C:\words.txt'; {assign f2}
temptxt = 'C:\temp.txt'; {assign f3}
var f1,f2,f3:text;
numstr,wordstr,tempstr:string;{one line max 255 char}
i:byte; {max 255 line!}
c:set of char;
begin
clrscr;
c:=['0','1','2','3','4','5','6','7','8','9'];
numstr:='';
tempstr:='';
assign(f1,numtxt);
assign(f2,wortxt);
assign(f3,temptxt);
rewrite(f3); {create a new,or delete its contents...}
append(f1); {open for append}
reset(f2); {open for read}
{while true ,that we're not in the end of the file}
while (eof(f2) = false) do begin
readln(f2,wordstr); {read one line}
{check the readed characters one by one}
for i:=1 To length(wordstr) do begin
if not (wordstr[i] in c) then break {!}
else numstr:=numstr + wordstr[i];
end;{of for}
writeln(f1,numstr); {append the results to the number.txt}
numstr:=''; {set to empty}
{ok,now let'see the temp file..}
for i:=1 to length(wordstr) do begin
case (wordstr[i]) of
'0'..'9':writeln;{do nothing}
',' :writeln;{here too}
else tempstr:=tempstr + wordstr[i]; {!}
end;{of case}
end;{of for}
writeln(f3,tempstr);
tempstr:='';
end; {of while}
clrscr;
{and last copy the temp.txt's contents to the words.txt}
reset(f3);
rewrite(f2);
while not eof(f3) do begin
readln(f3,tempstr);
writeln(f2,tempstr);
end;
{write out the results to the screen...}
{number.txt}
writeln('number.txt''s contents');
reset(f1);
tempstr:='';
while not eof(f1) do begin
readln(f1,tempstr);
writeln(tempstr);
end;
{words.txt}
writeln('words.txt''s contents');
reset(f2);
tempstr:='';
while not eof(f2) do begin
readln(f2,tempstr);
writeln(tempstr);
end;
{close the files...}
close(f1);
close(f2);
close(f3);
{just press the enter button to quit}
readln;
end.
{
-=created by FlamingClaw 2009.09.05=-
dev-pascal v.1.9.2
working....
} :D
Thank you mr FlamingClaw for your example .
you are not just a pascal programmer you are
master of pascal.
"real answers to difficult questions"
Mr FlamingClaw is "real pro programmer"
Thank you
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.