Announcement

Collapse
No announcement yet.

need help with ARRAY ?

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

  • need help with ARRAY ?

    Have never used PB ARRAY code, last time I use ARRAY was with MS QUICKBASIC

    Trying to lean arrays to check out sorting with arrays

    Doing a sample program to get started and have issues with the program closes using ARRAY ASSIGN sArray(), see rem on code

    Found many examples for sorting with ARRAYS , but doing my own sample program to learn PB arrays.

    Code:
    #COMPILE EXE 'array sort ra 1.bas
    
    
    GLOBAL hDlg AS DWORD , TEXTT AS STRING, sArray() AS STRING
    
    CALLBACK FUNCTION FILE1() AS LONG
    DIM sArray(3,2) AS STRING
    
    '*** CODE BELOW ONLY WORKS TO 12, AFTER 12 PROGRAM CLOSES **** <---
    'ARRAY ASSIGN sArray() = "1", "2", "3", "4",_
    ' "5", "6", "7", "8",_
    ' "9", "10", "11", "12",_
    ' "13"', "14", "15", "16"
    
    '*** CODE BELOW ONLY WORKS TO 12, AFTER 12 PROGRAM CLOSES **** <---
    ARRAY ASSIGN sArray() = "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" ', "13", "14", "15", "16"
    
    
    'R
    '3 13 14 15 16
    '2 9 10 11 12 sArray(row,column)
    '1 5 6 7 8
    '0 1 2 3 4
    '0 1 2 3 column
    
    CONTROL SET TEXT hDlg, 201, sArray(0,0)+" "+sArray(1,0)+" "+sArray(2,0)+" "+sArray(3,0)
    CONTROL SET TEXT hDlg, 202, sArray(0,1)+" "+sArray(1,1)+" "+sArray(2,1)+" "+sArray(3,1)
    CONTROL SET TEXT hDlg, 203, sArray(0,2)+" "+sArray(1,2)+" "+sArray(2,2)+" "+sArray(3,2)
    
    '*** CODE BELOW DOES NOT WORK, CLOSES PROGRAM, **** <---
    'CONTROL set text hDlg, 204, sArray(0,3)+" "+sArray(1,3)+" "+sArray(2,3)+" "+sArray(3,3)
    
    END FUNCTION
    
    
    FUNCTION PBMAIN() AS LONG
    
    
    DIALOG NEW 0, "SET TEXT Test",,,480,300, %WS_SYSMENU, 0 TO hDlg
    
    
    CONTROL ADD LABEL, hDlg, 201,"1 LINE", 15, 50,75,450,
    CONTROL ADD LABEL, hDlg, 202,"2 LINE", 15, 70,75,450,
    CONTROL ADD LABEL, hDlg, 203,"3 LINE", 15, 90,75,450,
    CONTROL ADD LABEL, hDlg, 204,"4 LINE", 15,110,75,450,
    CONTROL ADD LABEL, hDlg, 205,"5 LINE", 15,130,75,450,
    CONTROL ADD LABEL, hDlg, 206,"6 LINE", 15,150,75,450,
    
    CONTROL ADD BUTTON, hDlg, 101,"&1 FILE ONE", 15,250,75,15, CALL FILE1
    'CONTROL ADD BUTTON, hDlg, 102,"&2 FILE TWO", 100,250,75,15, 'CALL DONE
    'CONTROL ADD BUTTON, hDlg, 103,"SORT 1" , 185,250,75,15, 'CALL DONE
    'CONTROL ADD BUTTON, hDlg, 104,"SORT 2" , 270,250,75,15, 'CALL DONE
    'CONTROL ADD BUTTON, hDlg, 105,"&EXIT" , 355,250,75,15, 'CALL DONE
    DIALOG SHOW MODAL hDlg
    END FUNCTION
    Robert

  • #2
    you need to Dim a larger array to go beyond 12
    DIM sArray(3,2)

    0 to 3, 0 to 2 = 4x3 array = 12 total

    Comment


    • #3
      Code:
      '*** CODE BELOW DOES NOT WORK, CLOSES PROGRAM, **** <---
      'CONTROL set text hDlg, 204, sArray(0,3)+" "+sArray(1,3)+" "+sArray(2,3)+" "+sArray(3,3)
      again same problem
      you DIM sArray (3,2) but you try to access sArray (x,3), cannot be more than 2 unless you REDIM or DIM with enough elements.

      in you sample code you sould use DIM sArray(3,3)

      Comment


      • #4
        missed that, making it 3,3 works

        have two questions
        1. if while working on making the array code is there a way to trap the error that occurs to give a message and continued the program?
        2. in defining arrays , how do you assign columns with different data types? (string, long, currency)

        thanks rod for your help
        Robert

        Comment


        • #5
          SImple demo of populating a multidimensional array
          '
          Code:
          #COMPILE EXE
          #DIM ALL
          
          ' Ensure array out of bounds etc are trapped
          #DEBUG ERROR ON
          #DEBUG DISPLAY ON
          
          FUNCTION PBMAIN() AS LONG
           LOCAL lArray(), x,y AS LONG
           LOCAL s AS STRING
           DIM lArray(3,3) ' Two dimensional array of 4x4 = 16 elements i.e. 0-3, 0-3
           ARRAY ASSIGN lArray() = 0,1,2,3,10,11,12,13,20,21,22,23,30,31,32,33 'populate 16 elements
          
           'display elements
           FOR x = 0 TO 3
               FOR y = 0 TO 3
                   s += STR$(lArray(x,y))
               NEXT
               s += $LF
           NEXT
           ? s
          END FUNCTION
          '

          Comment


          • #6
            Originally posted by Robert Alvarez View Post
            missed that, making it 3,3 works

            have two questions
            1. if while working on making the array code is there a way to trap the error that occurs to give a message and continued the program?
            2. in defining arrays , how do you assign columns with different data types? (string, long, currency)

            thanks rod for your help
            1. Always use when in development code. (See Help on these two compiler instructions)
            #DEBUG DISPLAY ON
            #DEBUG ERROR ON

            2. You can't mix different variable types in one array. If you need that create a UDT and use an array of UDTs
            '
            Code:
            #COMPILE EXE
            #DIM ALL
            #DEBUG ERROR ON
            #DEBUG DISPLAY ON
            
            TYPE MyType
                lng AS LONG
                curr AS CURRENCY
                str AS STRING * 40
            END TYPE
            FUNCTION PBMAIN() AS LONG
             LOCAL myArray() AS MyType
             LOCAL s AS STRING
             DIM myArray(3)
            
             myArray(1).lng = 7
             myArray(1).curr = 15.25
             myArray(1).str = "Second element"
            
             s = STR$(myArray(1).lng) & STR$(myArray(1).curr) & " " & myArray(1).str
             ? s
            END FUNCTION
            '

            Comment


            • #7
              Originally posted by Stuart McLachlan View Post
              2. You can't mix different variable types in one array. If you need that create a UDT and use an array of UDTs
              Alternatively use multiple arrays - but sorting them becomes problematic.

              Comment


              • #8
                Sturart, I can keep the array using string data, but you say sorting a 2 dim array by column may cause problems? That's what I hoped to do.

                Was hoping to reduce the time to sort a file using a UDT which is has 13 fields totaling 195 bytes and takes about a minute and half to sort 1800 records.

                Below is updated code. Something strange is happening. When I press the 1 FILE ONE button, that button disappears , then when you press the space bar it displays the button with an outline , until you release the button , and when you click EXIT it appears .

                Code:
                #COMPILE EXE 'array sort ra 2.bas
                
                
                GLOBAL hDlg AS DWORD , TEXTT AS STRING, sArray() AS STRING
                
                CALLBACK FUNCTION FILE1() AS LONG
                DIM sArray(3,5) AS STRING
                
                '*** CODE BELOW ONLY WORKS TO 12, AFTER 12 PROGRAM CLOSES **** <---
                ARRAY ASSIGN sArray() = "1", "2", "3", "4",_
                "5", "6", "7", "8",_
                "9", "10", "11", "12",_
                "13", "14", "15", "16",_
                "17", "18", "19", "20",_
                "21", "22", "23", "24"
                
                'R sArray(row,column)
                '0 1 2 3 4
                '1 5 6 7 8
                '2 9 10 11 12
                '3 13 14 15 16
                '4 17 18 19 20
                '5 21 22 23 24
                '0 1 2 3 column
                
                CONTROL SET TEXT hDlg, 201, sArray(0,0)+" "+sArray(1,0)+" "+sArray(2,0)+" "+sArray(3,0)
                CONTROL SET TEXT hDlg, 202, sArray(0,1)+" "+sArray(1,1)+" "+sArray(2,1)+" "+sArray(3,1)
                CONTROL SET TEXT hDlg, 203, sArray(0,2)+" "+sArray(1,2)+" "+sArray(2,2)+" "+sArray(3,2)
                CONTROL SET TEXT hDlg, 204, sArray(0,3)+" "+sArray(1,3)+" "+sArray(2,3)+" "+sArray(3,3)
                'CONTROL set text hDlg, 205, sArray(0,4)+" "+sArray(1,4)+" "+sArray(2,4)+" "+sArray(3,4)
                'CONTROL set text hDlg, 206, sArray(0,5)+" "+sArray(1,5)+" "+sArray(2,5)+" "+sArray(3,5)
                END FUNCTION
                
                
                FUNCTION PBMAIN() AS LONG
                
                DIALOG NEW 0, "SET TEXT Test",,,480,300, %WS_SYSMENU, 0 TO hDlg
                
                
                CONTROL ADD LABEL, hDlg, 201,"1 LINE", 15, 50,75,450,
                CONTROL ADD LABEL, hDlg, 202,"2 LINE", 15, 70,75,450,
                CONTROL ADD LABEL, hDlg, 203,"3 LINE", 15, 90,75,450,
                CONTROL ADD LABEL, hDlg, 204,"4 LINE", 15,110,75,450,
                CONTROL ADD LABEL, hDlg, 205,"5 LINE", 15,130,75,450,
                CONTROL ADD LABEL, hDlg, 206,"6 LINE", 15,150,75,450,
                
                CONTROL ADD BUTTON, hDlg, 101,"&1 FILE ONE", 15,250,75,15, CALL FILE1
                'CONTROL ADD BUTTON, hDlg, 102,"&2 FILE TWO", 100,250,75,15, 'CALL DONE
                'CONTROL ADD BUTTON, hDlg, 103,"SORT 1" , 185,250,75,15, 'CALL DONE
                'CONTROL ADD BUTTON, hDlg, 104,"SORT 2" , 270,250,75,15, 'CALL DONE
                CONTROL ADD BUTTON, hDlg, 105,"&EXIT" , 355,250,75,15, 'CALL DONE
                DIALOG SHOW MODAL hDlg
                END FUNCTION



                Robert

                Comment


                • #9
                  Originally posted by Robert Alvarez View Post
                  Sturart, I can keep the array using string data, but you say sorting a 2 dim array by column may cause problems? That's what I hoped to do.
                  You can't sort on one diimension and have the other dimension(s) "tag along", it doesn't work like that.
                  From Help:
                  When sorting a multi-dimensional array, the array is treated as a single-dimension array containing all of the elements of the multi-dimensional array, in linear column-major order.
                  ...
                  Be very careful when using ARRAY SORT with multi-dimensional arrays so as not to disrupt the organization of the data in the arrays.


                  Comment


                  • #10
                    Originally posted by Robert Alvarez View Post
                    Was hoping to reduce the time to sort a file using a UDT which is has 13 fields totaling 195 bytes and takes about a minute and half to sort 1800 records.
                    There's something very wrong with your sort code if it's taking that long. Can you post your UDT and which field(s) you are sorting on?


                    Comment


                    • #11
                      Originally posted by Robert Alvarez View Post
                      tton with an outline , until you release the button , and when you click EXIT it appears .
                      Not sure exactly why, but the dialog is not getting redrawn.
                      One solution would be to add "CONTROL REDRAW hDlg ,101 " or "DIALOG REDRAW hDlg" to the end of the callback.

                      Edit: SEE POST 15!

                      Comment


                      • #12
                        reconsidering
                        Dale

                        Comment


                        • #13
                          Originally posted by Dale Yarker View Post
                          ref post 8

                          Endless loop in callback.

                          Pressing ID 201 sets text in ID 201 which causes a message to callback which again sets text, and on and on..
                          No, you've misread it.

                          The button is 101, not 201.

                          (Good illustration of the benefit of using named equates instead of "magic numbers" for control IDs )

                          Comment


                          • #14
                            I already discovered that. Maybe I should repeat the magic number part tough
                            Dale

                            Comment


                            • #15
                              Doh! Nothing to do with not redrawing, The CONTROL REDRAW or DIALOG REDRAW were just putting the button back on top!

                              Look at the size of the labels. They are covering the 1File button when Control Set text is invoked :

                              CONTROL ADD LABEL, hDlg, 201,"1 LINE", 15, 50,75,450,

                              Change the 450 to 20!

                              Comment


                              • #16
                                ditto
                                Rgds, Dave

                                Comment


                                • #17
                                  As Dale originally pointed out in his "reconsidered" post

                                  You need to test what messages are being sent to the callback because a lot of things happen to controls other than a click.
                                  You should consider using a dialog callback rather than iindividual control callbacks..
                                  "Equates are your friend"
                                  As is indentation.
                                  No point in recreating the array every time.

                                  Here's a modification to your code showing these points. You may like to use a variation of it as a template for all of your basic DDT applications. (See FIle Templates in Help)

                                  '
                                  Code:
                                  #COMPILE EXE
                                  #DIM ALL
                                  #DEBUG ERROR ON
                                  #DEBUG DISPLAY ON
                                  
                                  ENUM Controls SINGULAR
                                    lbl1 = 101
                                    lbl2
                                    lbl3
                                    lbl4
                                    btn1File
                                    btnExit
                                  END ENUM
                                  
                                  GLOBAL sArray() AS STRING
                                  
                                  CALLBACK FUNCTION DlgCB() AS LONG
                                      SELECT CASE AS LONG CB.MSG
                                          CASE %WM_COMMAND
                                              SELECT CASE AS LONG CB.CTL
                                                  CASE %btn1File
                                                      IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN
                                                           ARRAY ASSIGN sArray() = "1", "2", "3", "4",_
                                                          "5", "6", "7", "8",_
                                                          "9", "10", "11", "12",_
                                                          "13", "14", "15", "16",_
                                                          "17", "18", "19", "20",_
                                                          "21", "22", "23", "24"
                                  
                                                          CONTROL SET TEXT CB.HNDL, %lbl1, sArray(0,0)+" "+sArray(1,0)+" "+sArray(2,0)+" "+sArray(3,0)
                                                          CONTROL SET TEXT CB.HNDL, %lbl2, sArray(0,1)+" "+sArray(1,1)+" "+sArray(2,1)+" "+sArray(3,1)
                                                          CONTROL SET TEXT CB.HNDL, %lbl3, sArray(0,2)+" "+sArray(1,2)+" "+sArray(2,2)+" "+sArray(3,2)
                                                          CONTROL SET TEXT CB.HNDL, %lbl4, sArray(0,3)+" "+sArray(1,3)+" "+sArray(2,3)+" "+sArray(3,3)
                                                      END IF
                                  
                                                 CASE %btnExit
                                                      IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN
                                                          DIALOG END CB.HNDL
                                                      END IF
                                              END SELECT
                                      END SELECT
                                  END FUNCTION
                                  
                                  FUNCTION PBMAIN() AS LONG
                                      DIM sArray(3,5) AS STRING
                                      LOCAL hDlg AS DWORD
                                  
                                      DIALOG NEW 0, "SET TEXT Test",,,480,300, %WS_SYSMENU, 0 TO hDlg
                                          CONTROL ADD LABEL, hDlg, %lbl1,"1 LINE", 15, 50,75,20,
                                          CONTROL ADD LABEL, hDlg, %lbl2,"2 LINE", 15, 70,75,20,
                                          CONTROL ADD LABEL, hDlg, %lbl3,"3 LINE", 15, 90,75,20,
                                          CONTROL ADD LABEL, hDlg, %lbl4,"4 LINE", 15,110,75,20,
                                          CONTROL ADD BUTTON, hDlg, %btn1File,"&1 FILE ONE", 15,250,75,15
                                          CONTROL ADD BUTTON, hDlg, %btnExit,"&EXIT" , 355,250,75,15
                                      DIALOG SHOW MODAL hDlg CALL DlgCB
                                  END FUNCTION
                                  '

                                  Comment


                                  • #18
                                    Post #15, I check that, the dialog is 480 width, should have not cause any wrap around pushing text down over buttons below, and text for each row is small. Label size is 430.

                                    On my program sorting time, will have to check it, some of that time is for compiling two files of data just before the bubble sort.

                                    Now will try array scan (find) in this 2 dim array to see how that works, my find code I use is OK but at rare times will miss finding a word or some characters in a data file.


                                    Robert

                                    Comment


                                    • #19
                                      The labels cover over the buttons!
                                      Dale

                                      Comment


                                      • #20
                                        480 is dialog width (as stated), but 450 is label height. Button vertical position is 250. Setting label text then messes up button redraw.
                                        Dale

                                        Comment

                                        Working...
                                        X