?
Solved

How to work with an array of 3 elements

Posted on 2006-11-03
8
Medium Priority
?
198 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
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.

 
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

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.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

801 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