* * *

Author Topic: associative arrays in lazarus?  (Read 8401 times)

powtils

  • New member
  • *
  • Posts: 18
Re: associative arrays in lazarus?
« Reply #15 on: January 31, 2012, 10:02:41 pm »
How about TBucketList?

The problem I see with pascal is there is no single way to do it, you have way too many options. In PHP everyone uses consistent syntax for the associative array. In php, if you want to use an associative array, you use the associative array..  In pascal we have all sorts of ways of doing it.

What is the best way? contnrs unit? stringlist? generics?

How would you do it with a stringlist?
« Last Edit: January 31, 2012, 10:04:32 pm by powtils »

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1189
Re: associative arrays in lazarus?
« Reply #16 on: January 31, 2012, 10:17:59 pm »
How about TBucketList?

The problem I see with pascal is there is no single way to do it, you have way too many options.

You never have too many options, at least not till they bite eachother.

Quote
In PHP everyone uses consistent syntax for the associative array. In php, if you want to use an associative array, you use the associative array.. 
In pascal we have all sorts of ways of doing it.

That is because PHP is hardly typed, and quite often stringly typed. Pascal is strong typed, and stringly typing is not done.

So the problem is that you transfer a concept from PHP to Pascal, and then conclude there is no good way. That's not true, you are trying to solve problems the PHP way. Don't! This is Pascal!

That is like concluding that PHP will never amount to nothing because it has no pointer, because other major languages have it (C,C++,Pascal etc)
« Last Edit: February 01, 2012, 11:52:01 am by marcov »

Leledumbo

  • Hero Member
  • *****
  • Posts: 2997
Re: associative arrays in lazarus?
« Reply #17 on: February 01, 2012, 03:26:25 am »
Quote
In PHP everyone uses consistent syntax for the associative array. In php, if you want to use an associative array, you use the associative array..  In pascal we have all sorts of ways of doing it.
Because arrays in PHP are by default associative and it's a language construct (and no, you can't modify its complexity without recompiling the whole interpreter). In Pascal, it's implemented as a user defined type, contained in certain units.

Free Pascal do have a lot of data structures scattered all over packages and units. But as with any other languages (implementations), 3rd party libs do exist and sometimes packaged (or taken over) along with the standard distribution. This is what happens to fcl-stl, which seems to compete with fgl. Generics in Free Pascal existed AFTER the existence of classes and contnrs unit, and since modifying those units didn't look feasible, a new fgl unit was created. So it's not Pascal itself that has a lot of ways of doing the same thing, but the libraries that we wrote.

powtils

  • New member
  • *
  • Posts: 18
Re: associative arrays in lazarus?
« Reply #18 on: February 01, 2012, 10:13:29 pm »
That is because PHP is hardly typed, and quite often stringly typed. Pascal is strong typed, and stringly typing is not done.
Actually in Pascal people use stringlists and use Name=Value pairs which emulates associative arrays, but in the original posters request, it is more complicated and requires something else.

See:
http://www.delphibasics.co.uk/RTL.asp?Name=TStringList

Where they use Name=Value pairs to bypass pascal's strong typing.

Pascal is not fully strongly typed, that is why you use pointers in TLists which is dangerous and requires ugly casting that bloats up the source with line noise. generics and some other things help solve it, but as I am a minimalist I would like to see something simple without generics. If generics is required to do it then i would use that.  But what about TBucketList and what about your suggestion of using stringlist with arrays? I don't understand what you would do with arrays/stringlist.. would it require any pointers or casting?

So the problem is that you transfer a concept from PHP to Pascal, and then conclude there is no good way. That's not true, you are trying to solve problems the PHP way. Don't! This is Pascal!

I never concluded that there was no good way in pascal, I meant: how you would do this particular example with stringlists and arrays added to the stringlist like you suggested in this old post?

I would like to see something like TAssociativeArray in pascal, why can't that be done? We just call it something else because associative array is not the right name for it?

What sucks greatly about PHP is that it has no STRUCT or RECORD. I do not like PHP, I was simply saying that at least PHP has an associative array that everyone uses consistently, similar to how pascal has records that everyone uses consistently. 

The problem with PHP is that it has no integrity of your data, it is like SQLite in that you can add anything to a cell without there being any type sterility. As dijkstra says, this is like doctors not worrying about bacteria back in the old days and not sterilizing there tools. It is a step back, not a step forward. If PHP had an associative array that you could force consistent data in the array (rather than allowing dynamic typing) I think PHP would be much better. I think we can do this in pascal, so why not a pascal TAssociativeArray available (without dynamic typing, it would force integrity of the data unlike PHP)?
« Last Edit: February 01, 2012, 10:19:41 pm by powtils »

powtils

  • New member
  • *
  • Posts: 18
Re: associative arrays in lazarus?
« Reply #19 on: February 01, 2012, 10:25:54 pm »
Because arrays in PHP are by default associative and it's a language construct (and no, you can't modify its complexity without recompiling the whole interpreter). In Pascal, it's implemented as a user defined type, contained in certain units.

True but for example TStringList is standard and no one goes and reinvents stringlist everywhere scattered across several units. Most people all just use stringlist for storing lists of strings. So my question is, why can't we create a standard TAssociativeArray instead of renaming it HashList BucketList and all these things.  I understand in some cases that some data structures are better than others in different situations..   Imagine if there was a TAssociativeArray and everyone just used that when they wanted an associative array.. that would end all the trouble, no? And when they don't need an associative array, they use something else like a TStringList.  Or how about IntToStr.. no one goes and reinvents IntToStr in there own units, we all use a standard IntToStr function since it has been created for us.   Even though someone could reinvent IntToStr, most of us use the standard one provided. So if there was a standard associative array available... It would be good. Now I don't mean an associative array that you can add anything you want to it like SqlLite, I mean one that forces type integrity. If this is not possible in Pascal due to some limitation, or if it requires using ugly pointers and casts or there are show stoppers of some other sort, I would like to know.

Troodon

  • Sr. Member
  • ****
  • Posts: 485
Re: associative arrays in lazarus?
« Reply #20 on: February 02, 2012, 12:01:00 am »
"ugly pointers and casts" -- There are two features that make (some) programming languages really powerful: pointers and recursion. Mastering those two is an almost lost art.
Lazarus/FPC on Linux

powtils

  • New member
  • *
  • Posts: 18
Re: associative arrays in lazarus?
« Reply #21 on: February 02, 2012, 01:05:33 am »
http://sourceforge.net/projects/fundementals/

unit: cDataStructs.pas

Code: [Select]
{   Library:          Fundamentals 4.00                                        }
{   File name:        cDataStructs.pas                                         }
{   File version:     4.31                                                     }
{   Description:      Data structures                                          }
....

DICTIONARIES                                                               
                                                                             
   Dictionaries are associative arrays where the key value is a string.       
                                                                             
   Associative arrays, also referred to as mappings, are unordered           
   collections where an arbitrary key can be used to index a value.         
                                                                             
   This unit implements dictionary classes for each of the following types:   
     + Integer                                                               
     + Cardinal                                                               
     + Int64                                                                 
     + Single                                                                 
     + Double                                                               
     + Extended                                                           
     + Pointer                                                               
     + String                                                                 
     + TObject                                                             
     + IInterface                                                         
                                                                             
   For example, the class TIntegerDictionary is used where the key is an     
   arbitrary string and the value an integer.                                 
                                                                             
       Ages := TIntegerDictionary.Create;                                     
       Ages['John'] := 29;                                                   
       Ages['Tori'] := 35;                                                   
       if Ages.HasKey['John'] then                                           
         Ages.Delete('John');                                                 
       Ages.Free;               

But that is not all, it contains TStringDictionary and many more, not just TIntegerDictionary

Leledumbo

  • Hero Member
  • *****
  • Posts: 2997
Re: associative arrays in lazarus?
« Reply #22 on: February 02, 2012, 04:10:07 pm »
Quote
why can't we create a standard TAssociativeArray instead of renaming it HashList BucketList and all these things
Because everyone has their own needs. Space, speed, other optimizations. Choose wisely for your specific needs. fgl's TFPGMap is backed up by simple flat list, fcl-stl's TMap is backed up by a set (again) backed up by a red black tree (if I'm not mistaken), others might be a hashmap.
Quote
I understand in some cases that some data structures are better than others in different situations..   Imagine if there was a TAssociativeArray and everyone just used that when they wanted an associative array.. that would end all the trouble, no?
No, at least not always. Still related to above: it depends on the specific requirements. What device? How fast? How big is the available memory?
Quote
Or how about IntToStr.. no one goes and reinvents IntToStr in there own units, we all use a standard IntToStr function since it has been created for us.   Even though someone could reinvent IntToStr, most of us use the standard one provided. So if there was a standard associative array available... It would be good.
Then use TFPGMap from fgl, like I do most of the time. Or TMap from fcl-stl if you need a faster variant.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1189
Re: associative arrays in lazarus?
« Reply #23 on: February 02, 2012, 09:23:54 pm »
That is because PHP is hardly typed, and quite often stringly typed. Pascal is strong typed, and stringly typing is not done.
Actually in Pascal people use stringlists and use Name=Value pairs which emulates associative arrays,

Well it is what you want to see. Someobdy with a PHP background would see an associative array everywhere I guess.  :P

In reality, I think TStringList is basically an array around a array of string, that grew later. The fact that it is not sorted or an hash by default makes that impression even stronger (since that causes slow lookups, lookups being not tstringlists forte anyway).

Quote
Pascal is not fully strongly typed, that is why you use pointers in TLists which is dangerous and requires ugly casting that bloats up the source with line noise. generics and some other things help solve it, but as I am a minimalist I would like to see something simple without generics.

This sounds like you are confusing safe typing and strong typing, which are often correlated, but not necessarily the same. Let alone other subjective things like "line noise" "sourcecode bloat" etc.

TCollection is actually the main VCL container type btw,  not TList.  (which is actually more like TP's TCollection)

Quote
If generics is required to do it then i would use that.

Possible.

Quote
  But what about TBucketList

I never use it. I sometimes use hashes if I have to do a lot of string lookups, but that is not that common.

Quote
and what about your suggestion of using stringlist with arrays? I don't understand what you would do with arrays/stringlist.

That is for the rest. The main suggestion is to stop thinking of strings as array indexes, or more important as keys for data.

Quote
would it require any pointers or casting?

Could be, irrelevant.

Quote
So the problem is that you transfer a concept from PHP to Pascal, and then conclude there is no good way. That's not true, you are trying to solve problems the PHP way. Don't! This is Pascal!

I never concluded that there was no good way in pascal, I meant: how you would do this particular example with stringlists and arrays added to the stringlist like you suggested in this old post?

I wouldn't ever do it like that. I would resolve strings to something unique as soon as possible, and not use strings as keys. Without a full problem description I can't make a tailormade advise.

Which was the point mainly, the entire problem description is alien to Pascal.

Quote
I would like to see something like TAssociativeArray in pascal, why can't that be done? We just call it something else because associative array is not the right name for it?

Maybe there is something in fgl, but that is besides the point. It is not a way that I would advise even it some generic version was in there. At least not for the bulk of the cases for which such question might be raised by a newbie.

One could just as well ask how to do

Code: [Select]
for i:='one' to 'four' do
 ;

I won't answer that question too by providing a solution for that, but ask for background.

Quote
What sucks greatly about PHP is that it has no STRUCT or RECORD. I do not like PHP, I was simply saying that at least PHP has an associative array that everyone uses consistently,

PHP only has that, and puts that center to datastructures. Pascal has other datastructures, so use them, and don't emulate PHP
 

chrnobel

  • Full Member
  • ***
  • Posts: 126
Re: associative arrays in lazarus?
« Reply #24 on: February 03, 2012, 09:11:23 am »
"ugly pointers and casts" -- There are two features that make (some) programming languages really powerful: pointers and recursion. Mastering those two is an almost lost art.
Yes mastering those is an art, but many programmers are not necessarily artist, but are having a more artisan perspective on life, as they need to have a job done.

And IMO there is no need to work with pointers if having decent arrays, I have made tons of programs, used in the real world, and I have never used any pointers, but relied on the arrays etc (which might be created by the artists, and from my point of view something the compiler shall deal with, not me) in the toolbox I have been using.
This is also why I like Pascal so much, as I do not need to enter the lions pit, and by the end of the day that is what keeping me productive.

And without any scientific proof, I am convinced that a lot of unstable C-whatever programs are the effect of usage of pointers, where the programmer have lost the overview, and where no one in Pascal in a sober state would have made a similar stupid construction, but hey we use C-whatever, and that is serious programming and blablabla snobbing.

Leledumbo

  • Hero Member
  • *****
  • Posts: 2997
Re: associative arrays in lazarus?
« Reply #25 on: February 03, 2012, 12:48:27 pm »
Quote
And IMO there is no need to work with pointers if having decent arrays, I have made tons of programs, used in the real world, and I have never used any pointers, but relied on the arrays etc (which might be created by the artists, and from my point of view something the compiler shall deal with, not me) in the toolbox I have been using.
This is also why I like Pascal so much, as I do not need to enter the lions pit, and by the end of the day that is what keeping me productive.
Agree... Pascal has pointers, but no need to use it except when REALLY REALLY necessary. Use any kind of abstractions the language (and the compiler) has provided as much as possible.

Troodon

  • Sr. Member
  • ****
  • Posts: 485
Re: associative arrays in lazarus?
« Reply #26 on: February 04, 2012, 02:01:31 am »
Yes, until the best solution to a problem requires pointers, in which case you had better be familiar with data structures and agorithms that use pointers.

I have been wondering how some of you would solve this programming exercise:

----

Consider a bamboo stick. The number of nodes in the stick, including the ends, is N > 2. Each internode segment is distinct from the others. The stick can be cut off at any node but not between nodes. List all the combinations of stick fragments that are generated by cutting off the stick in all possible ways (Hint: the number of cuts can be 1, 2, ..., N-2). The value of N is unknown at design time.

----

I am sure one could solve the exercise using any programming language, including PHP or Javascript, but I am curious to know how you would do this in Object Pascal. Thanks.
« Last Edit: February 04, 2012, 05:12:42 am by Troodon »
Lazarus/FPC on Linux

Leledumbo

  • Hero Member
  • *****
  • Posts: 2997
Re: associative arrays in lazarus?
« Reply #27 on: February 04, 2012, 05:04:11 am »
Quote
I have been wondering how some of you would solve this programming exercise
It's more algorithmic than data structural. I mean, no (sophisticated/complicated) data structure is required to solve it, just a correct technique. There's a big hint the question: "List all the combinations" so it's merely a combination generator problem, with repeating cases however, thus a dynamic programming would be necessary.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1189
Re: associative arrays in lazarus?
« Reply #28 on: February 04, 2012, 11:53:56 am »

I have been wondering how some of you would solve this programming exercise:

Read the description, get vibe that it is probably NP complete, unassign myself, and assign it to the person with a mathematics degree :-)
 

chrnobel

  • Full Member
  • ***
  • Posts: 126
Re: associative arrays in lazarus?
« Reply #29 on: February 04, 2012, 12:25:18 pm »
Read the description, get vibe that it is probably NP complete, unassign myself, and assign it to the person with a mathematics degree :-)
Agree.
This is purely a mathematical problem, it has nothing to do with pointers, and anyone saying that has understood both things equally bad.

Besides that, from a quick glance, the problems you face in making a solution is more or less similar to the problems you face in quicksort, and quicksort does not require usage of pointers.

Let me repeat, my life is to short to mess around with pointers, and I have made several hundreds of programs using Delphi and FPC over the last 15 years+, single user or multi user applications, web applications for a wider audience etc. etc., and I have never declared a single pointer and neither do I intent to do so in the future.

That does not mean that I do not acknowledge that the much wiser guys than me, namely those developing FPC etc. do rely in using pointers, but to me that is a compiler problem, and I trust them to be good at their job.
For Average Joes like me, it is plain stupid, and in no sense necessarily to use pointers what so ever.

I also am very well aware that if one are programming micro controllers in assembler it is a damned necessity, but I consider that as a totally other ball game.

And that all the c-whatever snobs glue them selves into a sticky web of pointers must be their own problem.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads