Solved

How to work with an array of 3 elements

Posted on 2006-11-03
8
191 Views
Last Modified: 2010-04-05
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
Comment
Question by:hidrau
  • 4
  • 3
8 Comments
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 17867456
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
 
LVL 28

Expert Comment

by:TName
ID: 17867498
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
 
LVL 28

Expert Comment

by:TName
ID: 17867572
BTW, if you're not used to arrays, object lists are the safer solution ;)
0
 
LVL 1

Author Comment

by:hidrau
ID: 17869222
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 1

Author Comment

by:hidrau
ID: 17869284
ok, I did it

how can I scramble my array? the secound list?
0
 
LVL 28

Accepted Solution

by:
TName earned 500 total points
ID: 17869642
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
 
LVL 1

Author Comment

by:hidrau
ID: 17869735
Tname, where are you from?
0
 
LVL 28

Expert Comment

by:TName
ID: 17870162
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now