Shop OBEX P1 Docs P2 Docs Learn Events
multi dimentional arrays? — Parallax Forums

multi dimentional arrays?

RottenJalapenoRottenJalapeno Posts: 27
edited 2008-09-30 08:13 in Propeller 1
I was wondering if it is possible to make multidimentional arrays for variables?
What I am trying to do is have a list of 50 names (strings, 16 chars long), and have·information associated with each name,·that I can modify, and also index.
There might be an easier way to do this but I would think the easiest way would to through all the data into an array.
Any suggestions would be nice,
Thank you parallax, and all you parallaxers out there that helped me on several occasions
Jeremy

Comments

  • hippyhippy Posts: 1,981
    edited 2008-09-29 12:10
    There are no multi-dimensional arrays as such but it is possible to 'fake them'.

    Your 50 x 16 array is the same as a single array of 800 and you can replace all myArray[noparse][[/noparse]x,y] with myArray[noparse][[/noparse] x * 16 + y ]. You can also use helper methods to set and get array items ...

    VAR
      byte myArray[noparse][[/noparse]800]
    
    PUB Main | i
    
      i := Get_myArray(10,12)  ' myArray[noparse][[/noparse]1,1] := myArray[noparse][[/noparse]10,12]
      Set_myArray(1,1,i)
    
    PRI Get_myArray(x,y)
      return myAray[noparse][[/noparse] x * 50 + y ]
    
    PRI Set_myArray(x,y,value)
      myArray[noparse][[/noparse] x * 50 + y ] := value
    
    
    



    Not perfect but possible workrounds.
  • Ken PetersonKen Peterson Posts: 806
    edited 2008-09-29 13:27
    You can also use an array of pointers to arrays. However, Hippy's suggestion is easier to implement in code and it is how multidimensional arrays are implemented "behind the scenes" anyway.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"I have always wished that my computer would be as easy to use as my telephone.· My wish has come true.· I no longer know how to use my telephone."

    - Bjarne Stroustrup
  • RinksCustomsRinksCustoms Posts: 531
    edited 2008-09-29 23:40
    The math behind a pseudo-3D array intrigues me, can anyone put it into laymans terms?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    E3 = Thought

    http://folding.stanford.edu/·- Donating some CPU/GPU downtime just might lead to a cure for cancer! My team stats.
  • hippyhippy Posts: 1,981
    edited 2008-09-30 00:02
    You mean as in "myArray[noparse][[/noparse]x,y,z]" ?

    If so, for x = 0 to X, y = 0 to Y, z = 0 to Z

    myArray[noparse][[/noparse] x * ( (Y+1)*(Z+1) ) + y * (Z+1) + z ]

    ( keeping my fingers crossed I've got that right )
  • SapiehaSapieha Posts: 2,964
    edited 2008-09-30 00:04
    Hi RinksCustoms.

    hippy said..............
    VAR
    · byte myArray[noparse][[/noparse]800]

    800 Linear bytes(X00000y00000y00000X00000y00000y00000X00000y00000y00000X00000y00000y00000X00000y00000y00000)
    ·You can break it in logic ARAY elements
    X1 (X00000y00000y00000
    X2 (X00000y00000y00000
    X3 (X00000y00000y00000
    X4 (X00000y00000y00000
    X5 (X00000y00000y00000

    etc....

    PUB Main | i
    · i := Get_myArray(10,12)· ' myArray[noparse][[/noparse]1,1] := myArray[noparse][[/noparse]10,12]
    · Set_myArray(1,1,i)
    PRI Get_myArray(x,y)
    · return myAray[noparse][[/noparse] x * 50 + y ]
    PRI Set_myArray(x,y,value)
    · myArray[noparse][[/noparse] x * 50 + y ] := value

    ·

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nothing is impossible, there are only different degrees of difficulty.
    For every stupid question there is at least one intelligent answer.
    Don't guess - ask instead.
    If you don't ask you won't know.
    If your gonna construct something, make it·as simple as·possible yet as versatile as posible.


    Sapieha

    Post Edited (Sapieha) : 9/30/2008 12:33:45 AM GMT
  • RinksCustomsRinksCustoms Posts: 531
    edited 2008-09-30 00:09
    the only place i can think of that being useful is for a car engine emulator, or an RGB colorspace comparing program (like for matching a color to a databased color).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    E3 = Thought

    http://folding.stanford.edu/·- Donating some CPU/GPU downtime just might lead to a cure for cancer! My team stats.
  • SRLMSRLM Posts: 5,045
    edited 2008-09-30 02:51
    multi dimensional arrays are useful in lots of places. Pretty much anywhere where you could use a table in a GUI, a multi array could store that data behind the scenes. Besides the obvious uses in coordinate systems, multi arrays could be used to store data such as temperature and humidity for each month(one axis in in units of months, the other is the two values, a 2x12 array), books on a bookshelf (number of shelves high x books wide x book cases back), or other interresting uses. Of course, if you have to make your own system, you'll probably have more mundane uses for the array.
  • RottenJalapenoRottenJalapeno Posts: 27
    edited 2008-09-30 08:13
    Thank you guys for your advice, I sort of thought thats what I would have needed to do. and I guess once it is down on paper it is easier than I first thought.
    I really want to thank all of parallax for their free software, object exchange, and forums. They have helped me greatly in this project and the projects before. I am still learning a great deal during every project, and I hope one day I can put my 2 pennies back in the jar of knowledge. Thanks again parallax, you rock.
Sign In or Register to comment.