Delphi 如何在记录数组中按字段查找特定记录
我知道这可能是一个基本的问题,但问题来了。 我定义了一个常量记录数组:Delphi 如何在记录数组中按字段查找特定记录,delphi,Delphi,我知道这可能是一个基本的问题,但问题来了。 我定义了一个常量记录数组: TDocumentKindInfo = record Employee: integer; First: string; Last: string; Title: string; end; const CDocumentKindInfos: array[TDocumentKind] of TDocumentKindInfo = ( (Emplyee: 1; First
TDocumentKindInfo = record
Employee: integer;
First: string;
Last: string;
Title: string;
end;
const
CDocumentKindInfos: array[TDocumentKind] of TDocumentKindInfo = (
(Emplyee: 1; First: 'Bob'; Last: 'Fredricks'; Title: 'Manager'),
(Emplyee: 2; First: 'Bill'; Last: 'Evans'; Title: 'Cashier'),
(Emplyee: 3; First: 'Jill'; Last: 'Dunne'; Title: 'Stocker'),
...
如何找到作为经理的员工姓名。我环顾四周,找不到任何有用的东西。我是德尔菲的初学者。使用Delphi7。尝试以下代码
for I:= Low(CDocumentKindInfos) to high(CDocumentKindInfos) do
begin
if CDocumentKindInfos[I].Title = 'Manager' then
begin
ShowMessage('Employee name is ' + CDocumentKindInfos[I].Employee);
Break;
end;
end;
我通常会这样写:
function FindDocumentByTitle(const Title: string): TDocumentKind;
begin
for Result := low(Result) to high(Result) do
if SameText(Title, CDocumentKindInfos[Result].Title) then
exit;
raise EDocumentNotFound.CreateFmt('Document titled ''%s'' not found.', [Title]);
end;
type TData = record
Points: Integer;
Count: Cardinal;
flags: Cardinal;
end;
PData = ^Data;
TDataArray = array of TData
PDataArray = ^TDataArray
...
var LongNameForSomeNouns: TDataArray
Noun: PData; //pointer to the current Record
iNoun: Integer;
...
for iNoun:=Low(LongNameForSomeNouns) to High(LongNameForSomeNouns) do begin
Noun := @LongNameForSomeNouns[iNoun];
Noun.Points := 5;
Inc(Noun.Count);
SomeFunction(Noun);
end;
如果找不到项,函数将引发错误。很快你就会在课堂上总结所有这些问题。我意识到这个问题已经问了3年了,但我想我应该补充一下答案 在遍历记录数组时,我经常编写如下代码:
function FindDocumentByTitle(const Title: string): TDocumentKind;
begin
for Result := low(Result) to high(Result) do
if SameText(Title, CDocumentKindInfos[Result].Title) then
exit;
raise EDocumentNotFound.CreateFmt('Document titled ''%s'' not found.', [Title]);
end;
type TData = record
Points: Integer;
Count: Cardinal;
flags: Cardinal;
end;
PData = ^Data;
TDataArray = array of TData
PDataArray = ^TDataArray
...
var LongNameForSomeNouns: TDataArray
Noun: PData; //pointer to the current Record
iNoun: Integer;
...
for iNoun:=Low(LongNameForSomeNouns) to High(LongNameForSomeNouns) do begin
Noun := @LongNameForSomeNouns[iNoun];
Noun.Points := 5;
Inc(Noun.Count);
SomeFunction(Noun);
end;
我想提出三点:
是的,这只对某些类型的程序重要,而且只有在分析之后。但我注意到了这一点,速度提高了50-100倍。一个过去需要一周时间来处理1000万条数据库记录的程序现在可以在一夜之间完成。它以前是CPU绑定的,现在完全是数据库IO绑定的 或者
SameText(CDocumentKindInfos[I].Title,'Manager')
如果您想不区分大小写您在if中执行赋值,它将不会编译:)+1 for good循环,结果是迭代索引-这样的代码快速高效-唯一的缺点是您必须为错误指定一些超出范围的元素。您可以使用const dknown:TDocumentKind=TDocumentKind(255);若要创建一个“虚拟”TDocumentKind项目以在找不到它时返回。@a.Bouchez在没有明显的未找到哨兵的情况下,我刚刚选择了raise。我更喜欢raise例外。这样,您就不必编写9层以上的if-error-Then-exit代码。