Piece function...

Moderatoren: crack, Marwin, Krüsty

Antworten
Ignat
Alter Hase
Beiträge: 80
Registriert: Samstag 2. August 2003, 11:13
Kontaktdaten:

Beitrag von 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...

Silentx2000
Newbie
Beiträge: 1
Registriert: Freitag 19. Dezember 2003, 21:55

Beitrag von 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;

Ignat
Alter Hase
Beiträge: 80
Registriert: Samstag 2. August 2003, 11:13
Kontaktdaten:

Beitrag von 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...

Antworten