Menu

String CONTAINS missing?

2024-12-21
2024-12-21
  • Randall Nagy

    Randall Nagy - 2024-12-21

    Whilist cobc --list-reservedlist CONTAINS as supported, and VsCode does as well, we're at a loss as how to use it. Copilot, too:

       Search-By-Author.
           OPEN INPUT QuoteFile
           DISPLAY "Enter Author to search: "
           ACCEPT SearchAuthor
           PERFORM UNTIL AT-EOF
               READ QuoteFile INTO QuoteRecord
                   AT END MOVE 1 TO WS-EOF
                   NOT AT END
                       IF QuoteAuthor CONTAINS SearchAuthor
                           DISPLAY "ID: " QuoteID
                           DISPLAY "Author: " QuoteAuthor
                           DISPLAY "Quote: " QuoteText
                           DISPLAY ""
                       END-IF
               END-READ
           END-PERFORM
           CLOSE QuoteFile.
    

    Any ideas on how to enabile string-support for CONTAINS? The syntax railroads seem to be missing a demo, as well.

     

    Last edit: Randall Nagy 2024-12-21
    • Simon Sobisch

      Simon Sobisch - 2024-12-21

      Nearly all AI models are not usable with COBOL questions, as the training days they had didn't include much COBOL "knowledge". You can see this on basic questions like these, which every COBOL developer could answer after some days of training (no offense for you - but for AI models).

      There are "specialized" (commonly fine tuned) models that can make this better (and I'm quite sure IBMs would be different), but I highly suggest to do what developers did since the language was created:
      Check COBOL compiler documentation and free documentation like the programmers guide instead for questions like: what is CONTAINS used for. And/or read cobc/parser.y which commonly gives a good overwrite (where does the compiler accepts that token).

      Back to your question: if you want to use that directly in a condition, you'd have to write a user defined function. The common war would be to use an INSPECT ... TALLYING, if you want to ignore the case, then use FUNCTION LOWER() - once for the searched word, and directly in the INSPECT of your check.

      ... and in a real world application with thousands of elements, you would create a separate ISAM file with applicable foreign keys that reference the book - with the authors split and already lowercased (or possibly better, their phonetic value stored to catch different but similar versions - again all depending on the use case).

       
  • Randall Nagy

    Randall Nagy - 2024-12-21

    Thanks.

    Survey said:

    > Generative: 'how to determine if a string is part of another in GNU COBOL
    > without using the CONTAINS reserved word.'

    IDENTIFICATION DIVISION.
    PROGRAM-ID. SubstringCheck.
    
    DATA DIVISION.
    WORKING-STORAGE SECTION.
    01 main-string       PIC X(100) VALUE 'GNU COBOL is great for learning COBOL programming.'.
    01 search-string     PIC X(20) VALUE 'COBOL'.
    01 search-length     PIC 9(02).
    01 main-length       PIC 9(03).
    01 found-flag        PIC X VALUE 'N'.
    01 remaining-string  PIC X(100).
    
    PROCEDURE DIVISION.
    MAIN-PROCEDURE.
    
        MOVE LENGTH OF search-string TO search-length
        MOVE LENGTH OF main-string TO main-length
    
        PERFORM CHECK-SUBSTRING VARYING main-length FROM 1 BY 1
            UNTIL main-length < search-length OR found-flag = 'Y'
    
        IF found-flag = 'Y'
            DISPLAY "Substring found."
        ELSE
            DISPLAY "Substring not found."
    
        STOP RUN.
    
    CHECK-SUBSTRING.
        MOVE SPACE TO remaining-string
        MOVE main-string(main-length:) TO remaining-string
    
        INSPECT remaining-string TALLYING main-length FOR LEADING SPACES
    
        IF main-length = 0
            UNSTRING remaining-string DELIMITED BY SPACE INTO remaining-string
            IF remaining-string(1:search-length) = search-string
                MOVE 'Y' TO found-flag
            END-IF
        END-IF.
    
     
  • Randall Nagy

    Randall Nagy - 2024-12-21

    This was fun, too:

    > Generative: 'create a callable procedure designed to determine if a string
    > is part of another in GNU COBOL without using the CONTAINS reserved word.
    *> Include parameters into the linkage section.'
    IDENTIFICATION DIVISION.
    PROGRAM-ID. SubstringCheck.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  ws-main-string      PIC X(100).
       01  ws-search-string    PIC X(20).
       01  ws-found-flag       PIC X VALUE 'N'.
    
       LINKAGE SECTION.
       01  main-string         PIC X(100).
       01  search-string       PIC X(20).
       01  found-flag          PIC X.
    
       PROCEDURE DIVISION USING main-string search-string found-flag.
       MAIN-PROCEDURE.
    
           MOVE LENGTH OF search-string TO ws-search-length
           MOVE LENGTH OF main-string TO ws-main-length
    
           PERFORM CHECK-SUBSTRING VARYING ws-index FROM 1 BY 1
               UNTIL ws-index > ws-main-length - ws-search-length + 1 OR ws-found-flag = 'Y'
    
           MOVE ws-found-flag TO found-flag
    
           GOBACK.
    
       CHECK-SUBSTRING.
           MOVE SPACE TO ws-temp-string
           MOVE main-string(ws-index:) TO ws-temp-string
    
           IF ws-temp-string(1:ws-search-length) = search-string
               MOVE 'Y' TO ws-found-flag
           END-IF.
    
     
  • Randall Nagy

    Randall Nagy - 2024-12-21

    Also: If support for one reservered is A.W.O.L, one wonders how many more ... ?

    Anyone keeping a list they'd like to share?

     

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.