Recent

Author Topic: Is possible to do something lime this?  (Read 1257 times)

NoReason

  • New Member
  • *
  • Posts: 21
Is possible to do something lime this?
« on: December 17, 2018, 02:11:51 am »
(FindComponent('grupa' + IntToStr((g+5) div(5))) as array)[g mod 5]:=country[r];

Thats wont work, so i need good syntax.
I want make 10 arrays with 5 indexes.

so g will going from 0 to 50, so when g is 0, i need grupa1[0], when g is 1 grupa1[1], when g is 6 grupa2[1], etc.


PS:I know it can be easy done with 10 if-s functions or case, but need to know is possible to do something like this?
« Last Edit: December 17, 2018, 02:16:11 am by NoReason »

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Is possible to do something lime this?
« Reply #1 on: December 17, 2018, 02:47:48 am »
Try my code:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Memo1: TMemo;
  17.     procedure Button1Click(Sender: TObject);
  18.   end;
  19.  
  20. var
  21.   Form1: TForm1;
  22.  
  23. implementation
  24.  
  25. {$R *.lfm}
  26.  
  27. { TForm1 }
  28.  
  29. procedure TForm1.Button1Click(Sender: TObject);
  30. const
  31.   Prefix = 'groupa';
  32. var
  33.   i: Integer;
  34. begin
  35.   Memo1.Clear;
  36.   for i := 0 to 50 do
  37.     Memo1.Append(Prefix + (i div 5 + 1).ToString +'['+ (i mod 5).ToString +']');
  38. end;
  39.  
  40. end.

-edit-
fix some mistakes.
« Last Edit: December 17, 2018, 02:59:58 am by Handoko »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Is possible to do something lime this?
« Reply #2 on: December 17, 2018, 03:29:22 am »
If what you're using are simple arrays you can't use FindComponent(), because it returns ... well, a TComponent descendant.

What you're trying to do would best be solved using an array of array:

Code: Pascal  [Select][+][-]
  1. var
  2.   groupa: array[1..10, 0..4] of String; { or of whatever }
  3.  
  4. procedure FillItem(g, r: integer);
  5. var
  6.   i, j: Integer;
  7. begin
  8.   if g in [0..50] then begin
  9.     i := (g div 5) + 1;
  10.     j := g mod 5;
  11.     groupa[i, j] :=  cities[r];
  12.   end else
  13.     with EArgumentOutOfRangeException.Create('g must be 0..50') do
  14.       Raise
  15. end;
  16.  

ETA: You'll probably have to tweak the constant 5 for the div and mod. It's 3:30 a.m. here and my brain is crying "foul" already :)
« Last Edit: December 17, 2018, 03:33:14 am by lucamar »
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.

NoReason

  • New Member
  • *
  • Posts: 21
Re: Is possible to do something lime this?
« Reply #3 on: December 17, 2018, 03:48:59 am »
Why i always must complicate when its obviously i should use double arrays, but first reply works too

thanks.

 

TinyPortal © 2005-2018