Add leading zeros by using cobol

This is a Mainframe COBOL forum - you can post your queries on Mainframe COBOL, VS COBOL II, COBOL/370 , Enterprise COBOL

Moderators: dbzTHEdinosauer, Moderator Group

venky
Active Member
Posts: 50
Joined: Sat May 12, 2012 10:13 pm

Add leading zeros by using cobol

Post by venky » Sat Jul 14, 2012 8:06 am

hi,

I have a file contains numeric data from 1st position to 9th position. Please see below.

input file:

2
23
456
4256
26879
236984
2457896
23696352
369874544

the maximum length of the numeric data is 9. Now I need to add leading zeros to the above data. The output should get like as I mentioned below.

Output:

000000002
000000023
000000456
000004256
000026879
000236984
002457896
023696352
369874544

Please look into this and should be done only by using cobol.

Regards,
Venky.

DikDude
Moderator
Posts: 1001
Joined: Fri Jul 22, 2011 8:39 am
Location: usa

Post by DikDude » Sat Jul 14, 2012 8:57 am

Use NUMVAL (if you are certain the field will have only valid numbers).
Have a good one

venky
Active Member
Posts: 50
Joined: Sat May 12, 2012 10:13 pm

Post by venky » Sat Jul 14, 2012 6:19 pm

Thanks DikDude,

Can you please give me a example for the file which I have written.

Regards,
Venky.

NicC
Active Member
Posts: 650
Joined: Sun Jul 24, 2011 5:27 pm
Location: Down on the pig farm

Post by NicC » Sat Jul 14, 2012 6:36 pm

Why not teach yourself? Look it up in the manual and play until you get the hang of it.
Regards
Nic

User avatar
dbzTHEdinosauer
Moderator
Posts: 981
Joined: Mon Oct 02, 2006 8:31 pm

Post by dbzTHEdinosauer » Sat Jul 14, 2012 6:40 pm

Dick Brenholtz
JCL, SQL and code in programs have an irritating habit of doing what you say,
not what you meant.

venky
Active Member
Posts: 50
Joined: Sat May 12, 2012 10:13 pm

Post by venky » Mon Jul 23, 2012 8:14 pm

hi,

Thanks Numval is working, but I have one more question see below.

when there is space in between the row, the numval is notworking. Please see the below input file and suggest how to Ignore the space and remaining data should add leading zeros.

Inputfile:

2
23
456
4256
26879

236984
2457896
23696352
369874544


Thanks,
Venky.

DikDude
Moderator
Posts: 1001
Joined: Fri Jul 22, 2011 8:39 am
Location: usa

Post by DikDude » Mon Jul 23, 2012 8:26 pm

If the data contains spaces, skip the record or use some default value - it depends on the requirement of the process.

NUMVAL will only work when the data is a valid numeric.
Have a good one

venky
Active Member
Posts: 50
Joined: Sat May 12, 2012 10:13 pm

Post by venky » Mon Jul 23, 2012 8:57 pm

Thank you,

okay you are saying that I need to skip the record or pass default value,
but the spaces will be located at different rows in particular one column, It is difficult to find the spaces to skip the space records.

Is there is any other logic to Ignore the spaces and add leading zeros to other remaining values. I mean other then Numval???

Thanks once again.

Venky.

DikDude
Moderator
Posts: 1001
Joined: Fri Jul 22, 2011 8:39 am
Location: usa

Post by DikDude » Mon Jul 23, 2012 9:10 pm

You are writing a program. . .

If you want something done, you need to code for it. There are no magic bullets that will "skip over things".
It is difficult to find the spaces to skip the space records.
I must be missing something. . . How can it be be difficult to code:
IF THE-FIELD = SPACES
whatever action.
Have a good one

venky
Active Member
Posts: 50
Joined: Sat May 12, 2012 10:13 pm

Post by venky » Mon Aug 06, 2012 7:44 pm

hi,

without using Compute, can you provide me a logic for how to add leading zeros for the given file. Thank you in Advance.

Regards,
Venky.

DikDude
Moderator
Posts: 1001
Joined: Fri Jul 22, 2011 8:39 am
Location: usa

Post by DikDude » Tue Aug 07, 2012 12:48 am

Why are you against COMPUTE? Why even mention COMPUTE - it is not needed for this.

All you should need is to move the value to an output field that is not leading-zero suppressed (i.e. my-non-zero-field pic 9(9).) - unless there is more you have not posted. . .
Last edited by DikDude on Tue Aug 07, 2012 9:24 pm, edited 1 time in total.
Have a good one

Anuj Dhawan
Moderator
Posts: 1625
Joined: Sat Aug 09, 2008 9:02 am
Location: Mumbai, India

Post by Anuj Dhawan » Tue Aug 07, 2012 12:33 pm

Why don't you try something instead of just asking, venky?
Regards,
Anuj

User avatar
Gurugars
Active Member
Posts: 107
Joined: Sat Oct 23, 2010 2:17 pm
Location: Chennai,India.

Post by Gurugars » Tue Aug 14, 2012 3:03 pm

Venky,

Please let us know how exactly your input will be? and how exactly want the output to be?

Until unless getting the clear picture of your problem no one can provide the solution for that!
Guru:-)

You're never fully dressed without a smile :)

venky
Active Member
Posts: 50
Joined: Sat May 12, 2012 10:13 pm

Post by venky » Tue Aug 14, 2012 6:12 pm

Hey Guru,

I have mentioned in my first post about input and also output. Please take a look.

I need leading zeros should be added to the input by using cobol(without compute).

Thanks.
Regards,
Venky.

DikDude
Moderator
Posts: 1001
Joined: Fri Jul 22, 2011 8:39 am
Location: usa

Post by DikDude » Tue Aug 14, 2012 9:12 pm

It is hard to believe you do not have this resolved after so long . . .

Did you do as suggested and move the input field to a field defined as pic 9(9)? If not why not. . . :?

Did you compare the input for blanks and if blank, move zero to it? Again, if not, why not :(
Have a good one

Post Reply

FREE TUTORIALS

Tutorials
Free tutorials from mainframegurukul
  • JCL Tutorial
    Covers all important JCL concepts.
  • Cobol Tutorial
    This tutorials covers all Cobol Topics from STRING to COMP-3.
  • DB2 Tutorial
    DB2 Tutorial focuses on DB2 COBOL Programming.
  • SORT Tutorial
    This Tutorial covers all important aspects of DFSORT with examples
  • CICS Tutorial
    This CICS tutorial covers CICS concepts and CICS Basics, CICS COBOL Programming.
Interview
Mainframe Interview questions



Other References
Mainframe Tools and others