IMHO.WS

IMHO.WS (https://www.imho.ws/index.php)
-   Программирование (https://www.imho.ws/forumdisplay.php?f=40)
-   -   Хэши в Delphi (практически как в Perl'e =) ) (https://www.imho.ws/showthread.php?t=66827)

f00rd 23.08.2004 10:30

Хэши в Delphi (практически как в Perl'e =) )
 
Где-то был вопрос по созданию хэшей в Delphi, но я его не нашел ирешил выложить то, до чего я дошел...
Код:

type
  TStringHash = class(TObject)
  private
    values, names:array of string;
    function Get(Name:String):string;
    procedure Put(Name:string;const S:string);
  public
    property Strings[Name: String]: string read Get write Put; default;
    constructor Create;
  end;
...
implementation

{$R *.dfm}

constructor TStringHash.Create;
begin
 inherited Create;

 SetLength(values,0);
 SetLength(names, 0);
end;

procedure TStringHash.Put(Name:string;const S:string);
var
 i:integer;
begin
 for i:=0 to Length(names)-1 do
  begin
  if names[i]=name then
    begin
    values[i]:=s;
    exit;
    end;
  end;

 SetLength(names,Length(names)+1);
 SetLength(values,Length(values)+1);
 names[Length(names)-1]:=name;
 values[Length(values)-1]:=s;
end;

function TStringHash.Get(Name:String):string;
var
 i:integer;
begin
 Result:='';
 if Length(names)=0 then exit;
 for i:=0 to Length(names)-1 do
  begin
  if names[i]=name then
    begin
    Result:=values[i];
    exit;
    end;
  end;
end;

Пример использования:
Код:

var
 k:TStringHash;
begin
 k:=TStringHash.Create;
 k['help']:='help me!';
 k['m']:='mamant';

 ShowMessage('help='+k['help']+#13+'m='+k['m']+#13+'none='+k['none']);

 k.Free;

Надеюсь, кому-нить пригодится =)

ЗЫ: конечно этот пример работает не как в Perl'е и требует доработок =)


Часовой пояс GMT +4, время: 16:10.

Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.