Announcement

Collapse
No announcement yet.

ARRAY SCAN Questions

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

  • ARRAY SCAN Questions

    Hey Everyone

    I am really confused about how to go about scanning an array for a particular variable. First lets say you have a two dimensional string array, DIM recsArray(1 to N),(1 to 12) ,and you want to scan for a variable that would be in the 12th column, the variable could possibly be in other columns as well so you want to make sure the scan only matches info in the 12th column. How would you word the ARRAY SCAN command to search for lets call it "mVar" in the 12th column. I have read the Help files over and over and also read Gary Beene's tutorial on Array Scan but still can't figure out how to do it.

    The second question is to scan only data in the 1st column is this the proper way to set up the scan. ARRAY SCAN recsArray(1,1) FOR N+1, = mVar, TO var&

    I hope someone can clear me up on this ARRAY SCAN because right now I am totally confused.


    Thanks in advance for any and all help
    Leroy



  • #2
    Hi Leroy!

    Something like this? (untested)

    Code:
    Array Scan A(n*11+1,1)  for n, = mVar to iPos
    I'll go give it a try ...

    Comment


    • #3
      Sorry Gary that doesn't work.

      Comment


      • #4
        Howdy, Leroy!

        Here's an example that shows what you're asking.

        In this case, the array is 4 cols with 3 rows

        Code:
        'Compilable Example:
        Function PBMain() As Long
           Local iRow,iCol,iPos As Long, mVar$
        
                'D(cols,     rows)
           ReDim D(1 To 4, 1 To 3) As String
           D(1,1)="1"
           D(2,1)="2"
           D(3,1)="3"
           D(4,1)="4"
           D(1,2)="5"
           D(2,2)="6"
           D(3,2)="7"
           D(4,2)="8"
           D(1,3)="9"
           D(2,3)="xx"
           D(3,3)="11"
           D(4,3)="12"
           ? Join$(D(),$CrLf)
           mVar$ = "xx"
           Array Scan D(1,3) For 4, = mVar$, To iPos
           ? Str$(iPos)   'location relative to the starting point
        End Function
        
        
        '1  4   7   10
        '2  5   8   11
        '3  6   9   12

        Comment


        • #5
          And using Gary's code to scan first column only, if D(4,1)="xx", one can do: ARRAY SCAN D() FOR UBOUND(D(1)), = mVar$, TO iPos

          Comment


          • #6
            Howdy Borje!

            If I had put an "xx" in an earlier column, your suggestion would return that position. In this case, Leroy wanted to find where in the last column mVar$ was found, so the starting point of D() had to be D(1,3).

            Comment


            • #7
              Hi Gary, yes, absolutely right, but there were 2 questions. You did the hard work, I only followed up with a way to answer to Q 2.

              Comment


              • #8
                Howdy, Borje!

                I'd have answered his question sooner but I'd forgotten whether is was D(row,col) or D(col, row), insofar as the use of Join$ and Array Scan are concerned!

                He had a second question? I'm afraid my attention span of late has been quite limited Does your country use the phrase "Squirrel!" to acknowledge how a person can get distracted? It happens to me a lot - especially when my wife is in our mutually-shared office space. She can ask more distracting questions ... while spending at most 1-2s to answering the question herself. That's not only distracting but annoying!

                I'd get way more work done, and more thoroughly, if I had an office to myself!

                Comment


                • #9
                  Gary that works fine now. I already had it working with this statement in my example above. ARRAY SCAN recsArray(1,12), = mVar TO iPos& I wasn't sure it was ONLY checking column 12 but I tried all kinds of examples to try and mess it up but it seemed to work in every case. In BOTH cases is it doing the same thing?

                  Borje thanks. Here is the way I was doing it ARRAY SCAN D(1,1) FOR iNum+1 = mVar TO iPos............iNum would be UBOUND(d()) That is what was in the help file, I tried it and it worked so I was thinking I had that one right. The part I don't understand is what help calls the INDEX and COUNT. Help says index is the starting element and count is the number of consecutive elements that get scanned. So I don't guess you can start an index on a certain COLUMN which is the way I was understanding it BUT I WAS EVIDENTLY WRONG ABOUT THAT. Am I understanding this correctly now in that you can not start an index on a certain COLUMN.

                  Comment


                  • #10
                    > you can not start an index on a certain COLUMN

                    Can you rearrange the array from

                    DIM recsArray(1 to N),(1 to 12)

                    to

                    DIM recsArray(1 to 12),(1 to N)

                    ?
                    "Not my circus, not my monkeys."

                    Comment


                    • #11
                      I had problems with index, FOR, and FROM...TO... in multi-diensional arrays, so I came up with a kludge.


                      Code:
                      #COMPILE EXE
                      #DIM ALL
                      
                      FUNCTION PBMAIN () AS LONG
                          LOCAL rownum,colnum AS LONG
                          LOCAL strSearch AS STRING
                      
                          'Build test array
                          DIM s(2 TO 10, 3 TO 12) AS STRING
                          FOR rownum = 2 TO 10
                              FOR colnum = 3 TO 12
                                  s(rownum,colnum) = "Row" & STR$(rownum) & ":Column" & STR$(colnum)
                              NEXT
                          NEXT
                      
                      'Get something to search for in a column
                          colnum = 3
                          strSearch = s(2,colnum)
                      
                          rownum  = FindInColumn(s(),colnum,strSearch)
                      
                          IF rownum > 0 THEN
                           ? USING$($DQ & strSearch & $DQ & "   found at array(# , #)",rownum,colnum)
                          ELSE
                           ? strSearch & " not found in column" & STR$(colnum)
                          END IF
                      END FUNCTION
                      
                      FUNCTION FindInColumn(a() AS STRING, colnum AS LONG, s AS STRING) AS LONG
                          LOCAL lngRows AS LONG
                          LOCAL lngResult AS LONG
                          'Search from start of desired column
                          'FOR doesn't seem to work so search the rest of the array
                          'and check whether found index is in the first searched column
                          lngRows = UBOUND(a(1)) - LBOUND(a(1)) + 1
                          ARRAY SCAN a(LBOUND(a(1)),colnum)  ,= s, TO lngResult
                          IF lngResult > 0 AND lngResult <= lngRows THEN
                              FUNCTION = lngResult + LBOUND(a(1)) - 1
                          ELSE
                              FUNCTION = 0
                          END IF
                      END FUNCTION

                      Comment


                      • #12
                        Hey Stuart very good.

                        The way I have this 2 dimensional array in my mind is like this. Your rownum represents records in say a database file and the colnum represents the columns. So lets say you load all the records into an array then you want to search a certain COLUMN for a "string variable" but you DON'T KNOW WHAT rownum it is in. Using your example where strSearch = s(2,colnum), what if you don't know the rownum then how can I use your program to find my desired string.

                        Comment


                        • #13
                          Originally posted by Leroy Hindman View Post
                          Hey Stuart very good.

                          The way I have this 2 dimensional array in my mind is like this. Your rownum represents records in say a database file and the colnum represents the columns. So lets say you load all the records into an array then you want to search a certain COLUMN for a "string variable" but you DON'T KNOW WHAT rownum it is in. Using your example where strSearch = s(2,colnum), what if you don't know the rownum then how can I use your program to find my desired string.
                          As I said in the other thread, strSearch = s(2,colnum) is just test code to get a known value at a known location in the column given the way the test array was filled.

                          You can change the 2 to any other number to confirm that it finds the correct row or change colnum to another value to confirm that it doesn't find the string.


                          Try strSearch = (RANDOM(3,10),colnum). It will get a string from a random row in that column and you can still confirm that it has found the correct row.


                          In your situation, just make strSearch the value you are looking for.

                          Comment


                          • #14
                            Stuart thanks again but lets say you have a huge array and you want to search for a specific variable, you know which column it is in but you don't know what row it is in, so how would I use your program to search for it.

                            Comment


                            • #15
                              ARRAY SCAN is useful, but if it doesn't do exactly what you need then just program an equivalent yourself with FOR..NEXT loops.

                              Comment


                              • #16
                                Originally posted by Leroy Hindman View Post
                                Stuart thanks again but lets say you have a huge array and you want to search for a specific variable, you know which column it is in but you don't know what row it is in, so how would I use your program to search for it.
                                Here's a more complex demo which searches for both "=" and ">=" . You specify the data to search for and the column to search in:

                                You could extend it with another parameter to the functions to specify whether to use COLLATE in the scans.

                                Code:
                                FUNCTION PBMAIN AS LONG
                                    LOCAL x,y,z,colnum AS LONG
                                    LOCAL strSearch AS STRING
                                
                                    'NB  does not work properly with negative lower bounds!!!
                                    DIM arrtest(2 TO 100,5 TO 10) AS STRING
                                    RANDOMIZE
                                    FOR x = LBOUND(arrtest(1)) TO UBOUND(arrtest(1))
                                        FOR y = LBOUND(arrtest(2)) TO UBOUND(arrtest(2))
                                            'fill array with random data in form: TestDatax where x in range 1 to 9
                                            arrTest(x,y) = "TestData" & FORMAT$(RND(1,9))
                                         NEXT
                                     NEXT
                                   'Get search parameters
                                      strSearch = INPUTBOX$("Enter text to search for (as TestDataX)")
                                      colnum = VAL((INPUTBOX$("Enter column to search (between" &  STR$(LBOUND(arrtest(2))) & " and " & STR$(UBOUND(arrtest(2))))))
                                
                                 'Look for exact match
                                     z = FindIncolumn(arrTest(),colnum,strSearch)
                                     IF z >= LBOUND(arrtest(1)) THEN
                                         ? "'" & arrTest(z,colnum) & "' found in column " & STR$(colnum) & " at row " & STR$(z)
                                     ELSE
                                       ? "'" & strSearch & "' not found in column " & STR$(colnum)
                                    END IF
                                'Look for >= match
                                     z = FindGEIncolumn(arrTest(),colnum,strSearch)
                                     IF z >= LBOUND(arrtest(1)) THEN
                                             ? "'" & arrTest(z,colnum) & "' which is equal to or greater than '" & strSearch & "' found in column " & STR$(colnum) & " at row " & STR$(z)
                                       ELSE
                                       ? "'" & strSearch & "' or greater not found in column " & STR$(colnum)
                                     END IF
                                
                                 END FUNCTION
                                
                                
                                FUNCTION FindInColumn(a() AS STRING, colnum AS LONG, s AS STRING) AS LONG
                                    LOCAL lngRows, lngResult AS LONG
                                    lngRows = UBOUND(a(1)) - LBOUND(a(1)) + 1
                                    ARRAY SCAN a(LBOUND(a(1)),colnum)  ,= s, TO lngResult
                                    IF lngResult > 0 AND lngResult <= lngRows THEN
                                        FUNCTION = lngResult + LBOUND(a(1)) - 1
                                    ELSE
                                        FUNCTION = LBOUND(a(1)) - 1
                                    END IF
                                END FUNCTION
                                
                                FUNCTION FindGEInColumn(a() AS STRING, colnum AS LONG, s AS STRING) AS LONG
                                    LOCAL lngRows, lngResult AS LONG
                                    lngRows = UBOUND(a(1)) - LBOUND(a(1)) + 1
                                    ARRAY SCAN a(LBOUND(a(1)),colnum)  ,>= s, TO lngResult
                                    IF lngResult > 0 AND lngResult <= lngRows THEN
                                        FUNCTION = lngResult + LBOUND(a(1)) - 1
                                    ELSE
                                        FUNCTION = LBOUND(a(1)) - 1
                                    END IF
                                END FUNCTION

                                Comment


                                • #17
                                  Originally posted by Paul Dixon View Post
                                  ARRAY SCAN is useful, but if it doesn't do exactly what you need then just program an equivalent yourself with FOR..NEXT loops.
                                  Yep, simpler and may or may not be quicker, depending on number of rows/columns and which column you are searching
                                  (and it does work with negative lower bounds!)

                                  Code:
                                  FUNCTION PBMAIN AS LONG
                                      LOCAL x,y,z,colnum AS LONG
                                      LOCAL strSearch AS STRING
                                  
                                      DIM arrtest(2 TO 100,5 TO 10) AS STRING
                                      RANDOMIZE
                                      FOR x = LBOUND(arrtest(1)) TO UBOUND(arrtest(1))
                                          FOR y = LBOUND(arrtest(2)) TO UBOUND(arrtest(2))
                                              'fill array with random data in form: TestDatax where x in range 1 to 9
                                              arrTest(x,y) = "TestData" & FORMAT$(RND(1,9))
                                           NEXT
                                       NEXT
                                  
                                        strSearch = INPUTBOX$("Enter text to search for (as TestDataX)")
                                        colnum = VAL((INPUTBOX$("Enter column to search (between" &  STR$(LBOUND(arrtest(2))) & " and " & STR$(UBOUND(arrtest(2))))))
                                  
                                       z = FindIncolumn(arrTest(),colnum,strSearch)
                                       IF z >= LBOUND(arrtest(1)) THEN
                                           ? "'" & arrTest(z,colnum) & "' found in column " & STR$(colnum) & " at row " & STR$(z)
                                       ELSE
                                         ? "'" & strSearch & "' not found in column " & STR$(colnum)
                                  
                                       END IF
                                  
                                       z = FindGEIncolumn(arrTest(),colnum,strSearch)
                                  
                                       IF z >= LBOUND(arrtest(1)) THEN
                                               ? "'" & arrTest(z,colnum) & "' which is equal to or greater than '" & strSearch & "' found in column " & STR$(colnum) & " at row " & STR$(z)
                                         ELSE
                                         ? "'" & strSearch & "' or greater not found in column " & STR$(colnum)
                                       END IF
                                  
                                   END FUNCTION
                                  
                                  
                                  FUNCTION FindInColumn(a() AS STRING, colnum AS LONG, s AS STRING) AS LONG
                                      LOCAL x, lngResult AS LONG
                                      lngResult = LBOUND(a(1)) - 1
                                      FOR x = LBOUND(a(1)) TO UBOUND(a(1))
                                         IF a(x,colnum) = s THEN
                                             lngResult = x
                                             EXIT FOR
                                         END IF
                                      NEXT
                                      FUNCTION = lngResult
                                  END FUNCTION
                                  
                                  FUNCTION FindGEInColumn(a() AS STRING, colnum AS LONG, s AS STRING) AS LONG
                                      LOCAL x, lngResult AS LONG
                                      lngResult = LBOUND(a(1)) - 1
                                      FOR x = LBOUND(a(1)) TO UBOUND(a(1))
                                         IF a(x,colnum) >= s THEN
                                             lngResult = x
                                             EXIT FOR
                                         END IF
                                     NEXT
                                      FUNCTION = lngResult
                                  END FUNCTION

                                  Comment


                                  • #18
                                    Again, can you rearrange the array from

                                    DIM recsArray(1 to N),(1 to 12)

                                    to

                                    DIM recsArray(1 to 12),(1 to N)

                                    ?
                                    "Not my circus, not my monkeys."

                                    Comment


                                    • #19
                                      Originally posted by Eric Pearson View Post
                                      Again, can you rearrange the array from
                                      DIM recsArray(1 to N),(1 to 12)
                                      to
                                      DIM recsArray(1 to 12),(1 to N)
                                      ?
                                      So instead of looking for the value in Column X, you are looking for it in Row X?

                                      The array will be stored in as
                                      1,1
                                      2,1
                                      3,1
                                      ...
                                      12,1
                                      1,2
                                      2,2
                                      3,2
                                      ...
                                      12,2
                                      1,3
                                      2,3
                                      ...
                                      ...
                                      12,N

                                      To find something in Row X, you need to look at every 12th item, not just sequential through a block of items.

                                      Comment

                                      Working...
                                      X