Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2007-09

Pascals unit for associate arrays?

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Francisco Reyes

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

Reply by email

Paul Nicholls
to Francisco Reyes

----- Original Message -----

Earlier message 78 lines
From: "Francisco Reyes" To: "FreePascal list" 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

Reply by email

Marco van de Voort
to Francisco Reyes: Is there any unit, or built in functionality, to do the equivalent of perl's associate…
Show quoted text 19 lines
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

Have a look at Decal in contribs svn.

Reply by email