![]() |
Re: Monatskürzel "Jan".."Dec" in 01..12
Bei mir sieht es nun so aus (dank der Anregungen aus diesem Thread!):
Delphi-Quellcode:
type
TLsLand = (GER,USA,GBT); function IndexOfArrayItem(TheArray : Array of String ;Item : String ):Integer; overload; function IndexOfArrayItem(TheArray : Array of Byte ;Item : Byte ):Integer; overload; function IndexOfArrayItem(TheArray : Array of Integer ;Item : Integer ):Integer; overload; function IndexOfArrayItem(TheArray : Array of Char ;Item : Char ):Integer; overload; function IndexOfArrayItem(TheArray : Array of Currency;Item : Currency):Integer; overload; function IndexOfArrayItem(TheArray : Array of Double ;Item : Double ):Integer; overload; function IndexOfArrayItem(TheArray : Array of Boolean ;Item : Boolean ):Integer; overload; function ShortMonStrToInt(Month : String; Land : TLsLand):Integer; function LongMonStrToInt(Month : String; Land : TLsLand):Integer; implementation function IndexOfArrayItem(TheArray : Array of String;Item : String):Integer; begin Result := High(TheArray); while (Result >= 0) and (TheArray[Result] <> Item) do Dec(Result); end; // usw. usf. für alle überladenen Versionen bis: function IndexOfArrayItem(TheArray : Array of Boolean;Item : Boolean):Integer; begin Result := High(TheArray); while (Result >= 0) and (TheArray[Result] <> Item) do Dec(Result); end; function ShortMonStrToInt(Month : String; Land : TLsLand):Integer; const EngArray : Array[0..11] of String = ('JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC'); GerArray1 : Array[0..11] of String = ('JAN','FEB','MRZ','APR','MAI','JUN','JUL','AUG','SEP','OKT','NOV','DEZ'); GerArray2 : Array[0..11] of String = ('JAN','FEB','MÄR','APR','MAI','JUN','JUL','AUG','SEP','OKT','NOV','DEZ'); begin case Land of USA,GBT : Result := IndexOfArrayItem(EngArray,AnsiUpperCase(Month)); GER : begin Result := IndexOfArrayItem(GerArray1,AnsiUpperCase(Month)); if Result < 0 then Result := IndexOfArrayItem(GerArray2,AnsiUpperCase(Month)); end; else raise Exception.Create('Nichtdefiniertes Land übergeben!'); end; if Result >= 0 then inc(Result); // damit man auf die Monatszahl kommt, muß um 1 erhöht werden. end; function LongMonStrToInt(Month : String; Land : TLsLand):Integer; begin Result := ShortMonStrToInt(copy(Month,1,3), Land); end; |
Re: Monatskürzel "Jan".."Dec" in 01..12
Delphi-Quellcode:
sag ich ma.
function monthbystr(s: string): integer;
const months = 'JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC'; begin result := Pos(Uppercase(s),months) shr 2; end; |
Re: Monatskürzel "Jan".."Dec" in 01..12
Hallo hboy,
leider hast Du bei Deiner Variante von Hagens Vorschlag (s.o.) den Fall, dass ein Kürzel keinem Monat entspricht außer Acht gelassen. Es gilt bei Deiner Lösung
Delphi-Quellcode:
so dass der Client (Aufrufer Deiner Routine) nicht zwischen einem ungültigen Kürzel und dem Januar unterscheiden kann.
monthbystr('jan')=monthbystr('wrong')
|
Re: Monatskürzel "Jan".."Dec" in 01..12
Delphi-Quellcode:
und es is immer noch kurz
function monthbystr(s: string): integer;
const months = 'JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC'; var position: integer; begin position:= Pos(Uppercase(s),months); if position = -1 then result := -1 else result := position shr 2; end; |
Re: Monatskürzel "Jan".."Dec" in 01..12
bzw
Delphi-Quellcode:
function monthbystr(s: string): integer;
const months = ' JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC'; begin result := Pos(Uppercase(s),months) shr 2; end; |
Re: Monatskürzel "Jan".."Dec" in 01..12
Hallo hboy,
ich will nicht nerven, aber erstens ergibt Deine erste Korrektur immer noch kein gültiges Ergebnis: Zitat:
Delphi-Quellcode:
so dass weiterhin ungültige Strings auf gültige Monatsnummern gemappt werden. Ich empfand die Lösung von Hagen schon als recht gelungen und wollte mit der (zugegeben etwas längeren) Variante mit dem Hashing nur einen Vorschlag für einen performanteren Ansatz machen...
monthbystr('Jan')=monthbystr('Jan F')
|
Re: Monatskürzel "Jan".."Dec" in 01..12
dachte pos ist beim fehlschlag -1...
naja dann eben
Delphi-Quellcode:
function monthbystr(s: string): integer;
const months = ' JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC'; var st: string[3]; begin st := s[1]+s[2]+s[3]; result := Pos(Uppercase(st),months) shr 2; end; |
Re: Monatskürzel "Jan".."Dec" in 01..12
ausserdem... wo gibts denn da probleme bei der Performance? das ist doch nicht nennenswert. oder ?
|
Re: Monatskürzel "Jan".."Dec" in 01..12
Ob es ein Poblem der Performance ist, hängt vom Anwendungsfall ab. Was ich weiß ist, dass bei Deiner nunmehr dritten Korrektur folgendes gilt, hboy:
Delphi-Quellcode:
Bei der ursprünglichen Lösung von Hagen (die sehr ähnlich zu Deinem Versuch ist) findest Du einen elegenten Ansatz zu diesem Problem. Ein paar Ideen zur Performance sind ebenfalls in diesem Thread zu finden.
monthbystr('n f')=monthbystr('jan')
|
Re: Monatskürzel "Jan".."Dec" in 01..12
dann mach doch noch
function monthbystr(s: string): integer; const months = ' JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC'; var st: string[3]; position: integer; begin st := s[1]+s[2]+s[3]; position:= Pos(Uppercase(st),months); result := position shr 2; if position mod 4 <>0 then result :=0; end; ansonsten. ich habs zumindest versucht. Ausserdem liese sich dieses ord(..) shr...+ord(..) shr...+ord(..) shr... vereinfachen: var s: string[4]; v: integer absolute s; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 13:09 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