![]() |
"doppelten code" vermeiden?
hoi, in einem anderem thread hab ich diesen code gepostet gehabt:
Delphi-Quellcode:
wie man sieht sind der block oben und unten fast gleich, bis auf ' ' und '' (letzte beiden zeilen)
begin
Delete(params, 1, Pos(':', params)+1); Trim(params); while params <> '' do begin if Pos(' ', params) > 0 then begin SetLength(NickInfo, high(NickInfo)+1); NickInfo[high(NickInfo)].Nick := Copy(params, 1, Pos(',', params)-1); Delete(params, 1, Pos(',', params)); NickInfo[high(NickInfo)].ClanID := Copy(params, 1, Pos(',', params)-1); Delete(params, 1, Pos(',', params)); NickInfo[high(NickInfo)].LongIP := Copy(params, 1, Pos(' ', params)-1); Delete(params, 1, Pos(' ', params)); end else begin SetLength(NickInfo, high(NickInfo)+1); NickInfo[high(NickInfo)].Nick := Copy(params, 1, Pos(',', params)-1); Delete(params, 1, Pos(',', params)); NickInfo[high(NickInfo)].ClanID := Copy(params, 1, Pos(',', params)-1); Delete(params, 1, Pos(',', params)); NickInfo[high(NickInfo)].LongIP := Copy(params, 1, Pos('', params)-1); Delete(params, 1, Pos('', params)); end; end; end; es geht darum das ich z.b die wörter aus einem string holen möchte, sagen wir mal der string sieht z.b so aus: Zitat:
beim letzten wort gibt es aber kein whitespace mehr, sondern das ende ''... nur dafür hab ich den ganzen code nochmal schreiben müssen? (siehe 2ten block) es muss doch auch anders gehen oder? mfg |
Re: "doppelten code" vermeiden?
So sollte es gehen
Delphi-Quellcode:
var helpstring: String;
begin [...] if Pos(' ', params) > 0 then helpstring := ' ' else helpstring := ''; [...] NickInfo[high(NickInfo)].LongIP := Copy(params, 1, Pos(helpstring, params)-1); Delete(params, 1, Pos(helpstring, params)); |
Re: "doppelten code" vermeiden?
da hätt ich idiot auch drauf kommen können, danke sirthornberry :mrgreen:
|
Re: "doppelten code" vermeiden?
Noch ein paar kleine Fragen zu deinem Code:
Delphi-Quellcode:
Bist du sicher, das hier auch das gemacht wird, was du willst?
Trim(params);
Ich denke mir mal, es sollt wohl eher so aussehn:
Delphi-Quellcode:
params := Trim(params);
und dieser Teil dürft wohl auch nicht die gewünschten Resultate bringen:
Delphi-Quellcode:
Bei mir ist das Ergebnis immer 0:
Pos('', params)
Delphi-Quellcode:
Du könntest ja an params einfach ein ' ' anhängen, dann hat sich dein Problem gelöst:
Pos('', params) = 0
Delphi-Quellcode:
oder du arbeitest sauber (ohne vorher ein Pseudoleerzeichen anzuhängen) und machst das mit Length(params), was dann zum wegfallen von Copy und Delete führt:
begin
Delete(params, 1, Pos(':', params)+1); params := Trim(params) + ' '; while params <> '' do begin SetLength(NickInfo, high(NickInfo)+1); NickInfo[high(NickInfo)].Nick := Copy(params, 1, Pos(',', params)-1); Delete(params, 1, Pos(',', params)); NickInfo[high(NickInfo)].ClanID := Copy(params, 1, Pos(',', params)-1); Delete(params, 1, Pos(',', params)); NickInfo[high(NickInfo)].LongIP := Copy(params, 1, Pos(' ', params)-1); Delete(params, 1, Pos(' ', params)); end; end;
Delphi-Quellcode:
begin
Delete(params, 1, Pos(':', params)+1); Trim(params); while params <> '' do begin if Pos(' ', params) > 0 then begin SetLength(NickInfo, high(NickInfo)+1); NickInfo[high(NickInfo)].Nick := Copy(params, 1, Pos(',', params)-1); Delete(params, 1, Pos(',', params)); NickInfo[high(NickInfo)].ClanID := Copy(params, 1, Pos(',', params)-1); Delete(params, 1, Pos(',', params)); NickInfo[high(NickInfo)].LongIP := Copy(params, 1, Pos(' ', params)-1); Delete(params, 1, Pos(' ', params)); end else begin SetLength(NickInfo, high(NickInfo)+1); NickInfo[high(NickInfo)].Nick := Copy(params, 1, Pos(',', params)-1); Delete(params, 1, Pos(',', params)); NickInfo[high(NickInfo)].ClanID := Copy(params, 1, Pos(',', params)-1); Delete(params, 1, Pos(',', params)); {NickInfo[high(NickInfo)].LongIP := Copy(params, 1, Length(params));} NickInfo[high(NickInfo)].LongIP := params; {Delete(params, 1, Length(params));} params := ''; end; end; end; |
Re: "doppelten code" vermeiden?
mit dem trim hast du recht *g*
Zitat:
Zitat:
bei deinem letzten tip hab ich dann aber doch wieder 2 blöcke, also das mit dem "unsauberem" war mir doch lieber, also thx :] |
Re: "doppelten code" vermeiden?
Mir währe die 1. Variante auch lieber ;)
Zitat:
Code:
begin
Delete(params, 1, Pos(':', params)+1); Trim(params); while params <> '' do begin if Pos(' ', params) > 0 then begin ... end else begin ... NickInfo[high(NickInfo)].LongIP := Copy(params, 1, [color=red]Pos('', params)[/color]-1); Delete(params, 1, [color=red]Pos('', params)[/color]); end; end; end; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 15:27 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz