Announcement

Collapse
No announcement yet.

Array of Arrays?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Array of Arrays?

    Hey there, quick question for you oh-so-helpful gurus. )

    I have an array of ID's, each ID requires it's own specific array of items. For instance,

    Sally - 1, 2, 3
    Judy - 4, 5, 6

    What's the most efficient way to go about doing this, I would like the array to be dynamic that's why I'm not using UDT's. Any other suggestions?

    ------------------

  • #2
    Use a two-dimensional array:

    Code:
    %Sally = 0
    %Judy =  1
    %Joe  =  2
    %ID1  = 0
    %ID2  = 1
    %ID3  = 2
    
    DIM People (2,2) AS whatever
    
    Sally_Id1 = People(%Sally, %ID1)
    Joe_Id3   = People(%Joe, %ID3)
    etc. etc.
    MCM
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Without knowing more about the likely sizes of the elements (ie, how big they can get), etc, you could use UDTs in a linked-list manner, or go for the simple solution of 2 arrays:

      Array #1 is a single dimension array that holds the names.

      Array #2 is a two-dimensional array: the 1st dimension is the per-name item index value, the second is the actual item data.
      Code:
      A$(1) = "Sue"
      B(1,1) = 1
      B(1,2) = 2
      B(1,3) = 4
      A$(2) = "Bob"
      B(2,1) = 10
      B(2,2) = 11
      B(2,3) = 12
      ...
      Since REDIM PRESERVE only allows the outer dimension to be changed, you could reverse the dimension definitions if you need to resize the number of names, rather than the maximum number of data items per name.

      I seem to recall there being some discussions here on using a Heap to store and manage arrays of arrays... Although I can't locate the discussions right this minute.

      Maybe someone else can recall the title of that discussion?

      ------------------
      Lance
      PowerBASIC Support
      mailto:[email protected][email protected]</A>
      Lance
      mailto:[email protected]

      Comment


      • #4
        I once knew a guru - he lived in my Amiga 1000. Was into serious meditation...

        UDT's would simplify things. However, I think a fixed length string array might work ok.
        Code:
        ...
        Dim IDArray(1 to 20) As String * 32 ' (name len = 20;  3 long ints = 12
        IDArray(1) = "Billy"
        Mid$(IDArray(1), 21) = Mkl$(6)
        Mid$(IDArray(1), 25) = Mkl$(7)
        Mid$(IDArray(1), 29) = Mkl$(8)
        ...
        StdOut "Name:" & Left$(IDArray(1),20) & "  ID1:" & Str$(Cvl(Mid$(IDArray(1),21))) & _
                        "  ID2:" & Str$(Cvl(Mid$(IDArray(1),25))) & _
                        "  ID3:" & Str$(Cvl(Mid$(IDArray(1),29)))
        ...or something like that.
        Ron

        Comment


        • #5
          Well that's what's I need to do lance. Thanks for the two array suggestion. But I do need to resize the number of names, I'm actually writing a server/client app. And the client can actually connect to multiple servers at once, but since different servers have different plugins (the program uses .dll's dynamically as plugins), each server has it's own individual plugins, and various other information - server specific. So I need an array of arrays, and in visual basic I would do this simply by making an array of collections. In powerbasic I still am not clear on how to do this efficiently, even in VB a collection isn't efficient. Also, diffrent servers all do not have the same number of plugins, so perhaps this would require multiple arrays of multiple dimensions (thus one socket id might need an array of 3, another socket id might need an array of 5) and I need to be able to change the contents of all arrays, delete and add new id's with plugins on the fly., I'm stumped, please help. Thanks.

          ------------------

          Comment


          • #6
            You might want to get yourself a good book on "data structures".
            It sounds as if you're looking for something a bit more advanced than
            an array. Perhaps a tree or linked list would be more suitable.


            ------------------
            Tom Hanlin
            PowerBASIC Staff

            Comment


            • #7
              Matt,
              The simple way is to use a single string array, 2 dimensional with the second dimension as 2.
              ie. DIM MyConnections(1:20, 1:2) AS STRING

              Then, store your 1st order data as strings, and the 2nd order data as "comma-delimited" strings
              ie.
              MyConnections(1:1) = "Connect1:"
              MyConnections(1:2) = "Ps1,Tg3,NT4"
              MyConnections(2:1) = "Connect2:"
              MyConnections(2:2) = "AGP,TNB,NOV1,NT4,Ps1"

              Then you can use functions like PARSE$(), MID$(), etc to manage the data. If the data is numeric, you could use VAL() and FORMAT$() for conversions.

              regards,


              [This message has been edited by Ian Cairns (edited September 24, 2001).]
              :) IRC :)

              Comment


              • #8
                If you cross my idea with Ron's and Ian's, then you'll probably have a winner...

                That is, use a 2D string array, store the names in one element, and store MKL$(numval&) or MKI$(numval%) data concatenated together in the second element.

                However, if this wont fly, then go with a linked-list, or do some research on advanced data structures...

                ------------------
                Lance
                PowerBASIC Support
                mailto:[email protected][email protected]</A>
                Lance
                mailto:[email protected]

                Comment


                • #9
                  Thanks Ian, and Lance, it looks like a Hybrid of both your suggestions is going to work really well! MKL$/I is a great idea too, implimenting this right now! I'm also researching advanced data structures Tom :}.

                  Thanks again,
                  Matt

                  ------------------

                  Comment


                  • #10
                    Linked lists and trees aren't really such advanced data structures,
                    it's just that they are most easily implemented with the help of
                    pointers, so many BASIC programmers haven't had the chance to make
                    their acquaintance. Of course, unlike your average BASIC, PowerBASIC
                    does support pointers.

                    Data structures are definitely worth a little research. They can
                    pack a real whallop in your programming toolkit.

                    ------------------
                    Tom Hanlin
                    PowerBASIC Staff

                    Comment

                    Working...
                    X