Recent

Author Topic: [CLOSED] Assignment to string array  (Read 783 times)

Davo

  • Full Member
  • ***
  • Posts: 131
[CLOSED] Assignment to string array
« on: July 07, 2020, 03:07:58 am »
Have searched the Forum and other Lazarus documentation quite extensively but not found an answer.

I want to assign a collection of string values to a string array. The following doesn't work giving the compiler error “Fatal: Syntax error, “)” expected but “,” found” after " 'aa'," at the assignment line :

Code: Pascal  [Select][+][-]
  1. procedure tryIt;
  2. var foo : array of string;
  3. begin
  4.   setlength(foo,4);
  5.   foo := ('aa','bb','cc','dd');
  6. end;

No doubt my syntax is wrong and guidance for setting me straight would be much appreciated. Thanks.
« Last Edit: July 07, 2020, 08:20:44 am by Davo »

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 805
    • Lebeau Software
Re: Assignment to string array
« Reply #1 on: July 07, 2020, 03:49:07 am »
In FPC 3.0+, you can do the following:

Code: Pascal  [Select][+][-]
  1. procedure tryIt;
  2. type
  3.   myArray = array of string;
  4. var
  5.   foo : myArray;
  6. begin
  7.   foo := myArray.Create('aa', 'bb', 'cc', 'dd');
  8. end;

This is documented in Dynamic Array: Initializing.

Otherwise, you will have to simply assign the array elements individually, eg:

Code: Pascal  [Select][+][-]
  1. procedure tryIt;
  2. var
  3.   foo : array of string;
  4. begin
  5.   SetLength(foo, 4);
  6.   foo[0] := 'aa';
  7.   foo[1] := 'bb';
  8.   foo[2] := 'cc';
  9.   foo[3] := 'dd';
  10. end;
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Davo

  • Full Member
  • ***
  • Posts: 131
Re: Assignment to string array
« Reply #2 on: July 07, 2020, 04:04:00 am »
Bless you Remy. That's exactly what I need. Sorry about missing the mention in Dynamic Array : Initialising.

PascalDragon

  • Hero Member
  • *****
  • Posts: 1957
  • Compiler Developer
Re: [CLOSED] Assignment to string array
« Reply #3 on: July 07, 2020, 09:35:57 am »
No doubt my syntax is wrong and guidance for setting me straight would be much appreciated. Thanks.

Starting with FPC 3.2.0 the following is supported:

Code: Pascal  [Select][+][-]
  1. procedure tryIt;
  2. var foo : array of string;
  3. begin
  4.   foo := ['aa','bb','cc','dd'];
  5. end;

A SetLength is not necessary in that case. Internally it generates the same code that Remy Lebeau showed in his second example.

simone

  • Sr. Member
  • ****
  • Posts: 352
Re: Assignment to string array
« Reply #4 on: July 07, 2020, 10:01:32 am »
A doubt about this new feature: the documentation says "The [...] syntax can be used to initialize dynamic array variables in code or to pass them to dynamic arrays parameters". But in addition to initialization, it seems to me that it is possible to do subsequent assignments with the same syntax. So not only it is a new syntax for array constructors, but also for array assignments. It's correct?
Microsoft Windows 10 64 bit - Lazarus 2.0.6

PascalDragon

  • Hero Member
  • *****
  • Posts: 1957
  • Compiler Developer
Re: [CLOSED] Assignment to string array
« Reply #5 on: July 07, 2020, 01:07:09 pm »
No, it's only an array constructor. The result of the […] expression is an ordinary dynamic array, however you can then work with it as if it would be a dynamic array variable:

Code: Pascal  [Select][+][-]
  1.   arr := [1, 2, 3 4];
  2.   arr := [1, 2] + someOtherArr; // requires mode Delphi or modeswitch ArrayOperators
  3.   SomeDynArrFunc([1, 2, 3, 4]);

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 805
    • Lebeau Software
Re: [CLOSED] Assignment to string array
« Reply #6 on: July 08, 2020, 12:01:28 am »
Starting with FPC 3.2.0 the following is supported:

Code: Pascal  [Select][+][-]
  1. procedure tryIt;
  2. var foo : array of string;
  3. begin
  4.   foo := ['aa','bb','cc','dd'];
  5. end;

Cool.  Where is that documented?

No, it's only an array constructor. The result of the […] expression is an ordinary dynamic array, however you can then work with it as if it would be a dynamic array variable:

OK, but that "constructed array" can be "assigned" to an existing dynamic array variable, yes? Because that is exactly what both of your examples are doing (foo := ['aa','bb','cc','dd']; and arr := [1, 2, 3 4];).

I suspect something like the following:

Code: Pascal  [Select][+][-]
  1. procedure tryIt;
  2. var
  3.   foo : array of string;
  4. begin
  5.   foo := ['aa','bb','cc','dd'];
  6.   foo := ['ee','ff'];
  7. end;

To be treated like this:

Code: Pascal  [Select][+][-]
  1. procedure tryIt;
  2. var
  3.   foo : array of string;
  4. begin
  5.   //foo := ['aa','bb','cc','dd'];
  6.   SetLength(foo, 4);
  7.   foo[0] := 'aa';
  8.   foo[1] := 'bb';
  9.   foo[2] := 'cc';
  10.   foo[3] := 'dd';
  11.   //foo := ['ee','ff'];
  12.   SetLength(foo, 2);
  13.   foo[0] := 'ee';
  14.   foo[1] := 'ff';
  15. end;

And not more like this:

Code: Pascal  [Select][+][-]
  1. procedure tryIt;
  2. var
  3.   foo, tmp : array of string;
  4. begin
  5.   //foo := ['aa','bb','cc','dd'];
  6.   SetLength(foo, 4);
  7.   foo[0] := 'aa';
  8.   foo[1] := 'bb';
  9.   foo[2] := 'cc';
  10.   foo[3] := 'dd';
  11.   //foo := ['ee','ff'];
  12.   SetLength(tmp, 2);
  13.   tmp[0] := 'ee';
  14.   tmp[1] := 'ff';
  15.   foo := tmp;
  16. end;

Is that correct?
« Last Edit: July 08, 2020, 12:08:30 am by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

PascalDragon

  • Hero Member
  • *****
  • Posts: 1957
  • Compiler Developer
Re: [CLOSED] Assignment to string array
« Reply #7 on: July 08, 2020, 09:23:14 am »
Starting with FPC 3.2.0 the following is supported:

Code: Pascal  [Select][+][-]
  1. procedure tryIt;
  2. var foo : array of string;
  3. begin
  4.   foo := ['aa','bb','cc','dd'];
  5. end;

Cool.  Where is that documented?

I think it was forgotten to document this in the documentation proper. It's mentioned here however. Maybe you want to open a bug report for this? :)

No, it's only an array constructor. The result of the […] expression is an ordinary dynamic array, however you can then work with it as if it would be a dynamic array variable:

OK, but that "constructed array" can be "assigned" to an existing dynamic array variable, yes? Because that is exactly what both of your examples are doing (foo := ['aa','bb','cc','dd']; and arr := [1, 2, 3 4];).

As said, it's essentially treated as a dynamic array variable.

I suspect something like the following:

Code: Pascal  [Select][+][-]
  1. procedure tryIt;
  2. var
  3.   foo : array of string;
  4. begin
  5.   foo := ['aa','bb','cc','dd'];
  6.   foo := ['ee','ff'];
  7. end;

To be treated like this:

Code: Pascal  [Select][+][-]
  1. procedure tryIt;
  2. var
  3.   foo : array of string;
  4. begin
  5.   //foo := ['aa','bb','cc','dd'];
  6.   SetLength(foo, 4);
  7.   foo[0] := 'aa';
  8.   foo[1] := 'bb';
  9.   foo[2] := 'cc';
  10.   foo[3] := 'dd';
  11.   //foo := ['ee','ff'];
  12.   SetLength(foo, 2);
  13.   foo[0] := 'ee';
  14.   foo[1] := 'ff';
  15. end;

And not more like this:

Code: Pascal  [Select][+][-]
  1. procedure tryIt;
  2. var
  3.   foo, tmp : array of string;
  4. begin
  5.   //foo := ['aa','bb','cc','dd'];
  6.   SetLength(foo, 4);
  7.   foo[0] := 'aa';
  8.   foo[1] := 'bb';
  9.   foo[2] := 'cc';
  10.   foo[3] := 'dd';
  11.   //foo := ['ee','ff'];
  12.   SetLength(tmp, 2);
  13.   tmp[0] := 'ee';
  14.   tmp[1] := 'ff';
  15.   foo := tmp;
  16. end;

Is that correct?

It's more like this:

Code: Pascal  [Select][+][-]
  1. procedure tryIt;
  2. var
  3.   foo, tmp : array of string;
  4. begin
  5.   //foo := ['aa','bb','cc','dd'];
  6.   SetLength(tmp, 4);
  7.   tmp[0] := 'aa';
  8.   tmp[1] := 'bb';
  9.   tmp[2] := 'cc';
  10.   tmp[3] := 'dd';
  11.   foo := tmp;
  12.   //foo := ['ee','ff'];
  13.   SetLength(tmp, 2);
  14.   tmp[0] := 'ee';
  15.   tmp[1] := 'ff';
  16.   foo := tmp;
  17. end;

There might be improvements in the future however.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 805
    • Lebeau Software
Re: [CLOSED] Assignment to string array
« Reply #8 on: July 08, 2020, 07:58:21 pm »
It's more like this:

Code: Pascal  [Select][+][-]
  1. procedure tryIt;
  2. var
  3.   foo, tmp : array of string;
  4. begin
  5.   //foo := ['aa','bb','cc','dd'];
  6.   SetLength(tmp, 4);
  7.   tmp[0] := 'aa';
  8.   tmp[1] := 'bb';
  9.   tmp[2] := 'cc';
  10.   tmp[3] := 'dd';
  11.   foo := tmp;
  12.   //foo := ['ee','ff'];
  13.   SetLength(tmp, 2);
  14.   tmp[0] := 'ee';
  15.   tmp[1] := 'ff';
  16.   foo := tmp;
  17. end;

Good, I was hoping that was the case.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

simone

  • Sr. Member
  • ****
  • Posts: 352
Re: [CLOSED] Assignment to string array
« Reply #9 on: July 11, 2020, 01:28:18 pm »
Starting with FPC 3.2.0 the following is supported:

Code: Pascal  [Select][+][-]
  1. procedure tryIt;
  2. var foo : array of string;
  3. begin
  4.   foo := ['aa','bb','cc','dd'];
  5. end;

Cool.  Where is that documented?

I think it was forgotten to document this in the documentation proper. It's mentioned here however. Maybe you want to open a bug report for this? :)

I already opened a bug report some time ago, but at the moment there is no update.

https://bugs.freepascal.org/view.php?id=37256

I can't even find the documentation for the rtl-generics library now distributed with fpc. Hope the documentation can be updated soon.

Thanks for the great job done.

Microsoft Windows 10 64 bit - Lazarus 2.0.6

PascalDragon

  • Hero Member
  • *****
  • Posts: 1957
  • Compiler Developer
Re: [CLOSED] Assignment to string array
« Reply #10 on: July 11, 2020, 02:41:23 pm »
I already opened a bug report some time ago, but at the moment there is no update.

https://bugs.freepascal.org/view.php?id=37256

I'm sure that Michael will find the time some time in the future. :)

I can't even find the documentation for the rtl-generics library now distributed with fpc. Hope the documentation can be updated soon.

It's a rather big piece of code, so it takes some time to document it. For now you can mostly rely on Delphi's documentation as the public classes are compatible. Other than that, patches are welcome...

simone

  • Sr. Member
  • ****
  • Posts: 352
Re: [CLOSED] Assignment to string array
« Reply #11 on: July 11, 2020, 03:45:59 pm »
I'm aware that the documentation activity is very expensive and I infinitely appreciate the effort of the development team. I just note that the online reference guide is published as relative to fpc 3.2.0. Perhaps it would be better to leave it labeled as version 3.0.4.
Microsoft Windows 10 64 bit - Lazarus 2.0.6

PascalDragon

  • Hero Member
  • *****
  • Posts: 1957
  • Compiler Developer
Re: [CLOSED] Assignment to string array
« Reply #12 on: July 11, 2020, 05:32:21 pm »
I'm aware that the documentation activity is very expensive and I infinitely appreciate the effort of the development team. I just note that the online reference guide is published as relative to fpc 3.2.0. Perhaps it would be better to leave it labeled as version 3.0.4.

No, because it was generated based on the documentation that was adjusted for 3.2.0 and it contains information that does not apply to 3.0.4. Many units don't have documentation, that is no reason to label the whole documentation incorrectly.

 

TinyPortal © 2005-2018