[mdlug] [Discuss] I asked Grok 3 to create a COBOL program
Gib
gibmaxn at gmail.com
Thu Mar 13 16:56:28 EDT 2025
Okay, I think this will work with your example file.
It took me longer to describe what I wanted than for Grok 3 to create
the code.
I've never tried using a pic 9(5) to read data that was left justified and
space filled on the right.
01 INPUT-RECORD.
05 COLUMN-1 PIC 9(5).
05 FILLER PIC XX.
05 COLUMN-2 PIC 9(5).
Input record definition:
- COLUMN-1 uses PIC 9(5) for up to 5 digits
- FILLER PIC XX defines two spaces between columns
- COLUMN-2 uses PIC 9(5) for up to 5 digits
Expected input file format (INPUTVAR.TXT):
12345 67890234 567
12 89012
- Two columns separated by exactly two spaces
- Each number can be 1 to 5 digits (1-99999)
- Numbers should be right-aligned in their 5-digit field
- Each line represents one record
Notes:
- The program assumes numbers are padded with leading zeros or spaces if
less than 5 digits
- Maximum value per number is 99999
- Sums can handle up to 10 digits (9999999999)
- No decimal points allowed
- File must have exactly two spaces between numbers
On Thu, Mar 13, 2025 at 2:44 PM Jim McQuillan <jam at mcquil.com> wrote:
> That still won't work.
>
> The sample file is 1-digit codes with 2 spaces between them.
> The real file could be any number of digits
>
> here's the first 2 lines of my real data file:
>
> 39472 15292
> 41795 28867
>
> See if you can get it to handle any number of digits. or at least 1 digit
> or 5 digit.
> If AI can make COBOL do that, then I'll be impressed.
>
> Jim.
>
>
>
> On Thu, Mar 13, 2025 at 2:03 PM Gib <gibmaxn at gmail.com> wrote:
>
>> Yup. Easy to update the code with a simple request that just took a few
>> seconds. The text that I provided was this:
>>
>> The input file format is two columns consisting of one digit each. Create
>> another version of the source code with input of two single digits as input.
>>
>> The only change was the definition of the input file format:
>>
>> 01 INPUT-RECORD.
>> 05 COLUMN-1 PIC 9.
>> 05 FILLER PIC X.
>> 05 COLUMN-2 PIC 9.
>>
>>
>> On Thu, Mar 13, 2025 at 9:56 AM Jim McQuillan <jam at mcquil.com> wrote:
>>
>>> Gib,
>>>
>>> That's interesting but it's not actually going to work, given the data
>>> files from Advent of code.
>>>
>>> It's expecting the input to look like rows of:
>>> 9999999 9999999
>>>
>>> It would choke on the sample data file:
>>> 3 4
>>> 4 3
>>> 2 5
>>> 1 3
>>> 3 9
>>> 3 3
>>>
>>> With an input record specification of:
>>> 01 INPUT-RECORD.
>>> 05 COLUMN-1 PIC 9(5)V99.
>>> 05 FILLER PIC X.
>>> 05 COLUMN-2 PIC 9(5)V99.
>>>
>>> The sample data would have to look like:
>>> 0000300 0000400
>>> 0000300 0000300
>>> 0000200 0000500
>>> 0000100 0000300
>>> 0000300 0000900
>>> 0000300 0000300
>>>
>>> The difficult thing with COBOL is dealing with data that doesn't
>>> strictly adhere to a rigid format.
>>>
>>> You'd have to use something like 'UNSTRING' to take a string of numbers
>>> delimited by varying amounts of whitespace.
>>>
>>> I think it would be very cool if you could carry on a conversation with
>>> Grok 3, continually refining the specifications until you get something
>>> that actually solves the problem, both for the sample data set and then a
>>> real data set.
>>>
>>> Then... compare how long it took, versus just sitting down and writing
>>> the program yourself.
>>>
>>> My fear is that management is starting to believe that AI is ready to
>>> replace real programmers without realizing that every line of code produced
>>> by AI needs to be looked at very carefully to make sure it's actually doing
>>> the right thing.
>>>
>>> Jim.
>>>
>>>
>>>
>>>
>>> On Thu, Mar 13, 2025 at 10:32 AM Gib <gibmaxn at gmail.com> wrote:
>>>
>>>> The MUG group had a meeting this week with this topic:
>>>>
>>>> There’s a programming website called Advent of Code:
>>>> https://adventofcode.com.
>>>>
>>>> It’s an Advent calendar of 25 small programming puzzles for a variety
>>>> of skill
>>>> sets and levels that can be solved in any programming language you like.
>>>>
>>>> You can see the first puzzle at https://adventofcode.com/2024/day/1.
>>>>
>>>> I asked Grok 3 to create a COBOL program.
>>>> This took just a few seconds.
>>>>
>>>> Here was the prompt:
>>>> Create the source code in COBOL to read a file that contains two
>>>> columns of numbers and displays the difference of the sums of the two
>>>> columns.
>>>>
>>>> The result:
>>>>
>>>> Here's a COBOL program that reads a file with two columns of numbers
>>>> and calculates the difference between their sums:
>>>>
>>>> cobol
>>>>
>>>> IDENTIFICATION DIVISION. PROGRAM-ID. COLUMN-DIFFERENCE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT INPUT-FILE ASSIGN TO "INPUTNUM.TXT" ORGANIZATION IS LINE SEQUENTIAL. DATA DIVISION. FILE SECTION. FD INPUT-FILE. 01 INPUT-RECORD. 05 COLUMN-1 PIC 9(5)V99. 05 FILLER PIC X. 05 COLUMN-2 PIC 9(5)V99. WORKING-STORAGE SECTION. 01 SUMS. 05 SUM-COL-1 PIC 9(7)V99 VALUE 0. 05 SUM-COL-2 PIC 9(7)V99 VALUE 0. 05 DIFFERENCE PIC S9(7)V99 VALUE 0. 01 EOF-FLAG PIC X VALUE "N". 88 END-OF-FILE VALUE "Y". PROCEDURE DIVISION. MAIN-LOGIC. PERFORM INITIALIZE-PROGRAM PERFORM PROCESS-FILE PERFORM CALCULATE-AND-DISPLAY PERFORM TERMINATE-PROGRAM STOP RUN. INITIALIZE-PROGRAM. OPEN INPUT INPUT-FILE DISPLAY "Processing file...". PROCESS-FILE. PERFORM UNTIL END-OF-FILE READ INPUT-FILE AT END SET END-OF-FILE TO TRUE NOT AT END ADD COLUMN-1 TO SUM-COL-1 ADD COLUMN-2 TO SUM-COL-2 END-READ END-PERFORM. CALCULATE-AND-DISPLAY. COMPUTE DIFFERENCE = SUM-COL-1 - SUM-COL-2 DISPLAY "Sum of Column 1: " SUM-COL-1 DISPLAY "Sum of Column 2: " SUM-COL-2 DISPLAY "Difference (Col1 - Col2): " DIFFERENCE. TERMINATE-PROGRAM. CLOSE INPUT-FILE DISPLAY "Processing complete.".
>>>>
>>>> This COBOL program:
>>>>
>>>> 1. Defines an input file "INPUTNUM.TXT" with two columns of numbers
>>>> (each 5 digits with 2 decimal places)
>>>> 2. Creates variables to store the sums and difference
>>>> 3. Opens and reads the file line by line
>>>> 4. Accumulates sums for each column
>>>> 5. Calculates the difference between the sums
>>>> 6. Displays the results
>>>>
>>>> The expected input file format (INPUTNUM.TXT) should look like this:
>>>>
>>>> 12345.67 89012.3423456.78 34567.89
>>>> 34567.89 12345.67
>>>>
>>>>
>>>> - Two columns separated by a space
>>>> - Numbers can include decimals
>>>> - Each line represents one record
>>>>
>>>> Key features:
>>>>
>>>> - Uses LINE SEQUENTIAL organization for simple text file reading
>>>> - Includes error handling for end-of-file
>>>> - Uses PIC clauses to define number formats
>>>> - Displays formatted output
>>>>
>>>> To use this program:
>>>>
>>>> 1. Create a text file named "INPUTNUM.TXT" with your numbers
>>>> 2. Compile and run the program
>>>> 3. Check the output for the sums and difference
>>>>
>>>> The program assumes:
>>>>
>>>> - Numbers are non-negative
>>>> - Input file exists in the same directory
>>>> - Numbers fit within the defined picture clauses (7 digits total, 2
>>>> after decimal)
>>>>
>>>> _______________________________________________
>>>> Sponsors:
>>>> http://a2hosting.com a2hosting - proudly providing web hosting
>>>> services for MUG.org
>>>> http://altair.com Altair - Altair transforms products and orgs via
>>>> simulation, optimization and HPC
>>>> http://www.avairis.com Avairis, Inc. - Web based medical practice
>>>> management systems
>>>> http://www.penguicon.org Penguicon - Annual convention for Linux,
>>>> Science fiction and more
>>>>
>>>> Discuss mailing list
>>>> http://mug.org/mailman/listinfo/discuss_mug.org
>>>>
>>> _______________________________________________
>>> Sponsors:
>>> http://a2hosting.com a2hosting - proudly providing web hosting services
>>> for MUG.org
>>> http://altair.com Altair - Altair transforms products and orgs via
>>> simulation, optimization and HPC
>>> http://www.avairis.com Avairis, Inc. - Web based medical practice
>>> management systems
>>> http://www.penguicon.org Penguicon - Annual convention for Linux,
>>> Science fiction and more
>>>
>>> Discuss mailing list
>>> http://mug.org/mailman/listinfo/discuss_mug.org
>>>
>> _______________________________________________
>> Sponsors:
>> http://a2hosting.com a2hosting - proudly providing web hosting services
>> for MUG.org
>> http://altair.com Altair - Altair transforms products and orgs via
>> simulation, optimization and HPC
>> http://www.avairis.com Avairis, Inc. - Web based medical practice
>> management systems
>> http://www.penguicon.org Penguicon - Annual convention for Linux,
>> Science fiction and more
>>
>> Discuss mailing list
>> http://mug.org/mailman/listinfo/discuss_mug.org
>>
> _______________________________________________
> Sponsors:
> http://a2hosting.com a2hosting - proudly providing web hosting services
> for MUG.org
> http://altair.com Altair - Altair transforms products and orgs via
> simulation, optimization and HPC
> http://www.avairis.com Avairis, Inc. - Web based medical practice
> management systems
> http://www.penguicon.org Penguicon - Annual convention for Linux, Science
> fiction and more
>
> Discuss mailing list
> http://mug.org/mailman/listinfo/discuss_mug.org
>
More information about the mdlug
mailing list