# Concatenating Numeric values by removing leading zeroes

Author Message
aryanpa1

New User

Joined: 26 May 2007
Posts: 45
Location: Chennai

 Posted: Wed Sep 09, 2009 5:54 pm Hi Team , We have 4 fields defined as s9(9) comp. We need to concatenate all the 4 fields(into X(100) field) by removing leading zeroes. Below is the approach we are following. 1)Replace leading zeroes by Spaces using inspect. 2)String all the four fields. 3)Read each byte of concatenated string and move it to the final field where that byte is greater than spaces. Sample data : 000000123 (S9(9) comp) 000000456 000000789 000000000 1) ______123 (X(9)) ( '_' represent Space here) ______456 ______789 2)______123______456______789____________ 3)123456789 (X(100)) Can you please let me know if there is any easy to do this. Please let me know if my requirement is confusing.
rockish

Active User

Joined: 05 Jun 2009
Posts: 185
Location: Planet Earth

 Posted: Wed Sep 09, 2009 6:00 pm Can you pls clarify on your third step ? What command do you use to read and process the concatenated string byte by byte ?
Robert Sample

Global Moderator

Joined: 06 Jun 2008
Posts: 8651
Location: Dubuque, Iowa, USA

 Posted: Wed Sep 09, 2009 6:00 pm Do you know how COBOL stores a PIC S9(09) COMP field? Do you know why COBOL does not allow you to use the IF NUMERIC test on a COMP field? Answering these questions will help you understand why your INSPECT statement is going to fail to find leading zeroes in the COMP fields, and why you will wind up with very ... interesting ... output variables.
aryanpa1

New User

Joined: 26 May 2007
Posts: 45
Location: Chennai

Posted: Wed Sep 09, 2009 6:09 pm

 Quote: Can you pls clarify on your third step ? What command do you use to read and process the concatenated string byte by byte ?

We are reading each byte , if it is greater than Space we will move it to Output field. Here is hw it works

if Field1(Cnt1) > Spaces Move it to field2(CNT2)

" Here Field1 and Field2 are declared as Arrays."
aryanpa1

New User

Joined: 26 May 2007
Posts: 45
Location: Chennai

Posted: Wed Sep 09, 2009 6:12 pm

 Quote: Answering these questions will help you understand why your INSPECT statement is going to fail to find leading zeroes in the COMP fields

Hi Robert ,

This logic is already existing , as per logic, Comp field is moved to Alphanumeric and then Inspect statement is used on Alphanumeric field.
Craq Giegerich

Senior Member

Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

 Posted: Wed Sep 09, 2009 6:20 pm Robert, I think your comments went way over the posters head. I think a remedial course is Introduction to Computers and a remedial course in Introduction to Cobol is what is needed.
Robert Sample

Global Moderator

Joined: 06 Jun 2008
Posts: 8651
Location: Dubuque, Iowa, USA

 Posted: Wed Sep 09, 2009 6:21 pm Arrays or reference modification are the only ways I know to do this -- and neither is going to be much different from the other as far as processing requirements.
GuyC

Senior Member

Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

Posted: Wed Sep 09, 2009 6:37 pm

Maybe something like this :
 Code: 01 ws-fields.    03 num1 pic s9(9) comp.    03 num2 pic s9(9) comp.    03 num3 pic s9(9) comp.    03 ed1.       07 ednum1 pic Z(9).    03 ed2.       07 ednum1 pic Z(9).    03 ed3.       07 ednum3 pic Z(9).    03 outrev    pic x(27).    03 outputfld pic x(27). move num1 to ednum1. move num2 to ednum2. move num3 to ednum3. move spaces to outrev. string function reverse(ednum3) delimited by ' '        function  reverse(ednum3) delimited by ' '        function reverse(ednum3) delimited by ' ' into outrev. move function reverse(outrev) to outputfld.

but it won't be faster then using reference modification
mmwife

Super Moderator

Joined: 30 May 2003
Posts: 1592

 Posted: Fri Sep 11, 2009 4:09 am Guy, It looks like your moves to the numed flds will convert the data to decimal. If I understand the intent of the OP, he wants to retain the hex values of the orig data. I haven't tested, what say you?
Bill O'Boyle

CICS Moderator

Joined: 14 Jan 2008
Posts: 2501
Location: Atlanta, Georgia, USA

 Posted: Fri Sep 11, 2009 8:15 am Jack has the requirement down, so you would move each fullword to a work-fullword, which is redefined as a PIC X(04). Then, using reference modification, check each byte, beginning at byte-01 for LOW-VALUE. If true, then keep looping and checking until the "next" byte is NOT LOW-VALUE. You would then move the value, beginning at the non LOW-VALUE byte, into the PIC X(100) field, at byte-01, calculating the length of the data and positioning yourself at the "next" byte in the PIC X(100) field. Do this three more times and you're done. It might be a good idea to initialize the PIC X(100) to LOW-VALUES before you begin. Your result in the 100-byte field will then be a left-justified value padded with low-order LOW-VALUES, which is what I believe you want. Bill
GuyC

Senior Member

Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

Posted: Fri Sep 11, 2009 12:12 pm

 mmwife wrote: Guy, It looks like your moves to the numed flds will convert the data to decimal. If I understand the intent of the OP, he wants to retain the hex values of the orig data. I haven't tested, what say you?

I guess I misinterpreted the requirement.
Although the initial post mentions : 000000123 (S9(9) comp) ==> '______123' X(9) where _ means spaces
This doesn't look like he's talking about hexadecimal values.

If this is the requirement then you have to work with half bytes and is Bills solution not enough.

the term "decimal" is unclear.
Zoned decimal is mainframe talk for x'F1F2F3F4..'
but decimal can also mean packed decimal : x'01234C' (see DB2)

my code moves the numeric value to an edited zone decimal field.
dick scherrer

Moderator Emeritus

Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

Posted: Fri Sep 11, 2009 8:04 pm

Hello,

 Quote: Please let me know if my requirement is confusing.
I believe the differences in the replies demonstrates the requirement is not clear. . .

Given that the individual digits cannot be seen in a binary field, i don't believe the "parsing" of the 4 bytes 1 byte at a time will yield the desired numeric values.

I'm not sure why this is given:
 Code: Sample data : 000000123 (S9(9) comp) 000000456 000000789 000000000
Those are not the contents of a binary field (except the last one ). Might be the values, but not the content.

If the "input" was posted using HEX ON, and the oujtput needed from that input was posted, it would help us help you.
mmwife

Super Moderator

Joined: 30 May 2003
Posts: 1592

 Posted: Tue Sep 15, 2009 5:03 am It looks like Pavan has left the building. Playing off of Dick's observstion, the I/P fields should look like X'0000007B', X'000001C8', etc. His desired O/P X'F1F2F3F4F5F6', etc. That being said, it looks like GuyC had a pretty good solution. Sorry Guy.
aryanpa1

New User

Joined: 26 May 2007
Posts: 45
Location: Chennai

 Posted: Tue Sep 15, 2009 1:12 pm Hi Dick , The sample values given are not hw they really was. These values are stored as integer in DB2 tables from where we read them. "Sorry for the late reply , I am away on weekend" Thanks, Pavankumar.J
dick scherrer

Moderator Emeritus

Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

Posted: Tue Sep 15, 2009 9:02 pm

 Quote: "Sorry for the late reply , I am away on weekend"
Welcome back

Do you have what you need?
aryanpa1

New User

Joined: 26 May 2007
Posts: 45
Location: Chennai

 Posted: Wed Sep 16, 2009 2:38 pm Thanks Dick, I need to read 4 Integer values from DB2 and concatenate them into an Alphanumeric variable by removing if there are any leading zeroes or spaces available. The procedure I have explained in my initial Post is the one using now. So I want suggestion to achieve this requirement in better way. I am lookin for a solution which will take less time compared to existing scenario. Suppose 4 integer values are as below. 12345 345 6789 22 I need output as 12345345678922.
dick scherrer

Moderator Emeritus

Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

 Posted: Wed Sep 16, 2009 8:48 pm Hello, Have you tried the code provided by GuyC? If not, it would probably be good to try this. . .
Bill O'Boyle

CICS Moderator

Joined: 14 Jan 2008
Posts: 2501
Location: Atlanta, Georgia, USA

 Posted: Wed Sep 16, 2009 9:00 pm I'm still confused, because you have stated these are PIC S9(09) COMP (binary-fullwords) fields and a X'40' in ANY of the four-bytes is perfectly legal. When you say ZEROS, I'm assuming that you mean LOW-VALUES (X'00')? Please use real-life hexadecimal values as example data in these fullwords. If a fullword were X'40404040', this is a perfectly valid value or X'00007FFF' is another valid value. Bill
mmwife

Super Moderator

Joined: 30 May 2003
Posts: 1592

 Posted: Fri Sep 18, 2009 4:03 pm As I recall, a DB2 "INTEGER" is a 4 byte COMP field (SMALL INTEGER is 2 byte COMP). So, I guess we'll have to proceed on that, since the OP will not provide the "content" of the fields (e.g. X'000001C8)'.
GuyC

Senior Member

Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

Posted: Fri Sep 18, 2009 4:44 pm

This is just showing off, for readability and maintainability I wouldn't recommend it:

given 4 integers : 123 , 4560 , 7, 8090 this sql wil returns a varchar containing '123456078090' and one containing '123456789'.

(requirement is not clear if intermediate 0 must remain or disappear)

 Code: with test(a,b,c,d) as (select 123, 4560,7,8090 from sysibm.sysdummy1) select right(digits(a),int(log10(a))+1)     !! right(digits(b),int(log10(b))+1)     !! right(digits(c),int(log10(c))+1)     !! right(digits(d),int(log10(d))+1) ,  replace(digits(a),'0','') !! replace(digits(b),'0','') !! replace(digits(c),'0','') !! replace(digits(d),'0','') from test
GuyC

Senior Member

Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

Posted: Fri Sep 18, 2009 6:16 pm

Oops, you must add coalesce(...,'') to compensate a length of 0 when the number is 0

 Code: with test(a,b,c,d) as (select 123, 4560,0,8090 from sysibm.sysdummy1) select coalesce(right(digits(a),int(log10(a))+1),'')     !! coalesce(right(digits(b),int(log10(b))+1),'')      !! coalesce(right(digits(c),int(log10(c))+1),'')      !! coalesce(right(digits(d),int(log10(d))+1),'') from test
 View Bookmarks All times are GMT + 6 Hours

 Topic Forum Replies Similar Topics Converting ASCII values to COMP-3 (ZD... JCL & VSAM 2 Generate output lines (SYSIN card for... DFSORT/ICETOOL 4 Conditional replace values in output ... DFSORT/ICETOOL 3 Find a record count/numeric is multip... COBOL Programming 1 Join files where value in one is betw... DFSORT/ICETOOL 6
Search our Forums:

 IBMMainframes.com is not an official and/or affiliated with IBM® in anyway Board Rules | FAQ | Downloads | Wiki | SiteMap | Contact Us