Solved

Posted on 2005-04-28
449 Views
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
Question by:delphi3

LVL 6

Accepted Solution

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

LVL 11

Expert Comment

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

LVL 17

Expert Comment

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

LVL 4

Author Comment

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

LVL 11

Expert Comment

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

LVL 17

Expert Comment

Maybe this

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

LVL 11

Expert Comment

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

LVL 4

Author Comment

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

D3
0

LVL 4

Author Comment

Thanks to all of you for your responses.

D3
0

## Featured Post

### 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…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…