Home AMX User Forum AMXForums Archive Threads Tips and Tricks

Setting all values in an INTEGER array

GasHedGasHed Junior MemberPosts: 31
On the bottom of page 134 of the AMX Programmer book, it seems to indicate that you can set multiple values of an INTEGER array in one line using string syntax. However, in my little sample program below:

DEFINE_VARIABLE
INTEGER array[10]

DEFINE_START
array = "12,13,4"

I get an Dimension mismatch: [1] vs. [0] error on the DEFINE_START line. If I switch the array to a CHAR array it works (of course). Am I missing something obvious?

Patrick Clemins
TECHteriors LLC
Milwaukee, WI

Comments

  • jjamesjjames Just another dude Posts: 2,906
    Why not just declare it in the DEFINE_VARIABLE section?

    INTEGER nARRAY[3] = {12,13,4}

    or an "unsized array"

    INTEGER nARRAY[] = {12,15,65,23,66,43,99}

    Even if you declare it a size of 10, you can still only declare the first three.

    INTEGER nARRAY[10] = {12,13,4} would look like this in the diagnostics view
    NAME            FILE            LENGTH               VALUE          DISPLAY
    
    NARRAY[10]  | yourfile.axs |     10        | 12,13,4,0,0,0,0,0,0,0 | INTEGER
    
  • Marc ScheibeinMarc Scheibein Junior Member Posts: 681
    GasHed wrote:
    On the bottom of page 134 of the AMX Programmer book, it seems to indicate that you can set multiple values of an INTEGER array in one line using string syntax. However, in my little sample program below:

    DEFINE_VARIABLE
    INTEGER array[10]

    DEFINE_START
    array = "12,13,4"

    I get an Dimension mismatch: [1] vs. [0] error on the DEFINE_START line. If I switch the array to a CHAR array it works (of course). Am I missing something obvious?

    Patrick Clemins
    TECHteriors LLC
    Milwaukee, WI

    In this case this will not work because a string operation is used on an Integer array.....

    If necessary to do this in DEFINE_START, every single element of the array must be filled separately....
    OR
    if the values are <256, use the CHAR type array (I use this "workaround" in several places ;)). But in this way, if you want to write an integer value into the array, it is necessary to type_cast the integer to prevent compiler warnings.
    DEFINE_VARIABLE
    INTEGER MyValue = 200
    CHAR Arry[10]
    
    DEFINE_START
    array[2] = TYPE_CAST(MyInteger)
    // trying to put an Integer type variable into a char will give a warning, although the content of MyValue is <256
    
  • GasHed wrote:
    On the bottom of page 134 of the AMX Programmer book, it seems to indicate that you can set multiple values of an INTEGER array in one line using string syntax. However, in my little sample program below:

    DEFINE_VARIABLE
    INTEGER array[10]

    DEFINE_START
    array = "12,13,4"

    I get an Dimension mismatch: [1] vs. [0] error on the DEFINE_START line. If I switch the array to a CHAR array it works (of course). Am I missing something obvious?

    Patrick Clemins
    TECHteriors LLC
    Milwaukee, WI

    Welcome aboard Patrick.

    If your want to initialize the integer array values in Define_Start, the correct method is as follows as explained correctly by Marc. Here is an example.
    DEFINE_VARIABLE
    INTEGER array[10]
    
    DEFINE_START
    array[1] = 12
    array[2] = 13
    array[3] = 4
    

    I usually use the method Jerimiah suggested in the Define_Variable section.
  • GasHedGasHed Junior Member Posts: 31
    Figured

    I don't think I can define the values for the array in DEFINE_VARIABLES because the array is actually a data member of an array of structures, so my code in DEFINE_START really currently looks like:

    znZONE[1].array[1] = 1
    znZONE[1].array[2] = 2

    I left that out to simplify the original question... Marc gave me the answer I was looking for... I can't do that. <256 is fine, and I kinda wanted to hear that others did the CHAR array workaround before I did.

    Thanks for the input!

    Patrick Clemins
    TECHteriors, LLC
Sign In or Register to comment.