• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

How to work with an array of 3 elements

Hello guys,

I need to fill 1 array of 3 elements

The first element of my array is gonna have 100 words in english
The secound element of my array is gonna have 100 words in portuguese
The third element of my array is gonna have 100 words in portuguese

After filling the array, I need to scramble the secound element,

Thanks
0
hidrau
Asked:
hidrau
  • 4
  • 3
1 Solution
 
mikelittlewoodCommented:
Why use an array? Any particular reason?
Why not create your own object for this and use an objectlist instead.

TMyObject = class( TObject)
  private
    FEnglish,
    FPortuguese1,
    FPortuguese2: String;
  public
    property English: string read FEnglish write FEnglish;
    property Portuguese1: string read FPortuguese1 write FPortuguese1;
    property Portuguese2: string read FPortuguese2 write FPortuguese2;
  end;

var
  MyWordsList: TObjectList;

Then just create an object, store each version in each variable, then add the object to the objectlist

MyWordsList := TObjectList.Create( True);

WordObject := TMyObject.Create;
WordObject.English := 'Word1';
WordObject.Portuguese1 := 'Word1';
WordObject.Portuguese2 := 'Word1';

MyWordsList.Add( WordObject);

Make sure you free the objectlist on destroy of your form.
It will automatcially free the objects added to it as well

if Assigned( MyWordsList) then
  try
    MyWordsList.Free
  finally
    MyWordsList := nil
  end;
0
 
TNameCommented:
Hi,
Mike's solution looks fine to me.
But if you really want to use arrays, you could try something like this:


procedure TForm1.Button1Click(Sender: TObject);
var
anArray: Array [0..2] of Array [0..99] of string;
i,j:Integer;
begin
  for i:=0 to High(anArray) do begin
     for j:=0 to High(anArray[i]) do begin
       if i=0 then
         AnArray[i][j]:='English Word '+IntToStr(j)
       else
         AnArray[i][j]:='Portuguese Word '+IntToStr(i)+'.'+IntToStr(j);
     end;
  end;
  ShowMessage(AnArray[2][50]);
end;
0
 
TNameCommented:
BTW, if you're not used to arrays, object lists are the safer solution ;)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
hidrauAuthor Commented:
Tname,

All the information will come from a table and must be fill the same time.

for first element I will have a word "House" - in english
For the secound I will have its meaning in portuguese "Casa"
For the third I will have the same of the secound.

After filling my array I need to scramble the secound list, you see?

0
 
hidrauAuthor Commented:
ok, I did it

how can I scramble my array? the secound list?
0
 
TNameCommented:
By scramble you mean randomize, randomly exchange elements, right?
You could try something like this:

procedure TForm1.Scramble(var a:Array of String);
var
i,j: Integer;
s:String;
begin
  Randomize;
  for i:=High(a) downto 0 do begin
    j:=Random(High(a))+1;
    s:=a[j];
    a[j]:=a[i];
    a[i]:=s;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Scramble(AnArray[2]);
  Label1.Caption:=AnArray[2][50];
end;
0
 
hidrauAuthor Commented:
Tname, where are you from?
0
 
TNameCommented:
From  kretzschmar's country AND from ciuly's country :)
(living in Germany, I think I've mentioned this once)

Btw, your second list is AnArray[1] of course.
This scramble procedure doesn't guarantee that ALL elements will change places, because sometimes one or two elements will be scrambled back to where they originally were. If you want that no element should keep it's position under any circumstances, just say so...

Btw 2, have you seen the last version of the procedure in the "Only to improve a function" PAQ, the one that also gives you infos about labels? I posted that sometime after the Q was PAQed.


0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now