Recent

Author Topic: I can't see the error  (Read 6624 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
I can't see the error
« on: August 01, 2020, 09:19:18 pm »
unit1.pas(1101,27) Fatal: Syntax error, "CREATE" expected but "ITEMS" found
I get a compiiler error on line 20
I don't see it What has Create got to do with it?

Thanks.

Code: Pascal  [Select][+][-]
  1.  
  2.  
  3. Types
  4.  LBArray = Array of String;  
  5.  
  6. Var
  7.  UpperBoxAP    : LBArray;    
  8.  
  9.  
  10.  
  11.  
  12. procedure TForm1.LoadUpperArrayLBAPArchive;
  13.   Var i : Integer = -1;
  14.    Idx  : Integer = -1;
  15.    Item : String = '';
  16.    begin
  17.    if LBAPAirchive.Items.Count = 0 then begin exit; end;
  18.    SetLength (UpperBoxAP,5);
  19.    for i := 0 to LBAPAirchive.Items.Count - 1 do begin
  20.        Item := UpperBoxAP.Items[i];                 {<==== Shows error on this line}
  21.        Inc(IDX);
  22.        if IDX > High(UpperBoxAP) then begin
  23.           SetLength(UpperBoxAP, (High(UpperBoxAP) + BlockSize));
  24.        end;
  25.        UpperBoxAP[Idx] := Item;
  26.    end;
  27.     SetLength(UpperBoxAP,(i+1));
  28.  end;        
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: I can't see the error
« Reply #1 on: August 01, 2020, 09:23:06 pm »
My guess, that should be:

Code: Pascal  [Select][+][-]
  1.        Item := UpperBoxAP[i];

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: I can't see the error
« Reply #2 on: August 01, 2020, 09:23:34 pm »
Why would a array of string have a property items?

Just use
Code: Pascal  [Select][+][-]
  1. UpperBoxAP[i]

jamie

  • Hero Member
  • *****
  • Posts: 6133
Re: I can't see the error
« Reply #3 on: August 01, 2020, 09:28:13 pm »
Guess he has a TStringList in his mind ?
The only true wisdom is knowing you know nothing

process_1

  • Guest
Re: I can't see the error
« Reply #4 on: August 01, 2020, 09:35:15 pm »
Why using dynamic arrays? it is a lot of work with...

Use TStrings declaration and TStringList clas instance if you use Items. That is why compiler assumed you should create it first.


Code: Pascal  [Select][+][-]
  1. Var
  2.  UpperBoxAP :  TStrings;
  3. ...
  4.  
  5.  UpperBoxAP := TStringList.Create()...
  6. ....
  7.  
  8.  UpperBoxAP.Free;
  9.  
  10.  
« Last Edit: August 01, 2020, 09:38:42 pm by process_1 »

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: I can't see the error
« Reply #5 on: August 01, 2020, 09:46:00 pm »
Argh, I now see what you are trying to do.

You want
Code: Pascal  [Select][+][-]
  1. Item := LBAPAirchive.Item[I]

(LBAPAirchive is probably already a TString which needs to be converted to a array of string)

Btw, the SetLength(UpperBoxAP,(i+1)) at the end is useless and should be removed because the array is already the correct size.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: I can't see the error
« Reply #6 on: August 01, 2020, 10:10:55 pm »
Yes, of course my bad.

"Btw, the SetLength(UpperBoxAP,(i+1)) at the end is useless and should be removed because the array is already the correct size."

I'll remove it and see if works.

Thanks all
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

process_1

  • Guest
Re: I can't see the error
« Reply #7 on: August 01, 2020, 10:20:49 pm »
Yes, of course my bad.

But, why you insist on dynamic array of strings?

It is nothing but overkilling task. And it is slow, hard to insert, delete,... in a word it is hard to work with.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: I can't see the error
« Reply #8 on: August 02, 2020, 12:23:56 am »
Yes, of course my bad.

But, why you insist on dynamic array of strings?

It is nothing but overkilling task. And it is slow, hard to insert, delete,... in a word it is hard to work with.

I don't really insist on anything.
Notice I'm posting  in beginners.
It's what I know how to do and and how to make it work.

I have a listbox of items and I need them in an array.


What would you suggest?
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

process_1

  • Guest
Re: I can't see the error
« Reply #9 on: August 02, 2020, 08:49:47 am »
If you are beginner in Pascal, I could make few suggestions...

According to your code, you simply copy data from LBAPAirchive to UpperBoxAP, but if there is no data to copy, old data in UpperBoxAP retain. Is that what you want?

Perhaps you want to make a log, adding all current data in LBAPAirchive to UpperBoxAP?

Anyway, if simply copy to array of strings according your code, the working change in your original code is  just this:

Code: Pascal  [Select][+][-]
  1. /// Item := UpperBoxAP.Items[i];
  2. Item := LBAPAirchive.Items[i];
  3.  

However, minimalistic fast code instead of dinamically enlarge array all the time is following:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.LoadUpperArrayLBAPArchive;
  2. var
  3.   i : Integer;  
  4. begin
  5.    
  6.    if LBAPAirchive.Items.Count = 0 then
  7.      exit;
  8.    
  9.    SetLength (UpperBoxAP, LBAPAirchive.Items.Count);
  10.    
  11.    for i := 0 to LBAPAirchive.Items.Count - 1 do
  12.      UpperBoxAP[i] := LBAPAirchive.Items[i];
  13.      
  14.  end;    
  15.  

If LBAPAirchive is TListBox component and UpperBoxAP is TStringList instance, then all is quite simple:

Code: Pascal  [Select][+][-]
  1.  
  2.   // To copy strings
  3.   UpperBoxAP.Assign(LBAPAirchive.Items);
  4.  
  5.   // To add strings
  6.   UpperBoxAP.AddStrings(LBAPAirchive.Items);
  7.  
  8.  

Just declare UpperBoxAP as I have shown already, create it in the part when create form, free it when destroy it  and use just these simple methods. And that is it, no hassle with dynamic array of strings.

Arrays are basic data in pascal, but since this is object Pascal, using TStringList classs instance is preferable in your case and compatible for all components using it and as well simple and clear to use.
« Last Edit: August 02, 2020, 09:39:47 am by process_1 »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: I can't see the error
« Reply #10 on: August 02, 2020, 01:49:46 pm »
Yes, of course my bad.

But, why you insist on dynamic array of strings?

It is nothing but overkilling task. And it is slow, hard to insert, delete,... in a word it is hard to work with.

Ehm... something like a TStringList has much more overkill and one needs to remember to free it. Not to mention that FPC 3.2.0 also supports Insert and Delete for dynamic arrays.

process_1

  • Guest
Re: I can't see the error
« Reply #11 on: August 02, 2020, 03:02:50 pm »
Ehm... something like a TStringList has much more overkill and one needs to remember to free it.

Heh, you make me to laugh now... :)

Well, of course the class instance need be free, that is basic techique in OOP and languages without automatic garbage collector (as in JAVA with it, it is insane IMO to leave such sensitive job to the system).

The worst thing with dynamic arrays is if forgotten to be cleaned up, it is very hard to locate memory leak, while here anyone know that Create need to be paired with Free.
« Last Edit: August 02, 2020, 03:07:50 pm by process_1 »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: I can't see the error
« Reply #12 on: August 02, 2020, 03:58:22 pm »
The worst thing with dynamic arrays is if forgotten to be cleaned up, it is very hard to locate memory leak, while here anyone know that Create need to be paired with Free.

There can basically only be two instances where a dynamic array isn't freed:
  • you used a global variable (but even that will be freed at the end of the program)
  • the dynamic array is part of a structured type (record, class instance) that in turn wasn't freed

Also for class instances it isn't necessarily clear when they need to be freed either. Of course there is the usual textbook example of allocation and freeing inside the same routine, but that's not always the case.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: I can't see the error
« Reply #13 on: August 02, 2020, 07:44:30 pm »
This is all very interesting. However, the program is written and fully tested. Very reluctant to rewrite at this point. I'm just fixing a small issue. But I have book marked  this page and will revisit.

With Dynamic arrays Inserts and Deletes that would help maybe.

I'm sure there is a better way to do what I'm doing.

I have 3 text files. One is the world's airports (45,000), world's Airlines(15,000) and world's aircraft(900). The  user needs to work with a subset of each of the three files.

I wanted to do this in one program. With each of the files the user needs to add and delete records. Each file has an archive where all of the records of each files is stored. And then there are the subsets the user has chosen for active use for each file.

Every month he needs to have a file of the top 1,000 airports in the world, top 125 airlines and top 50 cargo carriers. The aircraft don't change that much. He can only add from the archive files for airports and carriers. The data files are used by another program to make API Calls for live flight data.

The program shows in a listbox the 1000 airports.  The user types in a 4 letter ICAO airport code in a edit control and the program will either highlight the airport if it is one of the current 1000 or look it up in the archives. If found in archives it will display the info and allow it to be added.

FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

process_1

  • Guest
Re: I can't see the error
« Reply #14 on: August 02, 2020, 08:19:52 pm »
As well, be carefull when use SetLength() and dynamic arrays.

If shrink or delete list, it will remove array length, but not affected strings. You have to manually set empty string on all affected items before action, otherwise you will end up with massive memory leaks.

As I mentioned, too many problems with dynamic arrays and strings.
« Last Edit: August 02, 2020, 08:23:37 pm by process_1 »

 

TinyPortal © 2005-2018