Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting Pic 9 to Pic X for COBOL/400 3

Status
Not open for further replies.

bitsandbits

Technical User
Oct 15, 2002
4
US
Hello,

I couldn't find this in the FAQ or Archives, and I'm new to COBOL (been learning on the job for two months). If this has already been asked, my apologies.

I am looking to convert a PIC 9 field VALUE 55 to PIC X, resulting in the printing of 55 asterisks. Sounds simple, but I am baffled on the conversion. Any help you can give is greatly appreciated.

Bitsandbits
 
I'm not sure exactly what you're trying to do. Sounds like you're given an integer (PIC 99 at least to hold 55) and want to print out a variable number of asterisks on a line depending on that value? That's a normal thing to do when printing a histogram for instance.

Here's one cut at solving that problem:

Code:
01  WS-VALUE      PIC 99 VALUE 55.
01  WS-PRINT-LINE PIC X(132).
01  SUB           PIC S9(4) COMP.
...
MOVE SPACES TO WS-PRINT-LINE
PERFORM VARYING SUB FROM 1 BY 1 UNTIL
                SUB > WS-VALUE
    MOVE "*"           TO WS-PRINT-LINE(SUB:1)
END-PERFORM
WRITE OUTPUT-RECORD FROM WS-PRINT-LINE
.
[\CODE]
 
I'm not sure if this will help but ....

Data definitition in Working Storage

05 Data.
10 Data-9 Pic 9(55).

In the Procedure Division

MOVE ALL '*' TO DATA.

Data now contains 55 *'s.

In effect you have two definitions of the same data, alphanumeric at the group level (Data) and numeric at the elementary level (Data-9).
 
3gm and gbell,

Wow! Thanks for the great posts. You solved my problem in a matter of minutes.

As I said, I've only been working with COBOL for two months or so, and haven't encountered a histogram until now. You two saved the day, and the boss will be happy!

Thanks again,

bitsandbits
 
Well, not to be too critical, but the PIC 9(55) will not work, for reasons that should be obvious.

Another approach which obviates the need for a PERFORM loop is shown. I am assuming the need to print 0-99 asterisks (i.e. a percentage histogram).
Code:
01  PRINT-LINE  PIC X(132).
....
01  STAR-LINE-UP  PIC X(99) VALUE ALL "*".
01  STARS-NEEDED  PIC 9(2).
...
    IF STARS-NEEDED NOT = 0
        MOVE STAR-LINE-UP (1: STARS-NEEDED) TO PRINT-LINE
    ELSE
        MOVE SPACES TO PRINT-LINE
    END-IF

An older variant of this would be to use a variable group, but the reference modification approach is cleaner, IMO. Tom Morrison
 
k5tm,

Thanks for the heads up on the PIC 9(55). I'll get the hang of this "handing out stars" thing yet.

Your idea worked. Thank you.

Dave Kibler
Bitsandbits
 
This isnt that hard. Print the stars like this:

USE A FIELD CALLED STAR-COUNT PIC 9(03).
USE A FIELD CALLED LINE-GRAPH PIC X(100).
USE A FIELD CALLED ONE-STAR PIC X(02) VALUE "* ".

MOVE SPACES TO LINE-GRAPH.
PERFORM GRAPH STAR-COUNT TIMES.

GRAPH.
STRING
LINE-GRAPH DELIMITED BY " "
ONE-STAR DELIMITED BY " "
INTO LINE-GRAPH
END-STRING.

YOU GOTTA LOVE IT.

This is only one way to do it.

String is not used that commonly in COBOL.

You can make a table and a loop and stop the subscripting or use varying depending on the value of STAR-COUNT.

01 LINE-GRAPH-TABLE
GRAPH-LOC OCCURS 100 TIMES INDEXED BY STR-IDX
PIC X(01).

SET STR-IDX TO 1.

PERFORM GRAPH-STARS UNTIL STR-IDX > STAR-COUNT
OR STR-IDX = 100.

GRAPH-STARS.
MOVE "*" TO GRAPH-LOC (STR-IDX).
SET STR-IDX UP BY 1.

Might be easier to do this with a search routine. If you do not like my post feel free to point out your opinion or my errors.
 
ceh4702,

Your approach will work, but you gotta go w/Tom's (k5tm) solution: no perf loop, easier to read, probably more efficient. Especially if STARS-NEEDED is COMP or COMP-3 (I'd have to check the generated BAL code). Don't know if that matters on a PC, I know it does on the mainframe.

Regards, Jack.
 
How to build in a subfile in COBOL/400 ? How to build a windown subfile?
 
For subfiles in COBOL/400 you may want to purchase a book for COBOL400 off of


I have the first edition of this at home. It is a pretty good book. The book is titled Programming in COBOL/400 by James Cooper and Stern & Stern.

I am guessing that a subfile is a logical file. With logical file descriptions you can either combine 2 files, or parts of them or possibly just describe one file and use a subset of the fields in the file. This is possible because on the AS400 the files are treated as a database. Files can be objects with sub-files inside of them also. This is a unique aspect of the AS400.





If you do not like my post feel free to point out your opinion or my errors.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top