11 Replies Latest reply on Nov 17, 2007 7:51 AM by Rothrock

    Multidimensional Arrays Math

      I'm having a problem with Multidimensional arrays that I've been thrashing round with for the past few days. I can load arrays of the type array[level][segment] i can see what's in the array using trace() but I don't seem to be able to operate on the elements.

      For example a particular array[level] may have some content that looks like array[level] = [4,3,5,-2, 5, -6].

      I can trace individual elements i.e. trace (array[level][2] );; output = 5 etc but i don't seem to be able to apply simple math to the elements i.e.

      if(array[level[2] > 0){do something like array[level[2] =array[level[2] *-1}

      I keep getting NaN replies even when i use Number(array[level[2] )

      I'm at a loss. Any help pointing me in the right direction would be truly appreciated.
        • 1. Re: Multidimensional Arrays Math
          Greg Dove Level 4
          This might help to understand.
          • 2. Re: Multidimensional Arrays Math
            PolyH Level 1

            Thanks for the quick reply - let me review it and see if i understand.
            • 3. Re: Multidimensional Arrays Math
              Greg Dove Level 4
              Just to clarify further,

              to access [1,2,3] in the test array, its test[0].

              to change the 1 value of that array, its test[0][0] = 5; or whatever
              • 4. Re: Multidimensional Arrays Math
                joeflashTO Level 1
                It's sometimes easier to type a multidimensional array with more whitespace, to make things clear:

                • 5. Re: Multidimensional Arrays Math
                  PolyH Level 1
                  GWD and JoeFlashTO

                  Thanks for your replies. I'm pretty well OK with identifying elements in a multi array etc but it seems like i can't get the system to behave 'logically' - I know - I know!!. If i use GWD's example where the array is defined things work. However, I'm loading arrays dynamically from calculations, i can view the array elements but what I still can not do is 'if statements' on the array elements i.e. I can do the following on GWD's array but not on my dynamically loaded arrays. I did try and test simply by loading a dummy array dynamically and the 'if statements' still don't seem to work!!

                  if (test[1][1] > 0) {test[1][1] = test[1][1] *-1}
                  • 6. Re: Multidimensional Arrays Math
                    Rothrock Level 5
                    Sometimes dynamically loaded arrays don't actually contain numbers -- even if they look like numbers and should be numbers. So you should look into casting:


                    Or most likely parseInt() and/or parseFloat(). Finally isNaN() could be your good friend as well!
                    • 7. Re: Multidimensional Arrays Math
                      Greg Dove Level 4
                      My thoughts exactly.

                      You might have trailing spaces.

                      trace(Number("3 ")); //fails with trailing spaces
                      trace(Number(" 3"));

                      trace(parseFloat("3 "))
                      trace(parseInt("3 "));

                      trace(parseFloat(" 3 "))
                      trace(parseInt(" 3 "));
                      • 8. Re: Multidimensional Arrays Math
                        PolyH Level 1
                        Rothrock and GWD

                        Thanks again - I did try the Numbers option earlier with no luck - I'll go back through and check my typing - I was hoping it was something I could push the blame on beside my own typing errors.
                        • 9. Re: Multidimensional Arrays Math
                          Rothrock Level 5
                          Well I did notice in your earlier post that at first you had the correct form:


                          but later you switched (consistently) to an incorrect form


                          But that should generate errors, so I figured it was just sloppy forum retyping not the actual state of your code.

                          Also, casting won't always work. I don't know why. Do use the parseInt() and parseFloat(). It might even be a good idea to do this in the part where you load your dynamic info.
                          • 10. Re: Multidimensional Arrays Math
                            PolyH Level 1
                            Thanks guys

                            My problem is solved! First Rothrock yes the format was just sloppy retyping and I will in future use parseInt() etc. Needless to say the problem was mine. I was trying to input numbers into a 2D array using the format

                            array[level][segment] = [24];

                            Which means the actual number is in a 3D array at array[level][segment][0] for example

                            as opposed to array[level][segment] = 24;

                            Not too swift! In my defense I'm used to loading and unloading using push() and pop(). FYI I think what triggered my revelation was the comment that sometime NaN is your friend - that set me searching.

                            Thanks again.
                            • 11. Re: Multidimensional Arrays Math
                              Rothrock Level 5
                              Glad you worked that out. Generally it is always a good idea to cut and paste the relevant bits of code that way we might have caught this earlier.

                              Plash does have push(), pop(), shift(), etc.