Announcement

Collapse
No announcement yet.

Array scanning

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

  • Array scanning

    Hi,

    I'm starting to explore the use of arrays in PB. I'm passing a numeric argument to a subroutine which has a string array. I'd like to look through the array for the string equivalent of that argument and return the full array element. Just don't know how. So in the code example below, if I passed 110 in the sp variable, it would return the string " 110 Spam".

    Example:

    SUB killem(filepath AS STRING, sp AS BYTE, inputline AS STRING)
    DIM res AS STRING
    DIM rsns{14)
    ARRAY ASSIGN rsns() =" 10 extortion attempt" ," 20 Translator Con" ," 30 had spammy dupe lines." ," 40 Fraud Attempt" ," 50 bogus TLD" ," 60 extortion attempt" ," 70 Spam" ," 80 Hacking" ," 90 Sex Spam" ," 100 Spam "
    rsns(10)=" 110 Spam" : rsns(11) =" 120 had IP-based URL." : rsns(12)=" 130 had crap string indicating spammer.": rsns(13)=" 140 Mailgun Spam"

    res$=<something using sp and the rsns array>

    END SUB

    CALL killem(fpath$, 30, emsaved$)


    There likely are better ways to do this. Open to suggestions.
    Thanks!

    --Ben

  • #2
    A SUB procedure and a subroutine are two different things. Subroutines are embedded in a procedure and are invoked with GOSUB...RETURN. You can't pass parameters to them, they can use whatever variables are available in their parent procedure.

    A SUB procedure is declared with "SUB ....." and invoked by name, either with or without the keyword "CALL"

    You appear to be passing to as SUB procedure

    SUB s don't "return" values. If you want to get values from the SUB, you need to pass variables BYREF to the SUB and modify them in the SUB. You can then use the modified values of the passed parameters after the SUB has ended.

    But iIt's usually a better idea to use a FUNCTION rather than a SUB if you just want a single return value.

    '
    Code:
    FUNCTION PBMAIN() AS LONG
      ? Killem(10)
      ? Killem(120)
      ? Killem (300)
    END FUNCTION
    
    FUNCTION killem(sp AS LONG) AS STRING
    DIM strReasons(1 TO 14) AS STRING
    LOCAL strNum, strResult AS STRING
    LOCAL idx AS LONG
    ARRAY ASSIGN strReasons() =" 10 extortion attempt" ," 20 Translator Con" ," 30 had spammy dupe lines." ," 40 Fraud Attempt" ," 50 bogus TLD" , _
                 " 60 extortion attempt" ," 70 Spam" ," 80 Hacking" ," 90 Sex Spam" ," 100 Spam ", _
                 " 110 Spam" ," 120 had IP-based URL."," 130 had crap string indicating spammer.", " 140 Mailgun Spam"
    
    strNum = LEFT$(STR$(sp) & " ",4)
    
    ARRAY SCAN strReasons(), FROM 1 TO 4,  =strNum, TO idx ' find a matching 4 characters at the start of an array element
    
    IF idx > 0 THEN
        FUNCTION = strReasons(idx)
    ELSE
        FUNCTION = "Code Not found"
    END IF
    END FUNCTION
    '

    Comment


    • #3
      It has been a very long time, but neither post strikes me as PB For DOS.
      Dale

      Comment


      • #4
        Originally posted by Dale Yarker View Post
        It has been a very long time, but neither post strikes me as PB For DOS.
        OOOPS!

        Mea Culpa!

        Didn't notice the area, does PB for DOS have ARRAY ASSIGN?

        Comment


        • #5
          does PB for DOS have ARRAY ASSIGN?
          - Introduced with PBCC4 I believe
          Rgds, Dave

          Comment


          • #6
            Originally posted by Stuart McLachlan View Post

            OOOPS!

            Mea Culpa!

            Didn't notice the area, does PB for DOS have ARRAY ASSIGN?
            In a sideways kind of way it is better. Now you've given a good answer to OP if Dave is correct, the whole thing started in wrong place.

            Dale

            Comment


            • #7
              PB for DOS do have ARRAY SORT statement. Do not have ARRAY ASSIGN.
              Last edited by Manuel Valdes; 29 Jan 2021, 06:36 PM.

              Comment


              • #8
                To address the question given it is PB/DOS:

                If you are looking for the string equivalent of a number is the first two positions of the string elements of an array, you may use the FROM...TO option of ARRAY SCAN as in"



                Code:
                  ,,,,
                  NumofInterest%  = 20
                  ARRAY SCAN rsns() FROM 1 to 2, = STR$(NumOfInterest) to iHit%
                  IF Ihit% THEN
                      TextReason$  = LTRIM$(MID$(Rsns(Ihit%),3) )   ' see note re one-based array
                 ELSE                  ' not found in rsns table
                      TextReason$ =  "Reason Code not found for  " & STR$(NumOfInterest%)
                END IF
                  ...
                IF rsns() is not one- based, you will have to adjust the subscript of rsns() corresponding to the hit.

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

                Comment


                • #9
                  Originally posted by Michael Mattias View Post
                  If you are looking for the string equivalent of a number is the first two positions of the string elements of an array, you may use the FROM...TO option of ARRAY SCAN as in"
                  Code:
                  ,,,,
                  NumofInterest% = 20
                  ARRAY SCAN rsns() FROM 1 to 2, = STR$(NumOfInterest) to iHit%
                  IF Ihit% THEN
                  TextReason$ = LTRIM$(MID$(Rsns(Ihit%),3) ) ' see note re one-based array
                  ELSE ' not found in rsns table
                  TextReason$ = "Reason Code not found for " & STR$(NumOfInterest%)
                  END IF
                  ...
                  That doesn't work for a number of reasons

                  1. STR$(20) is 3 characters, not two - it has a leading space and will never match a number "in the first ... positions of the string elements"
                  (fortunately the OP's strings do start with a space, but you need FROM 1 to 3 to match STR$(20)
                  2. The OP also has 3 digit numbers, which need 4 characters.
                  3. The fourth character with 2 digit numbers is a space.

                  An actual workable solution was already provided in post #2
                  Code:
                  strNum = LEFT$(STR$(sp) & " ",4)
                  [FONT=Courier New]ARRAY SCAN strReasons(), FROM 1 TO 4, =strNum, TO idx ' find a matching 4 characters at the start of an array element[/FONT]

                  Comment


                  • #10
                    An actual workable solution was already provided in post #2
                    The omissions of the "different lengths" was deliberate, but repeating the "use FROM .. TO with ARRAY SCAN" was not... that is, I totally missed the same solution in Post #2.

                    I have been missing lots of stuff like this of late. Birthdays. Can't live without em, huh?
                    Michael Mattias
                    Tal Systems (retired)
                    Port Washington WI USA
                    [email protected]
                    http://www.talsystems.com

                    Comment

                    Working...
                    X