루프를 반복하면서 배열의 여러 속성을 확인하면됩니다. 다음은 이름, 주소, 전화 또는 전자 메일 중 일치하는 항목을 검색하는 예제입니다. 여러 레코드 속성 (예 : 및 주소와 같은)에서 일치하는 항목을 찾으려면 테스트에서 or
절을 if (Customers[Idx].Name = Name) and (Customers[Idx].Address = Address) then
과 같이 and
을 사용하는 두 개 이상의 테스트로 바꿉니다.
type
TCustomer = record
Name: string[40];
Address: string[100];
Phone: string[15];
Email:string[50];
end;
TCustomerList: array of TCustomer;
function FindCustomer(const Name, Address, EMail,
Phone: string; const Customers: TCustomerList): Integer;
var
i: Integer;
begin
Result := -1; // Value if no match found
for i := Low(Customers) to High(Customers) do
begin
if (Customers[i].Name = Name) or // Name matches?
(Customers[i].Address = Address) or // Address?
(Customers[i].EMail = EMail) or // Same email?
(Customers[i].Phone = Phone) then // Same phone
begin
Result := i; // Yep. We have a match.
Exit; // We're done.
end;
end;
end;
샘플 사용 :
function FindCustomerByNameAndAddress(const Name, Address: string;
const Customers: TCustomerList): Integer;
var
i: Integer;
begin
Result := -1; // Value if no match found
for i := Low(Customers) to High(Customers) do
begin
if (Customers[i].Name = Name) then // Name matches.
if (Customers[i].Address = Address) then // Does address?
begin
Result := i; // Yep. We found it
Exit;
end;
end;
end;
샘플 사용 :
var
Idx: Integer;
begin
// Customers is your array of TCustomer in a TCustomerList
Idx := FindCustomer('', '', '', '[email protected]', Customers);
if (Idx = -1) then
WriteLn('No match found.')
else
WriteLn(Format('Customer %d: %s %s %s %s',
[Idx,
Customers[Idx].Name,
Customers[Idx].Address,
Customers[Idx].Phone,
Customers[Idx].EMail]));
end;
(예 : Name
및 Address
등) 값의 조합에 일치하려면, 그냥 적절하게 if
의 조건을 변경 :
Idx := FindCustomerByNameAndAddress('John Smith', '123 Main Street`);
if Idx = -1 then
// Not found
else
// Found. Same code as above to access record.
배열을 반복하여 일치하는 항목을 찾으십시오. 왜 고정 길이 문자열을 사용하는지 궁금합니다. –
@DavidHeffernan thats 내 요점은, 루프 내에서 배열에서 1 개의 특정 것을 검색 할 때만 할 수 있습니다. 너는 어떻게 생각하고 있니? : S – user3478431
궁극적으로 몇 가지 기준에 의해 일치를 검사하는 모든 요소를 둘러싼 루프가 있습니다. 여러 경기를 찾으려면 할 수 있습니다. 특정 문제가있는 경우 알려주십시오. –