IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Concatenating Numeric values by removing leading zeroes


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
aryanpa1

New User


Joined: 26 May 2007
Posts: 45
Location: Chennai

PostPosted: Wed Sep 09, 2009 5:54 pm
Reply with quote

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.
Back to top
View user's profile Send private message
rockish

Active User


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

PostPosted: Wed Sep 09, 2009 6:00 pm
Reply with quote

Can you pls clarify on your third step ?
What command do you use to read and process the concatenated string byte by byte ?
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


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

PostPosted: Wed Sep 09, 2009 6:00 pm
Reply with quote

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.
Back to top
View user's profile Send private message
aryanpa1

New User


Joined: 26 May 2007
Posts: 45
Location: Chennai

PostPosted: Wed Sep 09, 2009 6:09 pm
Reply with quote

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."
Back to top
View user's profile Send private message
aryanpa1

New User


Joined: 26 May 2007
Posts: 45
Location: Chennai

PostPosted: Wed Sep 09, 2009 6:12 pm
Reply with quote

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.
Back to top
View user's profile Send private message
Craq Giegerich

Senior Member


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

PostPosted: Wed Sep 09, 2009 6:20 pm
Reply with quote

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.
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


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

PostPosted: Wed Sep 09, 2009 6:21 pm
Reply with quote

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.
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Wed Sep 09, 2009 6:37 pm
Reply with quote

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
Back to top
View user's profile Send private message
mmwife

Super Moderator


Joined: 30 May 2003
Posts: 1592

PostPosted: Fri Sep 11, 2009 4:09 am
Reply with quote

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?
Back to top
View user's profile Send private message
Bill O'Boyle

CICS Moderator


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

PostPosted: Fri Sep 11, 2009 8:15 am
Reply with quote

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
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Fri Sep 11, 2009 12:12 pm
Reply with quote

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.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


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

PostPosted: Fri Sep 11, 2009 8:04 pm
Reply with quote

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 icon_smile.gif ). 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.
Back to top
View user's profile Send private message
mmwife

Super Moderator


Joined: 30 May 2003
Posts: 1592

PostPosted: Tue Sep 15, 2009 5:03 am
Reply with quote

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. icon_redface.gif
Back to top
View user's profile Send private message
aryanpa1

New User


Joined: 26 May 2007
Posts: 45
Location: Chennai

PostPosted: Tue Sep 15, 2009 1:12 pm
Reply with quote

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
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


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

PostPosted: Tue Sep 15, 2009 9:02 pm
Reply with quote

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

Do you have what you need?
Back to top
View user's profile Send private message
aryanpa1

New User


Joined: 26 May 2007
Posts: 45
Location: Chennai

PostPosted: Wed Sep 16, 2009 2:38 pm
Reply with quote

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.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


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

PostPosted: Wed Sep 16, 2009 8:48 pm
Reply with quote

Hello,

Have you tried the code provided by GuyC?

If not, it would probably be good to try this. . .
Back to top
View user's profile Send private message
Bill O'Boyle

CICS Moderator


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

PostPosted: Wed Sep 16, 2009 9:00 pm
Reply with quote

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
Back to top
View user's profile Send private message
mmwife

Super Moderator


Joined: 30 May 2003
Posts: 1592

PostPosted: Fri Sep 18, 2009 4:03 pm
Reply with quote

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)'.
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Fri Sep 18, 2009 4:44 pm
Reply with quote

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
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Fri Sep 18, 2009 6:16 pm
Reply with quote

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
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts how to eliminate null indicator value... DB2 7
No new posts Issues Converting From ZD to Signed N... DFSORT/ICETOOL 4
No new posts INCLUDE OMIT COND for Multiple values... DFSORT/ICETOOL 5
No new posts Replace Multiple Field values to Othe... DFSORT/ICETOOL 12
No new posts Keep leading zero(s) after convert fl... SYNCSORT 7
Search our Forums:

Back to Top