Delphi-PRAXiS
Seite 3 von 4     123 4      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi Monatskürzel "Jan".."Dec" in 01..12 umwandeln (https://www.delphipraxis.net/15437-monatskuerzel-jan-dec-01-12-umwandeln.html)

Leuselator 2. Feb 2004 14:10

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;

hboy 2. Feb 2004 16:26

Re: Monatskürzel "Jan".."Dec" in 01..12
 
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;
sag ich ma.

choose 2. Feb 2004 16:36

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:
monthbystr('jan')=monthbystr('wrong')
so dass der Client (Aufrufer Deiner Routine) nicht zwischen einem ungültigen Kürzel und dem Januar unterscheiden kann.

hboy 2. Feb 2004 16:42

Re: Monatskürzel "Jan".."Dec" in 01..12
 
Delphi-Quellcode:
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;
und es is immer noch kurz

hboy 2. Feb 2004 16:43

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;

choose 2. Feb 2004 16:54

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:

Zitat von OH
In Delphi sucht Pos in dem String S nach dem Teil-String Substr. [..] Ist Substr nicht vorhanden, wird der Wert Null zurückgegeben.

und weiterhin gilt bei Deiner zweiten Korrektur
Delphi-Quellcode:
monthbystr('Jan')=monthbystr('Jan F')
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...

hboy 2. Feb 2004 17:04

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;

hboy 2. Feb 2004 17:05

Re: Monatskürzel "Jan".."Dec" in 01..12
 
ausserdem... wo gibts denn da probleme bei der Performance? das ist doch nicht nennenswert. oder ?

choose 2. Feb 2004 17:12

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:
monthbystr('n f')=monthbystr('jan')
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.

hboy 2. Feb 2004 17:20

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.
Seite 3 von 4     123 4      

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