Solved

How to work with an array of 3 elements

Posted on 2006-11-03
8
192 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

867 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

23 Experts available now in Live!

Get 1:1 Help Now