Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to work with an array of 3 elements

Posted on 2006-11-03
8
Medium Priority
?
200 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

609 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