dimentional array

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
hacktorious
Posts: 15
Joined: 22 February 2007, 12:05 PM

dimentional array

Post by hacktorious »

Learning new languages is always fun....lol

I am having an issue with a two-dimensional array. I have the following code:

'array for intervals params of OutputCaptureEX
Dim intervals (1 To 2, 1 To 2, 1 To 2, 1 To 2, 1 To 2) As UnsignedInteger

How do I go about populating each individual index within the internal arrays? I tried the following, but get an error.

intervals(0,1) = 37500
intervals(0,2) = 38250
etc......

Here is the error:

Error: array "intervals" requires 5 index expressions

It seems I am being forced to put in a single value for each index of the intervals array, instead of being able to put values into the internal arrays indices.

Thanks.
Last edited by hacktorious on 09 March 2007, 21:14 PM, edited 1 time in total.
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: dimentional array

Post by dkinzer »

hacktorious wrote:I am having an issue with a two-dimensional array.

Code: Select all

Dim intervals (1 To 2, 1 To 2, 1 To 2, 1 To 2, 1 To 2) As UnsignedInteger 
You've defined a 5-dimension array which requires 5 indices to access an individual element. If you wanted a 2-dimension array, the definitions would look something like this:

Code: Select all

Dim intervals (1 To 2, 1 To 2) As UnsignedInteger 
This particular definition creates a 2x2 array. Is that what you wanted?
- Don Kinzer
hacktorious
Posts: 15
Joined: 22 February 2007, 12:05 PM

Post by hacktorious »

Oops..... No, I do want a 5 dimensional array. Two was a typo.

I need a 5 dimensional array, which contains five arrays. Each array within the 5-dimensional array must hold two values.

Dim intervals (1 To 2, 1 To 2, 1 To 2, 1 To 2, 1 To 2) As UnsignedInteger

How can I put values into the internal arrays?


Hmmmm, now that I think about it maybe a two-dimensional will work, but like this:
Dim intervals(1 To 5, 1 To 5) , I have to think about it more....getting sleepy...lol

OK, let's suppose the two-dimensional will work, how can I fill the internal arrays indices with some UnsignedIntegers?
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

hacktorious wrote:OK, let's suppose the two-dimensional will work, how can I fill the internal arrays indices with some UnsignedIntegers?
You simply assign values to each element of the array. In any event, you must supply an index for each dimension of the array - two if it is 2-dimension, three if it is 3-dimension, etc. The compiler error that you mentioned in the original post was informing you that you hadn't supplied the correct number of indices.

You may want to read section 3.17 of the ZBasic Reference Manual. It describes how arrays are laid out in memory.
- Don Kinzer
spamiam
Posts: 739
Joined: 13 November 2005, 6:39 AM

Post by spamiam »

hacktorious wrote:How can I put values into the internal arrays?

Well, you could do it by nested for-next loops

Code: Select all

Dim Intervals (1 To 2, 1 To 2, 1 To 2, 1 To 2, 1 To 2) As UnsignedInteger 
Dim Index_A as byte
Dim Index_B as Byte
Dim Index_C as byte
Dim Index_D as byte
Dim Index_E as byte

For Index_A = 1 to 2
     For Index_B = 1 to 2
          For Index_C = 1 to 2
               For Index_D = 1 to 2
                    For Index_E = 1 to 2
                         Intervals(Index_A, Index_B, Index_C, Index_D, Index_E) = 0
                    Next
               Next
          Next
     Next
Next
This assumes that you want to initialize them all the same. If they need individual unique assignments, then you need to do that manually. If there were a formula that you could use to calculate the initial value, then that could be done using the code above, but instead of setting the cell to a constant, there would be a calculation involved.

Hmmmm, now that I think about it maybe a two-dimensional will work, but like this:
Dim intervals(1 To 5, 1 To 5) , I have to think about it more....getting sleepy...lol

OK, let's suppose the two-dimensional will work, how can I fill the internal arrays indices with some UnsignedIntegers?
You would do it the same way as above, just fewer indices would be used.
Post Reply