• Status: Solved
• Priority: Medium
• Security: Public
• Views: 460

Hi All,

This is not my homework.

I am looking for an better/different  way to load an array other than what is shown
below

Sample 1
procedure TForm1.Button1Click(Sender: TObject);

var
ArrayOfInteger: array[0..48] of Integer;
begin
ArrayOfInteger[0] := 61;
ArrayOfInteger[1] := 39;
ArrayOfInteger[2] := 52;
// more of the same ..........

or sample 2

procedure TForm1.Button1Click(Sender: TObject);
var
ArrayOfInteger: array[0..48] of Integer;
str : string;
Data: Textfile;
I,C: integer;
begin
Reset(Data, 'test2.txt');
I := 50;
C := 0;
Canvas.Textout(30, i, str);
ArrayOfInteger[c] := StrToInt(Str);
while not eof(data) do
begin
Inc(C);
I := I + 15;
Canvas.Textout(30, I, str);
ArrayOfInteger[C] := StrToInt(Str);
end;
CloseFile(Data);
end;

0
delphi3
• 3
• 3
• 2
• +1
1 Solution

Commented:
const
ArrayOfInteger: array[0..48] of Integer =
(61, 39, 52, ....);
0

Software DeveloperCommented:
Q title is 'load array of integers'. Do you want to load them from a file or what?

If this array has a fixed count of elements and you are able to declare it as a global variable, you may specify values when declaring an array. It looks smth like this:

var
Form1 : TForm1;
arr : array [0..3] of integer = (0, 234, 23, 99);

If this array is a local variable (not a global), you can't specify values in this way.

If you want to load/save these values from/to file, here's a small example (there's also Memo1 that is used to input/show integers):

procedure TForm1.SaveBtnClick(Sender: TObject);
var
n, err : integer;
arr : array of integer;
f : file;
begin
SetLength(arr, Memo1.Lines.Count);
for n := 0 to high(arr) do Val(Memo1.Lines.Strings[n], arr[n], err);
// saving array
AssignFile(f, 'c:\int_arr.dat');
Rewrite(f, sizeof(integer));
BlockWrite(f, arr[0], length(arr));
CloseFile(f);
// end of saving array
end;

var
arr : array of integer;
f : file;
n : integer;
begin
AssignFile(f, 'c:\int_arr.dat');
Reset(f, sizeof(integer));
SetLength(arr, filesize(f));
CloseFile(f);
Memo1.Clear;
for n := 0 to high(arr) do Memo1.Lines.Append(format('%d', [arr[n]]));
end;
0

Commented:
You can also use TFileStream

procedure TForm1.Button1Click(Sender: TObject);
var
fs: TFileStream;
tab: array[0..100] of integer;
i: integer;
begin
for i := 0 to 100 do
tab[i] := i;
fs := TFileStream.Create('val.bin',fmCreate);
fs.WriteBuffer(tab,Sizeof(tab));
fs.Free;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
fs: TFileStream;
tab: array[0..100] of integer;
i: integer;
begin
fs.Free;
for i := 0 to 100 do
end;
0

Author Commented:
Hi

I was looking for an optional way of loading the array so that I did not have to write 48 times the
'ArrayOfInteger[i] :='
as in my example
ArrayOfInteger[0] := 61;
ArrayOfInteger[1] := 39;
ArrayOfInteger[2] := 52;

or not use a file to store the data and have to then read the file?

Is there anyway to load the array by a string sequence showing the string as in

var
Str : String;

begin

Str := '61 39 52      '
......

Parse the Str  so as to gather up the numbers that are between each of  spaces? I do not like the looks of either of the methods that I originally showed in my opening Q.

Delphi3

0

Software DeveloperCommented:
You may create your own procedure that would fill an array with values you want. Then just write those values in [ and ]. Here's a small example (use 0..your_value instead of 0..3)

procedure fill (var arr : array of integer; values : array of integer);
var
n : integer;
begin
for n := 0 to high(values) do arr[n] := values[n];
end;

procedure TForm1.FormCreate(Sender: TObject);
var
ArrayOfInt : array [0..3] of integer;
begin
fill(ArrayOfInt, [1234, 234, 23, 94]);
ShowMessageFmt('%d', [ArrayOfInt[0]]);
ShowMessageFmt('%d', [ArrayOfInt[1]]);
ShowMessageFmt('%d', [ArrayOfInt[2]]);
ShowMessageFmt('%d', [ArrayOfInt[3]]);
end;
0

Commented:
Maybe this

const
tab1: array[0..3] of integer=(11,22,33,44);
0

Software DeveloperCommented:
If using typed constants (as mokule does), $J directive must be used if you want to change these values later. Looks something like this: {$J+} // this turns on possibility to write to a typed constants
const
tab1 : array[0..3] of integer = (11,22,33,44);
begin
tab1[0] := 187;
ShowMessageFmt('%d', [tab1[0]]);
ShowMessageFmt('%d', [tab1[1]]);
0

Author Commented:

Thanks to all,
I need time to evaluate these  answers and award points and grade.

D3
0

Author Commented:
Thanks to all of you for your responses.

D3
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.