----- Original Message -----
From: "Francisco Reyes" <[EMAIL PROTECTED]>
To: "FreePascal list" <fpc-pascal@lists.freepascal.org>
Sent: Tuesday, September 11, 2007 1:38 PM
Subject: [fpc-pascal] Pascals unit for associate arrays?
Is there any unit, or built in functionality, to do the equivalent of
perl's associate arrays or Python's dictionaries?
What I am trying todo is to parse some lines and to store ocurrences of
certain strings.
For example
string1
string2
string1
string3
string2
What I want to do is to create an entry string1 and count all string1 one
ocurrences, the same for each different string. In the end I would have
string1 2
string2 2
string3 1
I don't know about buil-in associative arrays or similar, but for starters
you could use a TStringList to do a similar thing (email typed and
untested):
Procedure AddStringToList(AList: TStringList; AString: String);
Var
Index : Integer;
Value : Integer;
Begin
If Not AList.Find(AString,Index) Then
AList.AddObject(AString,Pointer(1))
Else
Begin
Value := Integer(AList.Objects[Index]);
Inc(Value);
AList.Objects[Index] := Pointer(Value);
End;
End;
Function GetStringCount(AList: TStringList; AString: String): Integer;
Var
Index : Integer;
Begin
Result := 0;
If Not AList.Find(AString,Index) Then Exit;
Result := Integer(AList.Objects[Index]);
End;
MyList := TStringList.Create;
MyList.Sorted := True;
AddStringToList(MyList,'string1');
AddStringToList(MyList,'string2');
AddStringToList(MyList,'string1');
AddStringToList(MyList,'string3');
AddStringToList(MyList,'string2');
WriteLn('string1 ',GetStringCount(MyList,'string1'));
WriteLn('string2 ',GetStringCount(MyList,'string2'));
WriteLn('string3 ',GetStringCount(MyList,'string3'));
Should output
string1 2
string2 2
string3 1
Hope this helps,
cheers,
Paul
_______________________________________________
fpc-pascal maillist - fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal