• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 243
  • Last Modified:

Loading a multi-dimensional static array

This feels like poorly written written code, is there a better way?
[ D6, WinXPHome ]

  ButtonArray: [1..7,1..5] of String;

  ButtonArray[1][1] := 'some text';
  ButtonArray[1][2] := 'some text';
  ButtonArray[1][3] := 'some text';
  ButtonArray[1][4] := 'some text';
  ButtonArray[1][5] := 'some text';

  ButtonArray[2][1] := 'some text';
  ButtonArray[2][2] := 'some text';
  ButtonArray[2][3] := 'some text';
  ButtonArray[2][4] := 'some text';
  ButtonArray[2][5] := 'some text';

 I know I can load a single dimension array like this
    ButtonArray: array [1..5] of String = ('some text','some text','some text','some text','some text');

Is there a better way to load a multi-dimensional array?

  • 3
  • 3
2 Solutions

  buttonarray: array[1..maxi,1..maxj] of string;

for i:=1 to maxi do
  for j:=1 to maxj do
    buttonarray[i,j]:='some text';

doh:I'm not thinking

of course you want sometext to be different ecvery time, i would use a resource file then


the resource file would be numbered from 1.
d32coderAuthor Commented:
I'd rather not have the data in an external file.   It's smaller code but not really what I'm looking for.   I was hoping I could put multiple strings into a single assignment as in the one dimensional array assignment in my example.
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

d32coderAuthor Commented:
Now I'm not thinking, you said a resource, not a data file.  
That's probably a good idea.  
Let's see what other experts have to say.

It depends what kind of strings you have.  In a program I'm writing at the moment, I construct the bit you put into brackets

('sometext','sometext' etc)

into an Excel file and do an Include at the declaration for the initial values, so it looks like this

   ButtonArray: array [1..5] of String = ('
{$INCLUDE progdata.csv}

...so that's another way.

If you are writing a multi-language program then you will want all of the language info in separate files.

Wim ten BrinkSelf-employed developerCommented:
  ButtonArray: array [1..5, 1..5] of String = (
    ('some text1A','some text2A','some text3A','some text4A','some text5A'),
    ('some text1B','some text2B','some text3B','some text4B','some text5B'),
    ('some text1C','some text2C','some text3C','some text4C','some text5C'),
    ('some text1D','some text2D','some text3D','some text4D','some text5D'),
    ('some text1E','some text2E','some text3E','some text4E','some text5E')

However, if you need to fill it with data from a file or other source then you have no other option than to fill it field by field. (for I... for J... ButtonArray[I,J] :=...)
If it is a file of shortstrings then it is possible to write the whole array to file as a single block of data, though. Something like:

  TButtonArray: array [1..5, 1..5] of shortstring;
  ButtonArray: TButtonArray;
  ButtonFile: file of TButtonArray;
  AssignFile(ButtonFile, 'Whatever.dat');
  Read(ButtonFile, ButtonArray);

but this ONLY works for simple datatypes like integers, doubles, booleans and shortstrings. Any type that is pointer-based (normal strings, PChar's, classes) cannot be written to file like this. Furthermore, this code is incompatible with Delphi 8.
d32coderAuthor Commented:
I split the points.  
Alex had the solution I was looking for; nested parentheses.  
I will use Moorhouse's idea on loading it into a resource file for this project.  
Thanks to both of you.

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.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now