### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Arbitrary complex data structure?  (Read 1736 times)

#### giahung1997

• Full Member
• Posts: 113
##### Arbitrary complex data structure?
« on: June 07, 2019, 10:25:16 am »
« Last Edit: June 20, 2019, 02:20:41 pm by giahung1997 »

• Hero Member
• Posts: 10444
##### Re: Arbitrary complex data structure?
« Reply #1 on: June 07, 2019, 03:09:17 pm »
Dynamic arrays can be ragged arrays. I gave an example on this forum some time ago.
Simplest example:
Code: Pascal  [Select][+][-]
1. {\$mode delphi}
2. var
3.    a:array of array of integer;
4. begin
5.    a:= [[1,2,3,4],[1,2,3]]; // now 4 X 3, rectangular
6.    a[1] := [4,5]; // change 1 element to 2 instead of three: ragged, no longer rectangular
7. end.
Needs FPC 3.2.0 or higher.
You can build infinitely complex structures using this technique. Does that help?
« Last Edit: June 07, 2019, 03:20:00 pm by Thaddy »
When you ask a question that is actually answered in the documentation, you are either lazy or a moron.

#### Zoran

• Hero Member
• Posts: 1589
##### Re: Arbitrary complex data structure?
« Reply #2 on: June 19, 2019, 07:34:42 pm »
Dynamic arrays can be ragged arrays. I gave an example on this forum some time ago.
Simplest example:
Code: Pascal  [Select][+][-]
1. {\$mode delphi}
2. var
3.    a:array of array of integer;
4. begin
5.    a:= [[1,2,3,4],[1,2,3]]; // now 4 X 3, rectangular
6.    a[1] := [4,5]; // change 1 element to 2 instead of three: ragged, no longer rectangular
7. end.
Needs FPC 3.2.0 or higher.
You can build infinitely complex structures using this technique. Does that help?

In this example, I don't understand how is a rectangular in the first place?!

#### lucamar

• Hero Member
• Posts: 3017
##### Re: Arbitrary complex data structure?
« Reply #3 on: June 19, 2019, 08:31:50 pm »
In this example, I don't understand how is a rectangular in the first place?!

The line:

Code: [Select]
`a:= [[1,2,3,4],[1,2,3]];`
creates an array of 4 rows, 3 columns per row (or viceversa ); i.e. a rectangular array, as if it had been declared as:
a: array[0..3, 0..2] of Integer;
« Last Edit: June 19, 2019, 08:34:56 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!)
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

• Hero Member
• Posts: 10444
##### Re: Arbitrary complex data structure?
« Reply #4 on: June 19, 2019, 08:43:42 pm »

In this example, I don't understand how is a rectangular in the first place?!
Well , that's disappointing  because it shows an arbitrary array that  I understood what OP meant.
When you ask a question that is actually answered in the documentation, you are either lazy or a moron.

#### Zoran

• Hero Member
• Posts: 1589
##### Re: Arbitrary complex data structure?
« Reply #5 on: June 19, 2019, 09:12:40 pm »
In this example, I don't understand how is a rectangular in the first place?!

The line:

Code: [Select]
`a:= [[1,2,3,4],[1,2,3]];`
creates an array of 4 rows, 3 columns per row (or viceversa ); i.e. a rectangular array, as if it had been declared as:
a: array[0..3, 0..2] of Integer;

Strange syntax it is.

I would expect that initializer to allocate and populate array a to something like this:
a[0] = [1, 2, 3, 4] -- four elements
a[1] = [1, 2, 3] -- three elements

but, if I understand you well, it is actually:
a[0] ... allocate three integers (will they be initialized? to what? zeros?)
a[1] ... ditto
a[2] ... ditto
a[3] ... ditto

?

« Last Edit: June 19, 2019, 09:15:29 pm by Zoran »

#### lucamar

• Hero Member
• Posts: 3017
##### Re: Arbitrary complex data structure?
« Reply #6 on: June 19, 2019, 09:49:15 pm »
Yes, that's it. It's kind of an array constructor; something like BASIC's Dim() sentence.

That is,  if I understood correctly the new sintax

ETA: Oh, re. initialization, IIRC they are initialized to the Default for the items' type. in that same constructor? I'm not sure.

It was announced in May, 2018 in (where else? ) the fpc-anounce mailing list and extensively discused in fpc-pascal
« Last Edit: June 19, 2019, 10:12:47 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!)
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

#### Zoran

• Hero Member
• Posts: 1589
##### Re: Arbitrary complex data structure?
« Reply #7 on: June 19, 2019, 11:28:30 pm »

It was announced in May, 2018 in (where else? ) the fpc-anounce mailing list and extensively discused in fpc-pascal

But no, according to this announcement, it will be what I thought in the first place -- it will allocate 4+3 = 7 integers (and initialize them to given values). It will not allocate 4 * 3 = 12 integers.

So, the array was not rectangular in the first place.

#### lucamar

• Hero Member
• Posts: 3017
##### Re: Arbitrary complex data structure?
« Reply #8 on: June 20, 2019, 09:34:52 am »
But no, according to this announcement, it will be what I thought in the first place -- it will allocate 4+3 = 7 integers (and initialize them to given values). It will not allocate 4 * 3 = 12 integers.

So, the array was not rectangular in the first place.

Hmmm ... You're right. It's not understandable otherwise.

Seems I'll have to re-learn quite some things after the 3.2 release
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!)
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

#### Zoran

• Hero Member
• Posts: 1589
##### Re: Arbitrary complex data structure?
« Reply #9 on: June 20, 2019, 10:38:48 am »
Ragged array like this is not what I means and I myself think it ugly. When people from JVM based language background said data structure he means Classes.

There are different container classes:
The simplest: TFPList

Then, TList
TColection

Then, generic containers (unit fgl)
Apart from these generic containers from fgl unit, there is more efficient generics.collections library, but not yet available in current FPC 3.0.

Some information in wiki: https://wiki.freepascal.org/Data_Structures,_Containers,_Collections

#### marcov

• Global Moderator
• Hero Member
• Posts: 8716
• FPC developer.
##### Re: Arbitrary complex data structure?
« Reply #10 on: June 20, 2019, 10:56:06 am »
I think he means something like Tuples in some languages. It is not an original java or C# feature, but one that was added later because their VMs got it added for other functional and/or scripting languages. (LINQ)

And while you can emulate it with arrays of variants to some agree (and TDataset etc does), the scripting like language level syntax is not there. And hopefully never comes.

If you want a scripting language, may I suggest Python?
« Last Edit: June 22, 2019, 12:58:37 pm by marcov »

• Hero Member
• Posts: 10444
##### Re: Arbitrary complex data structure?
« Reply #11 on: June 20, 2019, 11:32:09 am »
Yes, and has excellent FPC bindings...
When you ask a question that is actually answered in the documentation, you are either lazy or a moron.

#### giahung1997

• Full Member
• Posts: 113
##### Re: Arbitrary complex data structure?
« Reply #12 on: June 20, 2019, 03:33:47 pm »
I think he means something like Tuples in some languages. It is not an original java or C# feature, but one that was added later because their VMs got it added for other functional and/or scripting languages.

And while you can emulate it with arrays of variants to some agree (and TDataset etc does), the scripting like language level syntax is not there. And hopefully never comes.

If you want a scripting language, may I suggest Python?
« Last Edit: June 21, 2019, 03:12:45 am by giahung1997 »

#### PascalDragon

• Hero Member
• Posts: 2102
• Compiler Developer
##### Re: Arbitrary complex data structure?
« Reply #13 on: June 21, 2019, 10:09:27 am »