Piece function...

Moderatoren: crack, Krüsty, Marwin

Beitragvon Ignat » Samstag 16. August 2003, 20:57

Hi!
Now I am working at big Delpihi project - this program will work with CSV-files and with databases.
And I have a problem - I need a function that can "extract" substring in some position from string.
I mean if I have string=I1want1to1go than my function must return want if I set position=2 and separator=1.
Its like function $p in COS (Cache Object Script).
My programm will work with big files (10MB-100MB). It will get string from file, "extract" data and save in database. So I need a very fast function. Ive created some function, but its not so fast I need. How to optimize it or I should write this function on ASM or C++?
The function I write in "Pascal style" (its a Pascal topic! :) ):
Code: Alles auswählen
function piece(s:string;ch:char;p:word):string;
var vr_str:string;sch,i,j:word;
begin
if pos(ch,s) = 0 then piece:=s
 else
  begin sch:=1;
        i:=1;
        vr_str:=;
        while (s[i]=ch) and (i<=length(s)) do inc(i);
        for j:=i to length(s) do
        if s[j]<>ch then begin vr_str:=vr_str+s[j];
                               if j=length(s) then begin piece:=vr_str;
                                                         break;
                                                   end;
                         end
                     else
          if  sch=p then begin piece:=vr_str;
                               break;
                         end
                    else begin inc(sch);
                               vr_str:=;
                         end;
   end;
end;
var proba_string:string;
   ps:word;
   vr_ch:char;
begin
write(Please, input your string: );
readln(proba_string);
write(Input separator: );
readln(vr_ch);
write(Input position (,vr_ch, is separator): );
readln(ps);
writeln(piece(proba_string,vr_ch,ps));
writeln(Press Enter...);
readln;
end.



What can you advice me?...
If the ship don't know where to go - it has no fair wind...
Ignat
Alter Hase
 
Beiträge: 80
Registriert: Samstag 2. August 2003, 11:13

Beitragvon Silentx2000 » Freitag 19. Dezember 2003, 22:30

hi

Code: Alles auswählen
function piece(s: string; index: integer; sep: char): string;
var
 sl: TStringList;
begin
 sl := TStringList.Create;
 Try
   Repeat
     if Pos(sep, s) <> 0 then
     begin
       sl.Add(Copy(s, 1, Pos(sep, s)-1));
       Delete(s, 1, Pos(sep, s));
     end
     else
     begin
       sl.Add(s);
       s := ;
     end;
   until s = ;
   if index <= sl.Count then
   Result := sl.Strings[index-1]
   else Result := ;
 finally
   sl.Free;
 end;
end;
Silentx2000
Newbie
 
Beiträge: 1
Registriert: Freitag 19. Dezember 2003, 21:55

Beitragvon Ignat » Samstag 20. Dezember 2003, 16:46

Aha..
Hm. I tried to write this function in "Pascal" mode - may be in a future Ill need it in my Pascal programms, but you are right - your code is better! :)
If the ship don't know where to go - it has no fair wind...
Ignat
Alter Hase
 
Beiträge: 80
Registriert: Samstag 2. August 2003, 11:13


Zurück zu Turbo Pascal


Social Bookmarking
Bookmark bei: Mr. Wong Bookmark bei: Del.icio.us Bookmark bei: Webnews Bookmark bei: Icio Bookmark bei: Oneview Bookmark bei: Linkarena Bookmark bei: Newskick Bookmark bei: Folkd Bookmark bei: Yigg Bookmark bei: Digg Bookmark bei: Reddit Bookmark bei: Simpy Bookmark bei: StumbleUpon Bookmark bei: Slashdot Bookmark bei: Netscape Bookmark bei: Furl Bookmark bei: Yahoo Bookmark bei: Spurl Bookmark bei: Google Bookmark bei: Blinklist Bookmark bei: Blogmarks Bookmark bei: Diigo Bookmark bei: Technorati Bookmark bei: Newsvine Bookmark bei: Blinkbits Bookmark bei: Ma.Gnolia Bookmark bei: Smarking Bookmark bei: Netvouz

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder

cron