Jump to content
Sign in to follow this  
recursiveElk

Inline assignment to Multi Dimensional Dynamic Arrays

Recommended Posts

Hi, would it be possible to implement inline assignment similar to how static arrays are handled?

 

Currently i can do the following:

var myDynamicSingleArray : Array of String;
  myDynamicSingleArray:= ['Drinks','Boys','Girls'];

var myStaticSingleArray : Array[0..2] of String;
  myStaticSingleArray:= ['Drinks','Boys','Girls'];
And:

var myStaticMultiArray : Array [0..3] of Array [0..2] of String;
  myStaticMultiArray := [['Drinks','Boys','Girls'],['Milk','5','6'],['Water','3','2'],['Juice','2','1']];
But not:

var myDynamicMultiArray : Array of Array of String;
  myDynamicMultiArray := [['Drinks','Boys','Girls'],['Milk','5','6'],['Water','3','2'],['Juice','2','1']];
Syntax Error: Incompatible types: Cannot assign "array [0..3] of array [0..2] of String" to "array of array of String" [line: 228, column: 19, file: Form1]

Share this post


Link to post
Share on other sites

Also of note:

type
TStrArray : Array of String;

 

Valid:

var myMultiArray : Array of TStrArray;
var temparray : TStrArray;
temparray := ['Drinks','Boys','Girls'];
myMultiArray.add(temparray);
temparray := ['Milk','5','6'];
myMultiArray.add(temparray);
temparray := ['Water','3','2'];
myMultiArray.add(temparray);
temparray := ['Juice','2','1'];

 

Not Valid:

var myMultiArray : Array of TStrArray;
var temparray : TStrArray;
myMultiArray.add(['Drinks','Boys','Girls']);
myMultiArray.add(['Milk','5','6']);
myMultiArray.add(['Water','3','2']);

Share this post


Link to post
Share on other sites

In delphi constants have to be known at compile time, but dynamic arrays are allocated at runtime.

 

 

You can construct dynamic array in run-time either directly:

(your "Valid" example)

 

type    
  TStrArray = Array of String;

var myMultiArray : Array of TStrArray;
   temparray : TStrArray;
begin
  temparray := ['Drinks','Boys','Girls'];
  myMultiArray.add(temparray);
  ...
end;
or you can declare dynamic array of fixed array

type
   TStrFixArray = Array[0..2] of String;


var myMultiArray : Array of TStrFixArray ;
begin
  myMultiArray := [['Drinks','Boys','Girls'],['Milk','5','6'],['Water','3','2']];  
// or step by step....
  myMultiArray.add(['Juice','2','1']);
  myMultiArray.add(['Slivovice','6','9']);
  
end;

Share this post


Link to post
Share on other sites

I see why what i was asking is somewhat impossible then. It would be great if there was a function out there that parsed say a string equivalent of a multi array (eg '[['test1','test2','test3'],['test4','test5','test6']]') and then spat out a dynamic array without the laborious method of several add statements in a hard coded fashion.

 

Perhaps i will write one and upload it here if i find the time.

Share this post


Link to post
Share on other sites

you can use of course a trick, but you have to beware of data types

 

type    
TStrArray = Array of String;

var myMultiArray : Array of TStrArray;
begin
...
asm
var m = @myMultiArray ;
m = [['Drinks','Boys','Girls'],['Milk','5','6'],['Water','3','2']];
@myMultiArray = m;
end;
...
asm
var m = @myMultiArray ;
m.push(['Saratice','8','17']);
@myMultiArray = m;
end;


end;

Share this post


Link to post
Share on other sites
var myDynamicMultiArray : array of array[0..2] of string =  [['Drinks','Boys','Girls'],['Milk','5','6'],['Water','3','2'],['Juice','2','1']];

myDynamicMultiArray.add([["A","B","C"]]);
myDynamicMultiArray.add([["D","",""]]);
myDynamicMultiArray.add([["G","","J"]]);
myDynamicMultiArray.Delete( myDynamicMultiArray.indexOf( ["Milk","5","6"] ) );

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×