ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

COBOL String Scan

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

  • COBOL String Scan

    I wish COBOL had an intrinsic function like RPG's %SCAN. QCLSCAN has a too many parameters. This is the best I could come up. Anybody got anything better?

    Look for a decimal point in X-Value.

    Code:
    01  X-Value        pic x(10).         
    01  P-Ndx          pic s9(4)   binary.           
    
    move zero to P-Ndx               
    inspect X-Value tallying P-Ndx   
            for characters before '.'
    add 1 to P-Ndx                   
    if P-Ndx > length of X-Value 
        move zero to P-Ndx       
    
    end-if

  • #2
    Re: COBOL String Scan

    What about using SQL scalar Functions such as LOCATE?
    Syntax Locate(Search, String, Start) - 3rd parameter is optional - Returns the postition of the first occurence.

    Exec SQL set :Pos = Locate('X', :MyString, :StrPos)
    End-Exec.

    Birgitta

    Comment


    • #3
      Re: COBOL String Scan

      A function version of INSPECT would be nice. Then again, it'd sometimes be nice if %scan() did everything INSPECT can do. Win some; lose some.

      In general, I'd do it similar to what you've done. One minor change: I'd possibly initialize P-Ndx to (1) rather than zero if you always want to indicate the position of the decimal point, and then remove the ADD statement. Maybe, maybe not.
      Tom

      There are only two hard things in Computer Science: cache invalidation, naming things and off-by-one errors.

      Why is it that all of the instruments seeking intelligent life in the universe are pointed away from Earth?

      Comment


      • #4
        Re: COBOL String Scan

        Originally posted by B.Hauser View Post
        Exec SQL set :Pos = Locate('X', :MyString, :StrPos)
        End-Exec.
        Good idea, Birgitta.

        Comment


        • #5
          Re: COBOL String Scan

          Originally posted by tomliotta View Post
          One minor change: I'd possibly initialize P-Ndx to (1) rather than zero if you always want to indicate the position of the decimal point, and then remove the ADD statement.
          I like that minor change, Tom. I'm so used to setting the counter variable to zero before inspecting, it didn't occur to me to set it to 1.

          Comment

          Working...
          X