Announcement

Collapse
No announcement yet.

Assistance with "ARRAY SCAN" PBWIN 9

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

  • Assistance with "ARRAY SCAN" PBWIN 9

    My text file contains component & cost, comma delimited
    I Input these to a 2 dimensional array.
    I want to use ARRAY SCAN to find the index of a particular component in order to extract the cost.
    I have looked and experimented with the HELP examples for some time but I don't fully grasp the use of the "n+1" in this line:
    ARRAY SCAN A(0,0) FOR n+1
    and I think this will solve my problem when I fully understand its usage.
    Probably a very simple answer, but I'm not that smart with some of the help file example syntax.
    Any help appreciated!


    Ian B

  • #2
    The scan will only search the first n+1 elements for the condition.

    Just like it says in help:
    In this case, ARRAY SCAN A(0,0) FOR n+1 would scan only elements (0,0)...(n,0), while ARRAY SORT A(0,0) would sort the entire array: elements (0,0)…(n,x).
    (mixing in that reference to ARRAY SORT when explaining ARRAY SCAN is, well, "different")

    The way the memory is laid out, the n+1 elements (0,0) to (n,0) are in contiguous memory, so the scan is basically looking only at elements where the second subscript is 0
    That's exactly what you will want to do to find the component (row 0).

    I am assuming [CODE NOT SHOWN] your data are.....
    A(0,0) = component0 ; A(0,1) = cost0
    A(1,0) = component1 ; A(1,1) = cost1
    A(2,0) = component2 ; A(2,1) = cost2
    ....

    So, if you seach all the "first dimension subscript zero" by restricting the scan as shown, you will have searched all the components.

    Your cost will be at A(hit-1,1) because hit is always 1-based.

    MCM
    Michael Mattias
    Tal Systems Inc. (retired)
    Racine WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Ian,
      maybe an example will help:

      Paul.
      Code:
      'PBCC5.01 program
      
      %name=1
      %cost=2
      FUNCTION PBMAIN
      
      DIM a$(1 TO 5,1 TO 2)
         
      a$(1,%name)="Item1" :a$(1,%cost)="Cost1"
      a$(2,%name)="Item2" :a$(2,%cost)="Cost2"
      a$(3,%name)="Item3" :a$(3,%cost)="Cost3"
      a$(4,%name)="Item4" :a$(4,%cost)="Cost4"
      a$(5,%name)="Item5" :a$(5,%cost)="Cost5"
      
      
      ItemToScanFor$="Item3"
      ARRAY SCAN a$(1,%name) FOR 5 , =ItemToScanFor$, TO index&
      
      IF index& = 0 THEN
          PRINT ItemToScanFor$; " not found."
      ELSE
          PRINT "Cost of ";ItemToScanFor$;" = ";a$(index&,%cost)
      END IF
      
      
      ItemToScanFor$="Cost2"
      ARRAY SCAN a$(1,%cost) FOR 5 , =ItemToScanFor$, TO index&
      
      IF index& = 0 THEN
          PRINT "No item has a cost of ";ItemToScanFor$
      ELSE
          PRINT a$(index&,%name);" has a cost of ";ItemToScanFor$
      END IF
      
      
      WAITKEY$
      END FUNCTION
      Last edited by Paul Dixon; 12 May 2009, 07:28 PM. Reason: Better example

      Comment


      • #4
        Thankyou both for your assistance.
        Paul,
        I modified your code for PBWin as shown below, but this code returns "Cost4" wheras I was expectiing "Cost2". I also note that you are not using any "FOR" element in the ARRAY SCAN.
        Michael,
        I understand all that you reply, but, if I you can post the ARRAY SCAN line you would use in your example to return the index of component1, then I would be "Home and Hosed"!

        Ian B

        Code:
        'PBCC5.01 program
        %Name=1
        %cost=2
        Function PBMain
        
        Dim a$(1 To 5,1 To 2)
        Dim sItemToScanFor As String
        'fill the 2 dimensional array with data   
        a$(1,%Name)="Item1" :a$(1,%cost)="Cost1"
        a$(2,%Name)="Item2" :a$(2,%cost)="Cost2"
        a$(3,%Name)="Item3" :a$(3,%cost)="Cost3"
        a$(4,%Name)="Item4" :a$(4,%cost)="Cost4"
        a$(5,%Name)="Item5" :a$(5,%cost)="Cost5"
        
        
        sItemToScanFor="Item2"
        Array Scan a$(), From 1 To 5, ="Item4", To index&
        
        If index& = 0 Then
            MsgBox sItemToScanFor & " not found."
        Else
            MsgBox "Cost of "  & sItemToScanFor  & " = " & a$(index&,%cost)
        End If
        
        
        
        End Function

        Comment


        • #5
          Ian,
          your ARAY SCAN line is scanning for Item4 so it gets cost4!
          There may have been a cut&paste error somewhere so try this one, already fixed to run in PBWin

          Paul.
          Code:
          'PBWin9 program
          %name=1
          %cost=2
          FUNCTION PBMAIN
          
          DIM a$(1 TO 5,1 TO 2)
          
          a$(1,%name)="Item1" :a$(1,%cost)="Cost1"
          a$(2,%name)="Item2" :a$(2,%cost)="Cost2"
          a$(3,%name)="Item3" :a$(3,%cost)="Cost3"
          a$(4,%name)="Item4" :a$(4,%cost)="Cost4"
          a$(5,%name)="Item5" :a$(5,%cost)="Cost5"
          
          
          ItemToScanFor$="Item3"
          ARRAY SCAN a$(1,%name) FOR 5 , =ItemToScanFor$, TO index&
          
          IF index& = 0 THEN
              MSGBOX ItemToScanFor$ & " not found."
          ELSE
              MSGBOX "Cost of " & ItemToScanFor$ & " = " & a$(index&,%cost)
          END IF
          
          
          ItemToScanFor$="Cost2"
          ARRAY SCAN a$(1,%cost) FOR 5 , =ItemToScanFor$, TO index&
          
          IF index& = 0 THEN
              MSGBOX "No item has a cost of " & ItemToScanFor$
          ELSE
              MSGBOX a$(index&,%name) & " has a cost of " & ItemToScanFor$
          END IF
          
          
          END FUNCTION

          Comment


          • #6
            Thanks Paul.

            That works perfectly.
            I really did wreck the original cut & paste!!

            Thanks again.

            Ian B

            Comment


            • #7
              >ARRAY SCAN a$(1,%name) FOR 5

              Ugh, magic numbers ( = inline numeric literals) !!

              ARRAY SCAN a$(1,%name) FOR UBOUND(A$,%NAME) - LBOUND(A$,%NAME) + 1&

              (also maintenance points)
              Michael Mattias
              Tal Systems Inc. (retired)
              Racine WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                MCM,
                I have to leave him with something to figure out.

                Paul.

                Comment


                • #9
                  >I have to leave him with something to figure out.

                  That's just a generic protest. I HATE magic numbers in code.
                  Michael Mattias
                  Tal Systems Inc. (retired)
                  Racine WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment

                  Working...
                  X