Autor Beitrag
dkx
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 36



BeitragVerfasst: Sa 07.05.05 14:04 
Hallo,

ich habe folgenden Code:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
i := 1;

    zahlstring := '';
    j := 1;
    repeat
      zahlstring[j] := rohmatrix[i];
      inc(i);
      inc(j);
    until (rohmatrix[i] = ',');


....der nicht funktioniert.

Mein Ziel ist folgendes:
Ich habe einen String, z.B.: '354,3423,1234;343...' und möchte von diesem String die einzelnen Zahlen (also 354 und 3423 usw) in eine integer-variable schreiben.

Das Problem bei dem oben genannten Code ist ja, dass ich quasi nicht in zahlstring[3] irgendwas reinschreiben kann, wenn ich davor gesagt habe zahlstring=''.
Wie kann ich das Problem am einfachsten lösen?

Danke für die Mühen,
dkx
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Sa 07.05.05 14:41 
In Zeile 6 muss es heißen Zahlstring := Zahlstring + rohmatrix[i]; Den Zähler J kannst Du weglassen.

Du musst erst den Speicher für das neue Zeichen reservieren, ODER das neue Zeichen einfach anhängen.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
dkx Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 36



BeitragVerfasst: Sa 07.05.05 16:25 
Ok, danke für den Tipp mit dem DEUTLICH eleganteren Code.
Wegen dem eigentlichen Problem dachte ich mir schon die Lösungsansätze die du genannt hattest - nur das ich nicht genau weiss wie das geht. Habe jetzt einen Shortstring genommen und es klappt.
Die andere Methode wäre doch dann einen eigenen String-Typ zu definieren, oder? Auch wenn mein Problem gelöst ist fände ich es interessant zu wissen wie man das anstellt.

Danke, dkx
crakos
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 195


D2k5 Pers
BeitragVerfasst: Sa 07.05.05 17:03 
also ich als neuling würde das so machen
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
var matrix:string;
    j,zaehler:integer;
begin
  zaehler:=1;
  matrix:=rohmatrix;
  repeat
    i:=pos(',',matrix);  // ort vom komma bestimmen
    
    for j:=1 to i-1
      inc:=inc+matrix[j]; //in inc die zahlen reinschreiben
    delete(matrix,1,i);

    Case zaehler of  
    1:begin inc1:=inc end; //ob das begin end hier hin muss weiss ich net
    2:begin inc2:=inc end;
    end; 

    zaehler:=zaehler+1;
  until pos(',',matrix)=0; //wenn kein komma mehr da dann ende (=0)

so würde ich es machen, geht bestimmt eleganter