Autor Beitrag
R4Y
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 41



BeitragVerfasst: Sa 09.02.08 19:49 
Hi,
Ich hoffe das passt hier rein.

Ich hab folgendes problem. Ich mahce eine liste mit mehreren strings die werte enthalten, die ich auslesen möchte.

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
(group (index 101) (money (920 20)) (item (960 443 0) (1000 47 0)))
(group (index 102) (money (760 30)) (item (800 443 0) (840 2 0) (880 23 0) (920 91 0) (1000 47 0)))
(group (index 103) (money (760 30)) (item (800 443 0) (840 2 0) (880 23 0) (920 91 0) (1000 47 0)))
(group (index 104) (money (760 40)) (item (800 443 0) (840 47 0) (885 2 0) (935 23 0) (980 91 0)))
(group (index 105) (money (785 60)) (item (800 443 0) (815 6 0) (830 32 0) (845 70 0) (1000 47 0)))
(group (index 106) (money (879 80)) (item (919 443 0) (923 3 0) (925 92 0) (985 47 0) (987 6 0) (989 7 0) (991 8 0) (993 9 0)
          (995 10 0) (996 70 0) (997 71 0) (998 72 0) (999 73 0) (1000 74 0)))
(group (index 107) (money (979 80)) (item (919 443 0) (923 24 0) (925 92 0) (985 47 0) (987 32 0) (989 33 0) (991 34 0) (993 35 0)
          (995 36 0) (996 70 0) (997 71 0) (998 72 0) (999 73 0) (1000 74 0)))


Das ist meine liste. Ich muss nun nur den 2. eintrag aus Money haben also 20.

Jetzt hatte ich überlegt jeden buchstaben einzeln auszulesen aber das klappte irgendwie auch nicht so wirklich.

Dann hatte ich mir überlegt, da es immer 5 leerschritte sind bis meine werte kommen die leerschritte zu zählen.
Naja und da hänge ich jetzt. Mir fehlt es an der logik die werte korrekt auszulesen.

Das hatte ich bis jetzt mal so runtergeschrieben was natürlich nicht geklappt hat :(

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
procedure TForm1.Button1Click(Sender: TObject);
var
 i,ii,iii,zehler:integer;
 tmp,tmpp:string;
begin

 zehler:=0;

 for i:= 0 to memo1.Lines.Count-1 do
  begin

  for ii:= 0 to length(memo1.Lines.Strings[i])-1 do
   begin
   tmpp:=copy(memo1.Text,ii,1);

   showmessage(tmpp);

   if tmpp = ' ' then
    begin
     zehler:=zehler+1;
    end;

   if zehler = 5 then
    begin
     if tmpp=')'
      then
       begin
        zehler:=zehler+1;
       end;
     tmp:=tmp+tmpp;
    end;

    end;
  end;
 end;


ich hoffe ihr könnt mir helfen

//edit
Sry hatte vergessen zu sagen, Das ich diesen wert dann noch mal 10 nehemen muss und wieder da hinschreiben muss

greetz
R4Y

Moderiert von user profile iconNarses: Delphi-Tags hinzugefügt


Zuletzt bearbeitet von R4Y am So 10.02.08 12:35, insgesamt 1-mal bearbeitet
nagel
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 708

Win7, Ubuntu 10.10

BeitragVerfasst: Sa 09.02.08 20:16 
Weiß nicht, ob das das einzige Problem ist, aber der Zähler sollte innerhalb der ersten Schleife auf null gesetzt werden (also für jede Zeile wieder).
R4Y Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 41



BeitragVerfasst: So 10.02.08 00:50 
naja gut^^ 1. probl. gefixt aber ich bekomme ja noch netmal die werte rein
nagel
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 708

Win7, Ubuntu 10.10

BeitragVerfasst: So 10.02.08 01:37 
Noch was: Die ii-Schleife sollte von 1 bis length(..) gehen.

Und copy(memo1.Text,ii,1) sollte wohl besser memo1.Lines[i][ii] sein.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19326
Erhaltene Danke: 1749

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 10.02.08 06:22 
Das hier funktioniert, nur die Items (dafür sollte der auskommentierte Quelltext sein) auszulesen funktioniert nicht. Ich weiß nicht warum, aber es kommt eine Zugriffsverletzung, wenn ich den auskommentierten Quelltext mit reinnehme. Ich glaube es ist zu früh am Morgen^^.
Zumindest das was du haben wolltest, geht aber ;-).
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
type
  TContents = record
    Group:
      record
        Index: Integer;
        Money:
          record
            Value1, Value2: Integer;
          end;
      end;
//    Items: array of record
//        Value1, Value2, Value3: Integer;
//      end;
  end

  TfrmMain = class(TForm)
    memInput: TMemo;
    btnStart: TButton;
    memOutput: TMemo;
    procedure btnStartClick(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.dfm}

procedure TfrmMain.btnStartClick(Sender: TObject);

  function GetNumber(var uText: String; uSep1, uSep2: String): Integer;
  begin
    if uSep1 <> '' then    
      Delete(uText, 1, Pos(uSep1, uText));
    Result := StrToInt(Copy(uText, 1, Pos(uSep2, uText) - 1));
    Delete(uText, 1, Pos(uSep2, uText));
  end;

  procedure Analyze(var uCurrent: TContents; var uText: String; uToDelete: Integer = 0);
  var
    CurItem: Integer;
  begin
    Delete(uText, 1, uToDelete + 1); // zu löschende Zeichen plus Klammer
    if Pos('group', uText) = 1 then
    begin
      Analyze(uCurrent, uText, 6);
      Delete(uText, 12); // ") "
    end
    else if Pos('index', uText) = 1 then
    begin
      uCurrent.Group.Index := GetNumber(uText, ' '')');
      Analyze(uCurrent, uText, 1);
    end
    else if Pos('money', uText) = 1 then
    begin
      uCurrent.Group.Money.Value1 := GetNumber(uText, '('' ');
      uCurrent.Group.Money.Value2 := GetNumber(uText, ''')');
    end
//    else if Pos('item', uText) = 1 then
//    begin
//      CurItem := 0;
//      SetLength(uCurrent.Items, 0);
//      while uText[1] <> ')' do
//      begin
//        Inc(CurItem);
//        SetLength(uCurrent.Items, Length(uCurrent.Items) + 1);
//        uCurrent.Items[CurItem].Value1 := GetNumber(uText, '(', ' ');
//        uCurrent.Items[CurItem].Value2 := GetNumber(uText, '', ' ');
//        uCurrent.Items[CurItem].Value3 := GetNumber(uText, '', ')');
//      end;
//      Delete(uText, 1, 2); // "))"
//    end;
  end;

var
  i, j: Integer;
  CurrentLine: String;
  MyContents: array of TContents;
begin
  SetLength(MyContents, memInput.Lines.Count);
  for i := 0 to memInput.Lines.Count - 1 do
  begin
    CurrentLine := memInput.Lines[i];
//    while (CurrentLine <> '') do
      Analyze(MyContents[i], CurrentLine);
  end;
  for i := 0 to High(MyContents) do
  begin
    CurrentLine := 'Group-index: ' + IntToStr(MyContents[i].Group.Index)
      + ', Group-money: (' + IntToStr(MyContents[i].Group.Money.Value1) + ', '
      + IntToStr(MyContents[i].Group.Money.Value2) + '), Items: ';
//    for j := 0 to High(MyContents[i].Items) do
//      CurrentLine := CurrentLine + ' (' + IntToStr(MyContents[i].Items[j].Value1)
//        + ', ' + IntToStr(MyContents[i].Items[j].Value2)
//        + ', ' + IntToStr(MyContents[i].Items[j].Value3) + ')';
    memOutput.Lines.Add(CurrentLine);
  end;
end;
Einloggen, um Attachments anzusehen!
R4Y Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 41



BeitragVerfasst: So 10.02.08 12:34 
h joa die funktion erfüllt ihren zweck :) naja nur nicht ganz so wie ich mir das vorgestellt habe :)

Erstmal vielen dank. Ich werde versuchen ob ich das auch so reingefummelt bekomme.

Und zwar muss ich diesen wert dann noch mal 10 nehemen ;) und wieder da rein schreiben :(

Aber das konntest du ja nicht wissen sry^^

greetz
R4Y
R4Y Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 41



BeitragVerfasst: So 10.02.08 13:27 
so ich habe deine funktion jetzt etwas umgebaut und meinen zwecken angepasst :) nun funktioniert alles so wie es soll ;)

danke euch allen für eure bemühungen.

neuer code:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, Menus, ComCtrls;

type
  TForm1 = class(TForm)
    memInput: TMemo;
    Panel1: TPanel;
    OpenDialog1: TOpenDialog;
    MainMenu1: TMainMenu;
    Datei1: TMenuItem;
    ffnen1: TMenuItem;
    Speichern1: TMenuItem;
    N1: TMenuItem;
    Schlieen1: TMenuItem;
    StatusBar1: TStatusBar;
    Edit1: TEdit;
    Button1: TButton;
    memOutput: TMemo;
    SaveDialog1: TSaveDialog;
    procedure FormCreate(Sender: TObject);
    procedure Schlieen1Click(Sender: TObject);
    procedure memInputChange(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure ffnen1Click(Sender: TObject);
    procedure Speichern1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  type
  TContents = record
    Group:
      record
        Index: Integer;
        Money:
          record
            Value1, Value2: Integer;
          end;
      end;
//    Items: array of record
//        Value1, Value2, Value3: Integer;
//      end;
  end;

var
  Form1: TForm1;
  memochange:bool;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
 OpenDialog1.Execute;

 if OpenDialog1.FileName = '' then
  begin
   MessageBox(0,'Couldn''t load Itemgroup','Error',0);
  end
  else
   begin
   memInput.Lines.LoadFromFile(OpenDialog1.FileName);
  end;
end;

procedure TForm1.Schlieen1Click(Sender: TObject);
begin

 if memochange = true then
  begin
   if Application.MessageBox('Möchtest du speichern?','Speichern?',mb_yesnocancel) = IDYES then
    begin
     //kommt noch
    end
    else
     begin
      Application.Terminate;
     end;
  end;
end;

procedure TForm1.memInputChange(Sender: TObject);
begin
 memochange:=true;
end;

procedure TForm1.Button1Click(Sender: TObject);
function GetNumber(var uText: String; uSep1, uSep2: String): Integer;
  begin
    if uSep1 <> '' then    
      Delete(uText, 1, Pos(uSep1, uText));
    Result := StrToInt(Copy(uText, 1, Pos(uSep2, uText) - 1));
    Delete(uText, 1, Pos(uSep2, uText));
  end;

  procedure Analyze(var uCurrent: TContents; var uText: String; uToDelete: Integer = 0);
  var
    CurItem: Integer;
  begin
    Delete(uText, 1, uToDelete + 1); // zu löschende Zeichen plus Klammer
    if Pos('group', uText) = 1 then
    begin
      Analyze(uCurrent, uText, 6);
      Delete(uText, 12); // ") "
    end
    else if Pos('index', uText) = 1 then
    begin
      uCurrent.Group.Index := GetNumber(uText, ' '')');
      Analyze(uCurrent, uText, 1);
    end
    else if Pos('money', uText) = 1 then
    begin
      uCurrent.Group.Money.Value1 := GetNumber(uText, '('' ');
      uCurrent.Group.Money.Value2 := GetNumber(uText, ''')');
    end
//    else if Pos('item', uText) = 1 then
//    begin
//      CurItem := 0;
//      SetLength(uCurrent.Items, 0);
//      while uText[1] <> ')' do
//      begin
//        Inc(CurItem);
//        SetLength(uCurrent.Items, Length(uCurrent.Items) + 1);
//        uCurrent.Items[CurItem].Value1 := GetNumber(uText, '(', ' ');
//        uCurrent.Items[CurItem].Value2 := GetNumber(uText, '', ' ');
//        uCurrent.Items[CurItem].Value3 := GetNumber(uText, '', ')');
//      end;
//      Delete(uText, 1, 2); // "))"
//    end;
  end;

var
  i,ii, j: Integer;
  CurrentLine,newline: String;
  MyContents: array of TContents;
  newmoney,oldmoney:integer;
begin

  if edit1.Text = '' then
   begin
    MessageBox(0,'Please enter a correct value','Error',0);
   end
   else
    begin


  SetLength(MyContents, memInput.Lines.Count);
  for i := 0 to memInput.Lines.Count - 1 do
  begin
    CurrentLine := memInput.Lines[i];
//    while (CurrentLine <> '') do
      Analyze(MyContents[i], CurrentLine);
  end;
  for i := 0 to High(MyContents) do
  begin
    CurrentLine := '(money (' + IntToStr(MyContents[i].Group.Money.Value1) + ' '
      + IntToStr(MyContents[i].Group.Money.Value2)+'))';
//    for j := 0 to High(MyContents[i].Items) do
//      CurrentLine := CurrentLine + ' (' + IntToStr(MyContents[i].Items[j].Value1)
//        + ', ' + IntToStr(MyContents[i].Items[j].Value2)
//        + ', ' + IntToStr(MyContents[i].Items[j].Value3) + ')';

       memOutput.Lines.Add(CurrentLine);

       newline:='(money (' + IntToStr(MyContents[i].Group.Money.Value1) + ' ';

      for ii:= 0 to memoutput.Lines.Count-1 do
       begin

        oldmoney:=MyContents[i].Group.Money.Value2;
        newmoney:=oldmoney * strtoint(edit1.Text);

        Meminput.Lines.Strings[ii]:=stringreplace(meminput.Lines.Strings[ii],Currentline,newline+inttostr(newmoney)+'))',[rfReplaceAll, rfIgnoreCase]);
       end;

  end;
  end;
end;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if not (key in [#48..#57#8#44]) then key := #0
end;

procedure TForm1.ffnen1Click(Sender: TObject);
begin
 OpenDialog1.Execute;

 if OpenDialog1.FileName = '' then
  begin
   MessageBox(0,'Couldn''t load Itemgroup','Error',0);
  end
  else
   begin
   memInput.Lines.LoadFromFile(OpenDialog1.FileName);
  end;
end;

procedure TForm1.Speichern1Click(Sender: TObject);
begin
 SaveDialog1.Execute;

 if SaveDialog1.FileName = '' then
  begin
   MessageBox(0,'Couldn''t save Itemgroup','Error',0);
  end
  else
   begin
   Meminput.Lines.SaveToFile(SaveDialog1.FileName); 
  end;
end;

end.


Moderiert von user profile iconNarses: Code- durch Delphi-Tags ersetzt
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19326
Erhaltene Danke: 1749

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 10.02.08 13:56 
Schön dass es bei dir jetzt funktioniert wie du es wolltest ;-).

Und wie ich schon heute morgen geschrieben habe, es war einfach zu früh am Morgen, ich war blind^^.
Mein Fehler lag hier:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
      CurItem := 0;
      SetLength(uCurrent.Items, 0);
      while uText[1] <> ')' do
      begin
        Inc(CurItem);
        SetLength(uCurrent.Items, Length(uCurrent.Items) + 1);
        uCurrent.Items[CurItem].Value1 := GetNumber(uText, '('' ');
Ich habe CurItem erhöht bevor auf diesen Eintrag zugegriffen wurde, so dass statt auf Index 0 auf Index 1, etc. zugegriffen wurde...

Falls es also jemanden mal interessiert, hier die funktionierende Version, die das komplett parst ;-). (Und wenn du andere Werte verändern wolltest, könntest du das dann einfach alles, auch die Items, wieder zusammensetzen wie ich es auch im Beispiel tue ;-).
Einloggen, um Attachments anzusehen!
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: So 10.02.08 14:03 
Hallo,

wenn nur die Items 'money' berücksichtigt, und mit 10 multipliziert werden müssen, reicht es vor der schließenden Klammer eine '0' einzufügen.

user profile iconjaenickes Code ist natürlich flexibler :wink:

Hier mal meine Funktion:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
function MoneyInc10(s: String): String;
var i,iSearch,iResult : Integer;
    blIsMoney : Boolean;
    sSearch : String;
begin
  i := 0; iSearch := 1; iResult := 0; blIsMoney := False; sSearch := 'money';
  SetLength(Result,length(s)*2);//*2 sollte reichen
  while i < Length(s) do
    begin
    inc(i); inc(iResult);
    if blIsMoney then
      begin
      if s[i] = ')' then // suchen bis erste schließende Klammer nach 'money'
        begin
        Result[iResult] := '0';// *10 also '0' einfügen
        inc(iResult);
        Result[iResult] := ')';
        blIsMoney := False;
        end else Result[iResult] := s[i];
      end
      else if s[i] = sSearch[iSearch] then
             begin
             Result[iResult] := s[i];
             if iSearch = 5 then// 'money' gefunden
               begin
               blIsMoney := True;
               iSearch := 1;
               end else inc(iSearch);
             end else Result[iResult] := s[i];
    end;
  SetLength(Result,iResult-1);
end;

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
R4Y Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 41



BeitragVerfasst: So 10.02.08 14:09 
joa weiß ich doch ;)

nur ich baue es gleich dynamisch fall ich das mal *15 nehmen muss oder so ;)