Home AMX User Forum NetLinx Studio

Multi-dimension array definition?

It appears that I cannot create my own "offset" index for multi-dimension arrays. This works, and I get the values from the multi-dim array on the button push:
DEFINE_CONSTANT
integer lvl_mic_preval[][] = { 
{1,3,0},
{2,4,0},
{3,5,0}
}

DEFINE_EVENT
button_event[tp,121] 
{
    push: 
    {
	send_string debug,"'DEBUG: preset_val: 1=',itoa(lvl_mic_preval[button.input.channel - 120][1]),'; 2=',itoa(lvl_mic_preval[button.input.channel - 120][2]),'; 3=',itoa(lvl_mic_preval[button.input.channel - 120][3])"
    }
}


...and the following compiles OK, but I do not get the values from the multi-dim array:
DEFINE_CONSTANT
integer lvl_mic_preval[][] = { 
121,{1,3,0},
122,{2,4,0},
123,{3,5,0}
}

DEFINE_EVENT
button_event[tp,121] 
{
    push: 
    {
	send_string debug,"'DEBUG: preset_val: 1=',itoa(lvl_mic_preval[button.input.channel][1]),'; 2=',itoa(lvl_mic_preval[button.input.channel][2]),'; 3=',itoa(lvl_mic_preval[button.input.channel][3])"
    }
}

So It appears that I cannot specify what the value of that first dimension is, I just have to live with an auto-index starting at 1?

Is there a way to get an offset index for the first dimension of a muti-dim array?

Thanks,

Comments

  • mpullinmpullin Posts: 949
    I think I know what you're asking, and the answer is no. Arrays have to start at index 1.
  • jweatherjweather Posts: 320
    You are correct, dimensions always start at 1. For a simpler solution to the problem you're showing, try something like
    DEFINE_CONSTANT
    
    integer btnARRAY[] = {121, 122, 123}
    integer lvl_mic_preval[][] = { 
    {1,3,0}, // 121
    {2,4,0}, // 122
    {3,5,0}  // 123
    }
    
    DEFINE_EVENT
    button_event[tp,btnARRAY] 
    {
        push: 
        {
    	send_string debug,"'DEBUG: preset_val: 1=',itoa(lvl_mic_preval[GET_LAST(btnARRAY)][1]),'; 2=',itoa(lvl_mic_preval[GET_LAST(btnARRAY)][2]),'; 3=',itoa(lvl_mic_preval[GET_LAST(btnARRAY)][3])"
        }
    }
    

    Basically, GET_LAST and the btnARRAY give you your mapping from (121,122,123...) back to (1, 2, 3...) which are your array indices.

    Untested, please excuse any think-os.

    Jeremy
  • fogled@mizzoufogled@mizzou Posts: 549
    jweather wrote:
    Basically, GET_LAST and the btnARRAY give you your mapping from (121,122,123...) back to (1, 2, 3...) which are your array indices.

    Oh... the light bulb goes on...

    Thank you very much!
  • GSLogicGSLogic Posts: 562
    It appears that I cannot create my own "offset" index for multi-dimension arrays.
    You can create multi dimensional arrays, you can even use them in Structures:
      integer multiD[2][3][3];
    
    //multiD[house][floor][room]
      multiD[1][1][1] = 11;
      multiD[1][2][2] = 12;
      multiD[1][3][3] = 13;
    	
      multiD[2][1][1] = 21;
      multiD[2][2][2] = 22;
      multiD[2][3][3] = 23;
    
  • NMarkRobertsNMarkRoberts Posts: 455
    Use a wrapper function

    If you always refer to the array via wrapper functions, you can code the easy-to-get-wrong bit(s) just once and use them with confidence thereafter. Think object oriented.
  • DHawthorneDHawthorne Posts: 4,584
    I prefer to make a CONSTANT list defining my array elements, so instead of having to reference the numbers, I can have readable indexes.
  • fogled@mizzoufogled@mizzou Posts: 549
    DHawthorne wrote:
    I prefer to make a CONSTANT list defining my array elements, so instead of having to reference the numbers, I can have readable indexes.

    Any possibility you could expound on that just a bit? I'm not sure I understand what you mean. I'm trying to define a constant, and I can't get anything but numbers - not of my own choosing - for indexes.
  • alexanboalexanbo Posts: 282
    What I think he is talking about is say the first index of the array represents rooms.

    In your constants you'd do something like

    Living = 1
    Dining = 2
    Den = 3

    Then when referencing the array you can write
    [Living][blah]

    If you got real ambitious you might be able to use Hash Maps and such in Java on the Duet side.
  • DHawthorneDHawthorne Posts: 4,584
    alexanbo wrote:
    What I think he is talking about is say the first index of the array represents rooms.

    In your constants you'd do something like

    Living = 1
    Dining = 2
    Den = 3

    Then when referencing the array you can write
    [Living][blah]

    If you got real ambitious you might be able to use Hash Maps and such in Java on the Duet side.
    Exactly :) Sorry if I wasn't clear.
  • fogled@mizzoufogled@mizzou Posts: 549
    alexanbo wrote:
    Living = 1
    Dining = 2
    Den = 3

    Thanks everyone! I think I've got a handle on this now. On to the next question...
Sign In or Register to comment.