Dynamic Multidimensional Arrays

Post

Posted
Rating:
#1 (In Topic #1446)
Regular
tailkinker is in the usergroup ‘Regular’
 Hi,

Is there a way to get dynamic multidimensional arrays in Gambas, and how to use them?

I started this program in VB years ago, and sometimes it takes as many as 5 dimensions to account for recipe variants depending on what I choose to update.  Let's say I'm building a recipe for sponge cakes.  Even though they all have 11 or  12 main ingredients, variations may have 18 or more (example a lemon sponge cake has 12 ingredients, chocolate 15, fruit cake almost 20).  And then, recipes for dinner rolls (6 ingredients, cornbread 8), or casseroles a different number of ingredients altogether.  

So, in my vb program, I might do something like redim preserve nutrients.carbs(d1, d2, d3) as integer to change whichever dimension needs increasing at runtime.

As with control arrays (which I read about in the tip of the day - prompting this set of questions) - I have a suspicion I got a bit spoiled with some things in VB that I can't find in other languages. I chose to dive into Gambas, btw because it has forms and a grid control that works in Linux.

Lucille
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
Perhaps you want a Struct https://gambaswiki.org/wiki/lang/structdecl

( A request: Lucille, please don't put multiple questions in a post. Some people will answer one and others the other. Trying to figure out what is happening later on is difficult if not impossible.)

b

Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
Multi-mensional Arrays
In Gambas a multi-dimensional array can be created. But in my opinion they are a pain. Notwithstanding, you can have an array of arrays, which are easier to use.

Multi-dimensional

Code (gambas)

  1. Dim Crazy as Variant[6,8,2]
Note it must be dimensioned explicitly.

Array of Arrays

Code (gambas)

  1. Dim MyArray as Variant[][]
It doesn't need fixed bounds.
Now MyArray[3][1] will return the second item in the 4th "row" (for want of a better word) of the array.

b

Online now: No Back to the top

Post

Posted
Rating:
#4
Regular
tailkinker is in the usergroup ‘Regular’

thatbruce said


Bruce,

Sorry about that.

I edited my post so types is in its own discussion.  Sometimes it seems like I ask a ton of questions, so I wanted a smaller presence.

Lucille


( A request: Lucille, please don't put multiple questions in a post. Some people will answer one and others the other. Trying to figure out what is happening later on is difficult if not impossible.)

b
Online now: No Back to the top

Post

Posted
Rating:
#5
Regular
tailkinker is in the usergroup ‘Regular’
 Bruce,

I will see if array of arrays do what I need them to do.  Was considering collections or using files for each dimension, but that would make a mess out of my very elegant for/next loops (Yes I am proud of that tIny snippet of code in VB).

For me multi-dimensional arrays are like the innards of a Swiss watch or a manual transmission - hard to get used to. Once I got them figured out though, dynamic ones became indispensable.

Lucille
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Guru
cogier is in the usergroup ‘Guru’
The way I would do this is with a CSV file. Each line of ingredients can be separated by a comma and be as long, or short, as required. This means that you only need to process 1 line at a time, which can be Split into an array that will be as large, or small, as needed. Have a look at the 'List.csv' in the program attached program.

<IMG src="https://www.cogier.com/gambas/Ingedients.png"> </IMG>

Attachment
Online now: No Back to the top
1 guest and 0 members have just viewed this.