Recent

Author Topic: How can i find (or select ) Array with name??  (Read 5986 times)

majid.ebru

  • Sr. Member
  • ****
  • Posts: 494
How can i find (or select ) Array with name??
« on: January 15, 2019, 06:06:21 am »
Hi

i have many array like these :
Code: Pascal  [Select][+][-]
  1. const
  2.   //----- Verb 2 ----------------
  3.   {V01 = Verb 1 ___ 11 = Group 11 ___ 11 = + ___ 12 = -}
  4.   V021111 : array[0..02] of String = ('V17','V08','V19');
  5.   V021112 : array[0..03] of String = ('V10','V17','V08','V19');
  6.   V021121 : array[0..02] of String = ('V08','V09','V19');
  7.   V021122 : array[0..03] of String = ('V10','V08','V09','V19');
  8.   V021131 : array[0..02] of String = ('V30','V08','V09');
  9.   V021132 : array[0..03] of String = ('V10','S21','V08','V09');//-------------
  10.   V021211 : array[0..03] of String = ('V08','V17','V12','V19');
  11.   V021212 : array[0..04] of String = ('V08','V10','V17','V12','V19');
  12.   V021221 : array[0..02] of String = ('V08','V09','V19');
  13.   V021222 : array[0..03] of String = ('V08','V10','V09','V19');
  14.   V021231 : array[0..02] of String = ('V08','S21','V09');
  15.   V021232 : array[0..03] of String = ('V08','V10','S21','V09');//-------------
  16.   V021311 : array[0..02] of String = ('V08','V12','V19');
  17.   V021312 : array[0..03] of String = ('V08','V10','V12','V19');
  18.   V021321 : array[0..02] of String = ('V08','V09','V19');
  19.   V021322 : array[0..03] of String = ('V08','V10','V09','V19');
  20.   V021331 : array[0..02] of String = ('V08','S21','V09');
  21.   V021332 : array[0..03] of String = ('V08','V10','S21','V09');
  22. //--------------------
  23.  
  24.  

then i get name of array :
Code: Pascal  [Select][+][-]
  1. tAry[0] :=  P1_E_3.Text + P1_E_2.Text; // --->  like : V021322
  2.  

then i should send same array to my function "Find_DB('2',....)" :
Code: Pascal  [Select][+][-]
  1.       case(tAry[0])of
  2.         'V021111': tAry[0] := Find_DB('2',V02_11_11);
  3.         'V021112': tAry[0] := Find_DB('2',V02_11_12);
  4.         'V021121': tAry[0] := Find_DB('2',V02_11_21);
  5.         'V021122': tAry[0] := Find_DB('2',V02_11_22);
  6.         'V021131': tAry[0] := Find_DB('2',V02_11_31);
  7.         'V021132': tAry[0] := Find_DB('2',V02_11_32);
  8.         'V021211': tAry[0] := Find_DB('2',V02_12_11);
  9.         'V021212': tAry[0] := Find_DB('2',V02_12_12);
  10.         'V021221': tAry[0] := Find_DB('2',V02_12_21);
  11.         'V021222': tAry[0] := Find_DB('2',V02_12_22);
  12.         'V021231': tAry[0] := Find_DB('2',V02_12_31);
  13.         'V021232': tAry[0] := Find_DB('2',V02_12_32);
  14.         'V021311': tAry[0] := Find_DB('2',V02_13_11);
  15.         'V021312': tAry[0] := Find_DB('2',V02_13_12);
  16.         'V021321': tAry[0] := Find_DB('2',V02_13_21);
  17.         'V021322': tAry[0] := Find_DB('2',V02_13_22);
  18.         'V021331': tAry[0] := Find_DB('2',V02_13_31);
  19.         'V021332': tAry[0] := Find_DB('2',V02_13_32);
  20.         else ShowMessage('Error- U277');
  21.       end;
  22.  


i know this is mistek,but i need same that:


Code: Pascal  [Select][+][-]
  1. findcomponent('V02_13_32')as array //   this is misteck, I know
  2.  

can anybody help or guide me how can i select array with name
« Last Edit: January 16, 2019, 12:52:55 pm by majid.ebru »

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: How can i find (or elect ) Array with name??
« Reply #1 on: January 15, 2019, 07:04:31 am »
can anybody help or guide me how can i select array with name
The most flexible option would be to use an external file. But it will require a thorough check of its structure when loading.
If this option does not appeal, you can add references to arrays in TStringList with the desired names and look for them there. The list can be sorted.

majid.ebru

  • Sr. Member
  • ****
  • Posts: 494
Re: How can i find (or elect ) Array with name??
« Reply #2 on: January 15, 2019, 11:24:34 am »
The most flexible option would be to use an external file

How can i ?

you can add references to arrays in TStringList with the desired names and look for them there. The list can be sorted.

can you show me one example or one link about that?

Handoko

  • Hero Member
  • *****
  • Posts: 5129
  • My goal: build my own game engine using Lazarus
Re: How can i find (or elect ) Array with name??
« Reply #3 on: January 15, 2019, 02:52:26 pm »
I can't understand your explanation. Can you provide some examples data for the inputs and what are the outputs?

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How can i find (or elect ) Array with name??
« Reply #4 on: January 15, 2019, 03:14:59 pm »
The most flexible option would be to use an external file

How can i ?

Instead of using constants write a file with lines like, p.e.:
    V021212=V08 V10 V17 V12 V19

You can read this file as an INI file (see p.e. Using INI files) and build your arrays at run-time with the help of a TStrings descendant, using TStrings.DelimitedText

And this is just one option: following on the preceding idea you may use instead an INI file with sections such as:
Code: [Select]
[V021212]
Item0=V08
Item1=V10
Item2=V17
Item3=V12
Item4=V19
reading each section as needed with TIniFile.ReadSectionValues or TIniFile.ReadString and building your arrays on the fly.

Read the FCL docs for more info.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: How can i find (or elect ) Array with name??
« Reply #5 on: January 15, 2019, 04:12:06 pm »
you can add references to arrays in TStringList with the desired names and look for them there. The list can be sorted.
can you show me one example or one link about that?
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2. {$APPTYPE CONSOLE}
  3. {$LONGSTRINGS ON}
  4.  
  5. uses
  6.   Classes;
  7.  
  8. const
  9.   V021111: array[0..2] of string = ('V17', 'V08', 'V19');
  10.   V021112: array[0..3] of string = ('V10', 'V17', 'V08', 'V19');
  11.   V021121: array[0..2] of string = ('V08', 'V09', 'V19');
  12.   V021122: array[0..3] of string = ('V10', 'V08', 'V09', 'V19');
  13.  
  14. function ArrayAddrToObject(const Addr: Pointer): TObject; inline;
  15. begin
  16.   Result := TObject(Addr);
  17. end;
  18.  
  19. function ItemOfStringArray(ArrayAddr: TObject; Index: Integer): string; inline;
  20. begin
  21.   Result := string(PPointerArray(Pointer(ArrayAddr))^[Index]);
  22. end;
  23.  
  24. var
  25.   ListOfArrays: TStringList;
  26.   What: string;
  27.   Where: Integer;
  28. begin
  29.   ListOfArrays := TStringList.Create;
  30.   try
  31.     ListOfArrays.Sorted := True;
  32.     ListOfArrays.AddObject('V021111', ArrayAddrToObject(@V021111));
  33.     ListOfArrays.AddObject('V021112', ArrayAddrToObject(@V021112));
  34.     ListOfArrays.AddObject('V021121', ArrayAddrToObject(@V021121));
  35.     ListOfArrays.AddObject('V021122', ArrayAddrToObject(@V021122));
  36.     What := 'V021121';
  37.     if ListOfArrays.Find(What, Where) then
  38.     begin
  39.       Writeln('Found!');
  40.       Writeln(ItemOfStringArray(ListOfArrays.Objects[Where], 2));  // V19
  41.     end
  42.     else
  43.       Writeln('Not found');
  44.   finally
  45.     ListOfArrays.Free;
  46.   end;
  47.   Readln;
  48. end.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How can i find (or elect ) Array with name??
« Reply #6 on: January 15, 2019, 05:21:15 pm »
An alternative approach which checks for an invalid index would be something like this:
Code: Pascal  [Select][+][-]
  1. program ArrayConsts;
  2.  
  3. {$MODE OBJFPC}
  4. {$IfDef windows} {$APPTYPE CONSOLE} {$EndIf}
  5. {$LONGSTRINGS ON}
  6.  
  7. const
  8.   ArrayCount = 18; // adjust this and the const to suit
  9.  
  10. type
  11.   TIntStrArray = array[0..Pred(ArrayCount)] of array of String;
  12.  
  13. const
  14.  
  15.   IntStrArray: TIntStrArray = (('V17','V08','V19'),
  16.                                ('V10','V17','V08','V19'),
  17.                                ('V08','V09','V19'),
  18.                                ('V10','V08','V09','V19'),
  19.                                ('V30','V08','V09'),
  20.                                ('V10','S21','V08','V09'),
  21.                                ('V08','V17','V12','V19'),
  22.                                ('V08','V10','V17','V12','V19'),
  23.                                ('V08','V09','V19'),
  24.                                ('V08','V10','V09','V19'),
  25.                                ('V08','S21','V09'),
  26.                                ('V08','V10','S21','V09'),
  27.                                ('V08','V12','V19'),
  28.                                ('V08','V10','V12','V19'),
  29.                                ('V08','V09','V19'),
  30.                                ('V08','V10','V09','V19'),
  31.                                ('V08','S21','V09'),
  32.                                ('V08','V10','S21','V09'));
  33.  
  34.   function FoundIndexedItemInArrayNamed(const aName: String; anIndex: Integer; out Item: String): Boolean;
  35.   var
  36.     i: Integer = -1;
  37.   begin
  38.     Item := '';
  39.     Result := False;
  40.     case aName of
  41.       'V021111': i := 0;
  42.       'V021112': i := 1;
  43.       'V021121': i := 2;
  44.       'V021122': i := 3;
  45.       'V021131': i := 4;
  46.       'V021132': i := 5;
  47.       'V021211': i := 6;
  48.       'V021212': i := 7;
  49.       'V021221': i := 8;
  50.       'V021222': i := 9;
  51.       'V021231': i := 10;
  52.       'V021232': i := 11;
  53.       'V021311': i := 12;
  54.       'V021312': i := 13;
  55.       'V021321': i := 14;
  56.       'V021322': i := 15;
  57.       'V021331': i := 16;
  58.       'V021332': i := 17;
  59.       else Exit;
  60.                 end;
  61.     if anIndex <= High(IntStrArray[i]) then
  62.       begin
  63.            Result := True;
  64.            Item := IntStrArray[i][anIndex];
  65.       end;
  66.   end;
  67.  
  68. var
  69.   what, outStr: String;
  70. begin
  71.   what := 'V021121';
  72.   if FoundIndexedItemInArrayNamed(what, 2, outStr) then
  73.     WriteLn(what,' index ',2,' is ',outStr)
  74.   else
  75.     WriteLn(what,' index ',2,' not found');
  76.   ReadLn;
  77. end.

majid.ebru

  • Sr. Member
  • ****
  • Posts: 494
Re: How can i find (or elect ) Array with name??
« Reply #7 on: January 16, 2019, 09:03:45 am »
thank you

 :( :( :(

Handoko

  • Hero Member
  • *****
  • Posts: 5129
  • My goal: build my own game engine using Lazarus
Re: How can i find (or select ) Array with name??
« Reply #8 on: January 16, 2019, 02:53:14 pm »
Why not happy face?

majid.ebru

  • Sr. Member
  • ****
  • Posts: 494
Re: How can i find (or select ) Array with name??
« Reply #9 on: January 19, 2019, 08:40:46 pm »
Why not happy face?

because i don't want to use "case"!!

if i want to use "case",my codes worked and these are OK.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: How can i find (or select ) Array with name??
« Reply #10 on: January 19, 2019, 11:30:12 pm »
You need to use a formatted array so that you can create an Array[0..?] of a SpecificRecord.

The record will contain the fields you need.

You will need a size field for the  number of String entries because the way you are doing it now just isn't
going to work well for searching the list
The only true wisdom is knowing you know nothing

majid.ebru

  • Sr. Member
  • ****
  • Posts: 494
Re: How can i find (or select ) Array with name??
« Reply #11 on: January 21, 2019, 07:46:04 pm »
You need to use a formatted array so that you can create an Array[0..?] of a SpecificRecord.

The record will contain the fields you need.

You will need a size field for the  number of String entries because the way you are doing it now just isn't
going to work well for searching the list

ok

can you show an example

thank you
 :'( :'( :'( :'(

majid.ebru

  • Sr. Member
  • ****
  • Posts: 494
Re: How can i find (or select ) Array with name??
« Reply #12 on: January 21, 2019, 09:06:37 pm »
I have not used the "record " ever

i searched

Code: Pascal  [Select][+][-]
  1. type
  2.   TVerb = record
  3.     Name: string[10];
  4.     Arry: array of String;
  5.   end;
  6.  TVerbList = array of TVerb;  
  7.  

in event of button:
Code: Pascal  [Select][+][-]
  1. procedure TF1.Button2Click(Sender: TObject);
  2. begin
  3.   TVerbList[0].Name:= 'V01_1111';
  4.   TVerbList[0].Arry:= ('S04','V08','S10');
  5.  
  6.  
  7.   TVerbList[1].Name:= 'V01_1121';
  8.   TVerbList[1].Arry:= ('S06','V08','S10','V09','S15');
  9.  
  10. end;
  11.  

but i have errors.

how can i put data in record??

guide or help me
« Last Edit: January 21, 2019, 09:08:24 pm by majid.ebru »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How can i find (or select ) Array with name??
« Reply #13 on: January 21, 2019, 10:24:21 pm »
It is not clear what you are trying to do.
However, if you want to use record constants (?) you have to pre-declare them.
You cannot create such constants on-the-fly in an event handler.
Are you looking for something like the following?
Code: Pascal  [Select][+][-]
  1. type
  2.  
  3.    TVerb = record
  4.      Name: string[10];
  5.      Arry: array of String;
  6.    end;
  7.    TVerbList = array of TVerb;
  8.  
  9. const
  10.  
  11.   Verb0: TVerb = (Name:'V01_1111'; Arry:('S04','V08','S10'));
  12.   Verb1: TVerb = (Name:'V01_1121'; Arry:('S06','V08','S10','V09','S15'));
  13.  
  14. type
  15.  
  16.   TForm1 = class(TForm)
  17.                 Button1: TButton;
  18.                 procedure Button1Click(Sender: TObject);
  19.   private
  20.     VerbList: TVerbList;
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. procedure TForm1.Button1Click(Sender: TObject);
  29. begin
  30.   SetLength(VerbList, 2);
  31.   VerbList[0] := Verb0;
  32.   VerbList[1] := Verb1;
  33. end;

Kays

  • Hero Member
  • *****
  • Posts: 569
  • Whasup!?
    • KaiBurghardt.de
Re: How can i find (or select ) Array with name??
« Reply #14 on: January 21, 2019, 10:26:20 pm »
[…] i have many array like these : […]
Analyzing your identifier names, you have a tree.

If you got data following a structure, you should organize it as such, i.e. your arrays should be part of a tree. Anything else are just attempts to repair a broken system. That won't work. Redesign your project and your issue will be solved.
Yours Sincerely
Kai Burghardt

 

TinyPortal © 2005-2018