Announcement

Collapse
No announcement yet.

PB 3.5 DOS

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

  • PB 3.5 DOS

    Hi
    Can anybody help please, i'm trying to do an ARRAY SORT
    with a UDT that is defined as VIRTUAL and I am getting an
    error 210, is there somthing wrong with my sintax or is it
    not possible.

    ARRAY SORT battext() FOR nm%, FROM 2 TO 9
    also tryed without the FOR nm%

    many thanks

    Bob



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

  • #2
    The ARRAY statement is not able to be used on virtual arrays.

    There are a couple of solutions:
    1. If the array is small, copy the array to a dynamic array, ARRAY SORT that array, and copy back to the virtual array.
    2. Use a quick-sort or similar algorithm directly on the virtual array.

    There have been several examples of sorting posted to this BBS - try searching for "quick sort".


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

    Comment


    • #3
      You cannot use any ARRAY command with VIRTUAL or HUGE arrays. And I'm wondering if it's possible to ARRAY SORT
      a UDT array at all. I just want to say this about ARRAY SORT: it's unbelievable fast! If you ever need to sort a UDT with several
      sort keys, don't hesitate to copy the entire array to a temporary array and then sort it with an index number array as TAGARRAY...
      Have a look at the following example:
      Code:
      TYPE Stat
          Power       AS INTEGER
          Toughness   AS INTEGER
      END TYPE
       
      TYPE MagicCard
          Colour      AS INTEGER
          Cardtype    AS INTEGER
          Stats       AS Stat
      END TYPE
       
      DIM LastCard2              AS SHARED INTEGER
      DIM Card2(1 TO 500)        AS SHARED MagicCard
       
      SUB SortCards(BYVAL SortKey AS INTEGER)
          DIM ValueData(1 TO LastCard2)  AS LOCAL INTEGER 'never forget LOCAL keyword
          DIM Index(1 TO LastCard2)      AS LOCAL INTEGER    
          DIM Temp                       AS LOCAL MagicCard
       
          'Fill temporary array with sort values and index numbers
          FOR i = 1 TO LastCard2
      	IF SortKey = 1 THEN                  'Card colour
                  ValueData(i) = Card2(i).Colour
              ELSEIF SortKey = 2 THEN	             'Card type
                  ValueData(i) = Card2(i).Cardtype
              ELSEIF SortKey = 3 THEN              'Card power
                  ValueData(i) = Card2(i).Stats.Power
              ELSEIF SortKey = 4 THEN              'Card toughness
                  ValueData(i) = Card2(i).Stats.Toughness
              END IF
       
              Index(i) = i
          NEXT i
          'At this point, Index() holds the original position of each element
       
          'Sort the data and remember original positions
          ARRAY SORT ValueData(1) FOR LastCard2, TAGARRAY Index(), ASCEND
       
          'Now swap the original array elements
          FOR i = 1 TO LastCard2
          	OldPos = Index(i)
              NewPos = i
       
              Temp = Card2(OldPos)
              Card2(OldPos) = Card2(NewPos)
              Card2(NewPos) = Temp
       
              'We swapped an element, so we need to update the corresponding Index() element
              FOR j = i + 1 TO LastCard2
                  IF Index(j) = NewPos THEN
                      Index(j) = OldPos
                      EXIT FOR
                  END IF
              NEXT j
          NEXT i
      END SUB
      I've done several tests with sort routines, and I must say ARRAY SORT is amazingly fast... It's much more
      efficient than my own recursive QuickSort routine (just forget about BubbleSort ). If you can, use it!

      (*mutter* Somehow Lance always wins )

      Greetings,

      ------------------
      Sebastian Groeneveld
      mailto:[email protected][email protected]</A>

      [This message has been edited by Sebastian Groeneveld (edited November 14, 2000).]
      Sebastian Groeneveld
      mailto:[email protected][email protected]ail.com</A>

      Comment


      • #4
        Thanks Lance & Sebastian for your help

        The array is about 3000 so i'll try another type of sort

        Many thanks


        Bob

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

        Comment


        • #5
          Just for whatever help it may give you Robert, here's yet
          another sort mechanism. You can substitute a hard equted
          for the swap statements in it for virutal arrays. Works
          fine that way .. Just do a simple equate statement, not a swap
          as below and you're in business with virtual memory..

          Code:
               ' Simple illustration of Shell-Metzner sorting routine
               ' for memory held arrays.  Read or store all elements
               ' into element of array with concurrent fields in each
               ' element and then sort it.  If you wish to write a merge
               ' file to the disk, and fan-fold this into block runs,
               ' you can sort larger files in stages.  The fan fold
               ' file makes a jointly open file pair pass, stuffing the
               ' final building file with the same <= comparason that is
               ' used below - first one file then the other into the final
               ' output file until done.
          
               ' Set variable X to the cap number of items needed sorting
               ' Use INTEGERS as they are much faster on sorts!   
          
          4180 PRINT
               PRINT "SORTING ACCOUNTS SEQUENCE" '        If needed
               PRINT
               L = X - 1 '                                Decrement -1 to force close
               C = L '                                    Set temp flag
               B = L '                                    Set temp flag
          4250 C = INT(C / 2) '                           Calculate array midpoint
               PRINT C; '                                 User progress tic..
                  IF C = 0 THEN '                         We are done
                     GOTO 4470 '                          Exit
                  ELSE '                                  Otherwise
                     D = 1 '                              Continue sorting
                     E = B - C '                          Set numeric comparator flag
                  END IF
          4280 F = D '                                    Set numeric comparator flag
          4290 G = F + C '                                Set numeric comparator flag
               ' Here is the comparator line.  You may use any of the elements
               ' as a comparator, but you have to understand the pecking order
               ' of what the sort heierachy is!  Further, you also need to
               ' realize that strings which are shorter are, different in
               ' sort value - they may wind up in a slightly different order
               ' than you first thought!   Shell sort works sort of by pitching
               ' each new item into upper or lower moving piles.
          
                  IF F$(F) <= F$(G) THEN 4450 '           Pitch element one way
          
               ' The comparison made - then perform what all else needs to
               ' be done in your code including full calculations if needed!
          
               SWAP F$(F), F$(G) '                        Swap invoice number
               SWAP P$(F), P$(G) '                        Swap client name
               SWAP N$(F), N$(G) '                        Swap Responsible party
               SWAP R$(F), R$(G) '                        Swap address
               SWAP S$(F), S$(G) '                        Swap City - St - Zip
               SWAP E$(F), E$(G) '                        Swap quantity
               SWAP E1$(F), E1$(G) '                      Swap invoice item
               SWAP P1$(F), P1$(G) '                      Swap Swap amount due
               SWAP V(F), V(G) '                          Swap application amount
               FOR N = 1 TO 10 '                          For ten items
                  SWAP QH(N, F), QH(N, G) '               Swap folio sub accounts
               NEXT N
          
               ' After which move markers to accurately reflect building set sizes
          
               F = F - C '                                Decrement cap
                  IF F < 1 THEN '                         Goes one way
                     GOTO 4450 '                          Do it
                  ELSE '                                  Goes the other
                     GOTO 4290 '                          Is next on already less?
                  END IF
          4450 D = D + 1 '                                Increment cap
                  IF D > E THEN '                         Get another new element
                     GOTO 4250 '                          Do it
                  ELSE '                                  Adjust cap flag
                     GOTO 4280 '                          Go do it
                  END IF
               ' We never wind uo here below unless it is all over
          4470 L = L + 1 '                                Increment progress count
               PRINT
               PRINT '                                    Invoices are now sorted by cus
               ' Your array has been sorted at this point.
          
               ' I happen to add a next element with nulls in it just for
               ' other uses, but this isn't needed just for the sort.
               F$(L) = "0" '                              Set closing format
               P$(L) = "0" '                              Set closing format
               N$(L) = "0" '                              Set closing format
               R$(L) = "0" '                              Set closing format
               S$(L) = "0" '                              Set closing format
               E$(L) = "0" '                              Set closing format
               E1$(L) = "0" '                             Set closing format
               V(L) = 0 '                                 Set closing format
               P1$(L) = "0" '                             Set closing format
               T = 0 '                                    Set closing format
               T1 = 0 '                                   Set closing format
               PRINT "AUTOMATIC POSTING NOW..."
          That ought to be enough to get you thinking about how to work with
          a solution if you want or have to maintain virtual elements... Of
          course it isn't efficient and further, degredates, I think, if
          everything is already in perfet order. Sure the function that
          is provided in PB is wonderful, but this will at least get the
          job done, --- eventually, grin ---- even with tens of thousands
          of thingees to sort! For those who protest that writing to
          a disk to fan-ffold the above is insane, it's not necessarily
          as bad as one might think. Modern operating systems with good
          disk caching make things a lot better now that it used to be. AS
          well, if you are really picky, simply put the slush work on a
          virtual disk! It all is handled in memory except for the final
          transfer you may need to store it ... Thwptt!




          ------------------
          Mike Luther
          [email protected]
          Mike Luther
          [email protected]

          Comment

          Working...
          X