Announcement

Collapse
No announcement yet.

Array sort, array scan.. on udt's ?

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

  • Array sort, array scan.. on udt's ?

    I have a number of 'records' consisting of 4 'fields' in memory.
    I would like to use the commands 'array sort', 'array scan',
    'array insert' and 'array delete'. Question is: is this possible and
    what data structure should I use. I could use a UDT-array or 4 separate
    arrays (one for every field).
    'Array sort' only works on 1 array and 1 tagarray..which leaves 2 arrays
    unsorted.
    Sorting a udt-array (of 4 fields) seems to work, but scan, delete and
    insert didn't work in my small test.
    I see 2 ways open:
    - eiter write my own sort and insert routines (on arrays or udt's),
    - or use a main array on which i use the array sort, together with
    a tagarray of pointers that points to a udt-array with the 3
    remaining fields (or pointers to 3 separate arrays).

    Anybody any suggestion?

    Kind regards
    Eddy



    ------------------
    Eddy

  • #2
    Eddy,

    ARRAY SCAN & ARRAY SORT have a [, FROM start TO end] option which
    allows you to specify the byte positions of the UDT field which you
    want to sort/scan.

    I've been able to use ARRAY INSERT & ARRAY DELETE with UDT arrays.
    Be careful with ARRAY INSERT and UDT arrays though. There is a known
    glitch with UDT's which have a LEN MOD 4 <> 0. If your UDT is an
    "odd" size, you should pad the UDT with an extra BYTE at the end.



    ------------------
    Bernard Ertl
    Bernard Ertl
    InterPlan Systems

    Comment


    • #3
      Or use DWORD alignment. Note that this issue will be addressed in the next update to the compiler.



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

      Comment


      • #4
        Thanks for the tip, Bern!
        I will try that!!


        ------------------
        Eddy

        Comment


        • #5
          Originally posted by Bern Ertl:
          Eddy,

          ARRAY SCAN & ARRAY SORT have a [, FROM start TO end] option which
          allows you to specify the byte positions of the UDT field which you
          want to sort/scan.

          I've been able to use ARRAY INSERT & ARRAY DELETE with UDT arrays.
          Be careful with ARRAY INSERT and UDT arrays though. There is a known
          glitch with UDT's which have a LEN MOD 4 <> 0. If your UDT is an
          "odd" size, you should pad the UDT with an extra BYTE at the end.

          Bern,
          I tried it. Array sort, scan and delete work, but array insert does not.
          After inserting, I can only read the array elements lower than the position
          where the new element was inserted.
          My test-UDT was:

          Type UDTtype
          f1 as string*8
          f2 as string*4
          end type
          dim test(5) as UDTtype
          dim ins as UDTtype

          array insert test(3) for 2, ins

          After this I can only read test(1) and test(2)

          ------------------
          Eddy

          Comment


          • #6
            Could you post the complete example?



            ------------------
            Bernard Ertl
            Bernard Ertl
            InterPlan Systems

            Comment


            • #7
              Sorry that I couldn't reply earlier, but here is some code which
              demonstrates my problem: Array sort, scan and delete work but
              'array insert' produces strange results:
              I hope the code is readable cause I don't know how to preserve
              the indenting...

              Code:
              'Demo of problem with 'array insert' on an array of UDT's,
              'Array scan, sort and delete (of a UDT) seems to work.
              #COMPILE EXE
              #REGISTER NONE
              #DIM ALL
              #INCLUDE "win32api.inc"
              #INCLUDE "commctrl.inc"
              #INCLUDE "COMDLG32.INC"
              
              TYPE testUDT
                  f1  AS STRING * 8
                  f2  AS STRING * 4
              END TYPE
              
              FUNCTION PBMAIN
                  LOCAL hMainForm&, Style&
                  Style& = %WS_POPUP OR %DS_MODALFRAME OR %WS_CAPTION OR %WS_MINIMIZEBOX OR %WS_SYSMENU OR %DS_CENTER
                  DIALOG NEW 0, "UDTtest", 0, 0,  300,  300, Style&, 0  TO hMainForm&
                  CONTROL ADD TEXTBOX, hMainForm&,  100,  "", 10, 10, 280, 280, _
                    %WS_CHILD OR %WS_VISIBLE OR %ES_AUTOHSCROLL OR %ES_MULTILINE OR %ES_WANTRETURN OR %ES_AUTOVSCROLL _
                    OR %WS_HSCROLL OR %WS_VSCROLL, %WS_EX_CLIENTEDGE
              
                  DIM i       AS LONG
                  DIM j       AS LONG
                  DIM buffer$
                  DIM udt(1:6)  AS testUDT
                  DIM SingleUdt AS testUDT
                      SingleUdt.f1= "udt-test"
                      SingleUdt.f2= "0000"
                      udt(1).f1   = "zzzz    "
                      udt(1).f2   = "1111"
                      udt(2).f1   = "aaaa    "
                      udt(2).f2   = "2222"
                      udt(3).f1   = "cccc    "
                      udt(3).f2   = "3333"
                      udt(4).f1   = "bbbb    "
                      udt(4).f2   = "4444"
                      udt(5) = SingleUdt
                      ARRAY SCAN udt(1),FROM 1 TO 8, = "bbbb    ", TO j       '<<<<<<<<<<<<<<<
                      FOR i = 1 TO 5
                          buffer$=buffer$+ udt(i).f1 + udt(i).f2 + $CRLF
                      NEXT i
                      buffer$=buffer$+"Result of 'array scan' for 'bbbb'is:"+STR$(j)+ $CRLF + $CRLF
              
                          ARRAY SORT udt(1) FOR 5                             '<<<<<<<<<<<<<<<
                          FOR i = 1 TO 5
                              buffer$=buffer$+ udt(i).f1 + udt(i).f2 + $CRLF
                          NEXT i
                          buffer$=buffer$+"Result after 'array sort'"+ $CRLF + $CRLF
              
                      ARRAY DELETE udt(4)                                     '<<<<<<<<<<<<<<<
                      FOR i = 1 TO 4
                          buffer$=buffer$+ udt(i).f1 + udt(i).f2 + $CRLF
                      NEXT i
                      buffer$=buffer$+"Result after 'array delete' element 4"+ $CRLF + $CRLF
              
                          buffer$=buffer$+"'array insert' seems to fail:"+ $CRLF
                          ARRAY INSERT udt(2) FOR 3, SingleUdt                '<<<<<<<<<<<<<<<
                          FOR i = 1 TO 4
                              buffer$=buffer$+ udt(i).f1 + udt(i).f2 + $CRLF
                          NEXT i
                          buffer$=buffer$+"Result after 'array insert'"+ $CRLF + $CRLF
              
                      CONTROL SET TEXT hMainForm&, 100, buffer$
                  DIALOG SHOW MODAL hMainForm&
              END FUNCTION

              ------------------
              Eddy

              Comment


              • #8
                Eddy, you are another victim of the known glitch about the last
                byte getting corrupted. If you will notice, the value that is
                actually inserted is missing the last "0" character. Strange,
                since your UDT is dword aligned & sized.

                Change your type declaration like so:

                Code:
                TYPE testUDT
                    f1  AS STRING * 8
                    f2  AS STRING * 4
                    dummy AS BYTE
                END TYPE
                and it will work.



                ------------------
                Bernard Ertl
                Bernard Ertl
                InterPlan Systems

                Comment


                • #9
                  Originally posted by Bern Ertl:
                  Change your type declaration like so:

                  Code:
                  TYPE testUDT
                      f1  AS STRING * 8
                      f2  AS STRING * 4
                      dummy AS BYTE
                  END TYPE
                  and it will work.
                  Indeed Bern, it works now!
                  I thought I was safe because of the 4-byte alignment. Obviously
                  I thought wrong...
                  Thanks for the support!
                  Eddy



                  ------------------
                  Eddy

                  Comment

                  Working...
                  X