JohnLM 23 Posted January 18 (edited) Is it possible to set variable size multi-dimentional arrays dynamically at runtime? Or, do I have to be clever about it in another way? My usual ways to create m-a's are the typical triangle shape, not irregularly shaped. I want to create the irregularly shaped, like the examples listed below. I have been playing around with run-time dynamic arrays via SetLength() and then realized I can't create them dynamically with different size Columns. I have been using for/next and repeat/until loops of various kinds to attempt to do the irregularly shapped arrays but when I come to create a new Row and reset the C to 1, the data is wiped or I get strange behavior, which leads me to realize this route is not possible, and that I have to do something really clever, maybe to simulate multi-dim dynamic arrays. I have a string list that I want to parse as values--text for strings, numbers for integers. The multi-dim array can look something like the following examples below: where s=string, n=number, r=row, c=column So, using ex 1, as I parse my string list, (a single column list), R is set to 1 for row 1, and C is set to 1, and as I parse through the list, I inc(C) for row 1, thus, ( 1 [n, n, n] ) When I create row 2, I inc(R), and is now row 2, thus ( 2 [n, n, n, n, n] ), . . ., and so on, for row 3, . . . etc. ex 1 ==== R | C - - - > ----------------- 1 [n, n, n] 2 [n, n, n, n, n] 3 [n, n] 4 [n, n, n, n] 5 [n] ex 2 ==== R | C - - - > ----------------------------------------- 1 [n, n, n, n, n, n, n, n, n, n, n, n, n] 2 [s, s, s] 3 [n, n] 4 [s, s, s, s, s, s, s, s] ex 3 ==== R | C - - - > -------------------------------------------------------------- 1 [s, s] 2 [s] 3 [s, s, s, s, s] 4 [s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s] 5 [s, s, s] 6 [n, n, n, n, n, n, n] Edited January 18 by JohnLM typo Share this post Link to post
JohnLM 23 Posted January 18 note, I am not asking for someone to write the code for me. If it is possible, then I am asking for pointer(s) to which function does this. I will write the code mentioned for the 3 examples listed in my initial post above. Share this post Link to post
PeterBelow 240 Posted January 18 in a dynamic 2D array ( a type like TMyArray = array of array of integer) you can set the number of rows with a SetLength call on the array variable and, for each row, set the number of columns with a SetLength(arrayvar, rowindex) call. That should cover your requirements. To reduce memory fragmentation you should avoid multiple SetLength calls on the same array row or the array itself, though. Determine the number of rows/elements beforehand, if possible. 1 Share this post Link to post
Uwe Raabe 2083 Posted January 18 Just now, PeterBelow said: for each row, set the number of columns with a SetLength(arrayvar, rowindex) call. This doesn't seem right. The call should be SetLength(arrayvar[rowindex], columnsize), shouldn't it? Share this post Link to post
PeterBelow 240 Posted January 18 Just now, Uwe Raabe said: This doesn't seem right. The call should be SetLength(arrayvar[rowindex], columnsize), shouldn't it? Yes, my bad. I'm getting old ... 🙂 Share this post Link to post
JohnLM 23 Posted January 18 (edited) I don't know why I am having so much trouble with something that seems too easy to accomplish, yet I am. I just got up and saw these posts, and decided to sketch out an illustration of what I am trying to accomplish. Please forgive me if I seem too slow at all this. I am a very slow person these days in my old age. Okay. I have simplified this in an Excel sheet to help illustrate the basic process. * in the finished product, the "irregularly" shaped array should look like what's shown in blue in fig 4. I believe the main part of my problem is that I am doing this setlength() inside the parser routine and that I have to build the irregularly shaped array afterwards once I have the (R,C) criteria in fig 4. Also, I found this link that may help: https://docwiki.embarcadero.com/RADStudio/Athens/en/Structured_Types_(Delphi)#Multidimensional_Dynamic_Arrays PS: I've just updated the grid in fig 4 to show that they are individual elements in the array, not one string. Edited January 18 by JohnLM uploaded new image Share this post Link to post
JohnLM 23 Posted January 18 9 hours ago, PeterBelow said: Determine the number of rows/elements beforehand, if possible. @PeterBelow -- that is my idea too. I just realized now, that you had said this earlier--I missed that part. But yes, I believe that is what I have to do, thanks. Share this post Link to post
JohnLM 23 Posted January 19 Update. . . Okay, I have finally figured it out. Am running to work but may post an update with a solution or explanation later. . . Share this post Link to post
pmcgee 23 Posted January 22 (edited) Going off your picture ... I'd say 'Yes', I think you are over-complicating things. But it is normal for us to come at a problem, trying out various ideas ... getting too close to the problem, going down dead ends, and then finally making some realisations that simplify the whole thing. Sometimes over and over and over - until hopefully achieving a simple and elegant solution. Consider just making a StringList. {$APPTYPE CONSOLE} program Project1; uses System.Classes, System.Types; type TState = (int, alpha); const Digits : set of char = ['0','1','2','3','4','5','6','7','8','9']; function Parse( s:String ) : TStringList; begin Result := TStringList.Create; var state : TState := int; var temp : string := ''; for var ch in s do case state of int: if ch in Digits then temp := temp+ch else begin if temp <> '' then result.Add(temp); temp := ch; state := alpha; end; alpha: if not (ch in Digits) then temp := temp+ch else begin if temp <> '' then result.Add(temp); temp := ch; state := int; end; end; if temp <> '' then result.Add(temp); end; begin var s:TStringList := Parse('12abcd345ghi6kl7mnopq890'); writeln( s.Text ); s.Free; readln; end. (Actually, with a StringList, all you'd really have to do is insert your chosen delimiter between runs of Alphas and Ints. But I wanted to model a bit more general approach.) Edited January 22 by pmcgee Share this post Link to post