Autor Beitrag
DiamondDog
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 460



BeitragVerfasst: Sa 14.02.09 13:51 
Hallo Leute,
ich hab da folgendes Problem und zwar würde ich gerne mein RichEdit durchsuchen.
Mein Richedit sieht zb so aus:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
Name1
Name2
Name1
Name3
Name5
Name4
Name2
Name1
jetzt würde ich mir gerne anzeigen lassen welche Name wie oft im RichEdit steht, das Problem dabei ist ich weiß vorher nicht wie die Liste in dem Richedit aussieht (welche Namen vorhanden sind).
Die Ausgabe sollte in etwa dann so aussehn in einem neuen Richedit:
ausblenden Quelltext
1:
2:
3:
4:
5:
3 Name1
2 Name2
1 Name3
1 Name4
1 Name5
Die Liste braucht nicht sotiert zu sein oder so Hauptsache ich weiß wieviel mal, welche Name in der Liste drinn steht.

Hoffe das mir das jemand Helfen kann, schon mal Danke an alle.

Mfg Dog


Zuletzt bearbeitet von DiamondDog am Sa 14.02.09 15:52, insgesamt 1-mal bearbeitet
DeddyH
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Sa 14.02.09 14:00 
Eine relativ einfache und Quick'n Dirty-Lösung wäre die Verwendung einer TMemIniFile, wobei der einzelne Name dann den Ident und die Anzahl den Wert darstellt. Schau dazu mal in der Hilfe nach (TMemIniFile, ReadInteger, WriteInteger).
DiamondDog Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 460



BeitragVerfasst: Sa 14.02.09 14:15 
Hab gerde mal in die Hilfe reingeschaut, aber irgend wie kann ich mir keine Reim drauf bilden wie ich damit mein Problem lösen kann. In der Hilfe steht was von speichern und laden von Ini datei und sowas aber irgend wie versteh ich nicht ganz wie ich damit eine RichEdit durchsuchen kann.
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Sa 14.02.09 14:25 
Moin!

Ausnahmsweise mal direkt Code - allerdings für ein Memo, nicht RichEdit und die Ausgabe darfst du auch selbst machen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
procedure TForm1.Button1Click(Sender: TObject);
  var
    SL: TStringList;
    i,j: Integer;
begin
   SL := TStringList.Create;
   try
     for i := 0 to Memo1.Lines.Count-1 do begin // über alle Zeilen
       j := SL.IndexOf(Memo1.Lines.Strings[i]); // Element schon gefunden?
       if (j = -1) then // nein
         SL.AddObject(Memo1.Lines.Strings[i],Pointer(1)) // neu mit Counter 1 anlegen
       else
         SL.Objects[j] := Pointer(Integer(SL.Objects[j]) +1); // Anzahl +1
     end;
   finally
     SL.Free;
   end;
end;
cu
Narses

PS: Was hälst du von diesem Thread-Titel: "Häufigkeit von Strings in einem RichEdit zählen"? ;)

_________________
There are 10 types of people - those who understand binary and those who don´t.
DeddyH
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Sa 14.02.09 14:43 
Oder der Vollständigkeit halber mit meinem Quick' n Dirty-Ansatz:
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:
uses IniFiles;

procedure CountSameLines(const strIn, strOut: TStrings);
const sSection = 'Anzahl';
var i: Integer;
    memIni: TMemIniFile;
    sTemp: string;
    dummy: TStringList;
begin
  SetLength(sTemp, GetTempPath(0, nil));
  GetTempPath(Length(sTemp), PAnsiChar(sTemp));
  sTemp := IncludeTrailingPathDelimiter(sTemp) + 'temp.ini';
  memIni := TMemIniFile.Create(sTemp);
  strOut.BeginUpdate;
  try
    strOut.Clear;
    for i := 0 to strIn.Count-1 do
      memIni.WriteInteger(sSection,strIn[i],memIni.ReadInteger(sSection,strIn[i],0) + 1);
    dummy := TStringList.Create;
    try
      memIni.ReadSection(sSection,dummy);
      for i := 0 to dummy.Count-1 do
        strOut.Add(Format('%d %s',[memIni.ReadInteger(sSection,dummy[i],0),dummy[i]]));
    finally
      dummy.Free;
    end;
  finally
    memIni.Free;
    strOut.EndUpdate;
  end;
end;
DiamondDog Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 460



BeitragVerfasst: Sa 14.02.09 15:14 
Also ich habs jetzt mal so versucht:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
begin
   Memo2.Clear;
   SL := TStringList.Create;
   try
     for i := 0 to Memo1.Lines.Count-1 do // über alle Zeilen
       begin
       j := SL.IndexOf(Memo1.Lines.Strings[i]); // Element schon gefunden?
       if (j = -1) then // nein
         begin
           SL.AddObject(Memo1.Lines.Strings[i],Pointer(1)); // neu mit Counter 1 anlegen
         end else
         begin
           SL.Objects[j] := Pointer(Integer(SL.Objects[j]) +1); // Anzahl +1
         end;
       Memo2.Lines.Add(inttostr(j+2)+' '+Memo1.Lines.Strings[i]);
       end;
   finally
     SL.Free;
   end;
end;
Leider ist es so noch nicht ganz richtig, er mach immer nen Fehler bei Name1 und Name2 so sieht die Ausgabe aus:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
1 Name1
1 Name2
2 Name1
1 Name3
1 Name5
1 Name4
3 Name2
2 Name1
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Sa 14.02.09 15:34 
Moin!

Du darst in der Such-Schleife nicht schon die Ausgabe machen, dass muss in einer weiteren Schleife hinterher passieren. :idea: ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
DiamondDog Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 460



BeitragVerfasst: Sa 14.02.09 16:06 
Hier hab ich mal die Schleife eingabaut das mit der Ausgabe Verstehe ich auch nicht so ganz weil ich nicht versteh, wo die Zahlen hingeschrieben werden und wie ich diese genau dann wieder vor die Namen setze.
Hier mal der Code biß jetzt:
ausblenden 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:
begin
   Memo2.Clear;
   SL := TStringList.Create;
   try
   for i := 0 to Memo1.Lines.Count-1 do // über alle Zeilen
       begin
       j := SL.IndexOf(Memo1.Lines.Strings[i]); // Element schon gefunden?
       if (j = -1) then // nein
         begin
           SL.AddObject(Memo1.Lines.Strings[i],Pointer(1)); // neu mit Counter 1 anlegen
         end else
         begin
           SL.Objects[j] := Pointer(Integer(SL.Objects[j]) +1); // Anzahl +1
         end;
       end;
   // neue Schleife
   for i := 0 to SL.Count do // mit SL.Count bin ich mir nicht zu 100% sicher
     begin
       Memo2.Lines.Strings[i] := // bekomme ich leider nicht hin
     end;
   finally
     SL.Free;
   end;
end;
Bei SL.Count war ich mir nicht sicher aber wenn ich das richtig verstehe, muß ich ja da an der Stelle, die Länge der SL nehmen und nicht mehr vom Memo oder?
DeddyH
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Sa 14.02.09 16:26 
Um mal Logik und Darstellung zu trennen (Narses Version übernommen):
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
procedure CountSameLines(const strIn, strOut: TStrings);
var SL: TStringList;
    i,j: Integer;
begin
   SL := TStringList.Create;
   strOut.BeginUpdate;
   try
     strOut.Clear;
     for i := 0 to strIn.Count-1 do begin // über alle Zeilen
       j := SL.IndexOf(strIn[i]); // Element schon gefunden?
       if (j = -1) then // nein
         SL.AddObject(strIn[i],Pointer(1)) // neu mit Counter 1 anlegen
       else
         SL.Objects[j] := Pointer(Integer(SL.Objects[j]) +1); // Anzahl +1
     end;
     for i := 0 to SL.Count-1 do
       strOut.Add(Format('%d %s',[Integer(Pointer(SL.Objects[i])),SL[i]]));
   finally
     SL.Free;
     strOut.EndUpdate;
   end;
end;
DiamondDog Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 460



BeitragVerfasst: Sa 14.02.09 17:22 
Super habs mit dem Code von DeddyH hinbekommen.
Besten Dank an alle.