View previous topic :: :: View next topic

Author 
Message 
vidyaa
New User
Joined: 02 May 2008 Posts: 77 Location: chennai




hi,
i have an variable decared as
03 WSSAFECRT PIC 9(4).
the values can be like WSSAFECRT = 1234, WSSAFECRT = 0102,
WSSAFECRT=0045
i need to take the valid values of the variable (i.e) remove the leading zeros and take the length of the vaibable and divide by 1000,1000,100 corresponding to the original length of the variable
if WSSAFECRT = 1234 then RESULT = WSSAFECRT/10000 (since lenght of 1234 is 4 hence dividing by 10000)
WSSAFECRT = 0102 then RESULT = WSSAFECRT/1000 (since lenght of 0102 is 3 hence dividing by 1000)
WSSAFECRT = 0045 then RESULT = WSSAFECRT/100 (since lenght of 0045 is 2 hence dividing by 100)
how can this be done in cobol 

Back to top 




dick scherrer
Site Director
Joined: 23 Nov 2006 Posts: 19270 Location: Inside the Matrix




Hello,
Code: 
IF WSSAFECRT > 999 COMPUTE THERESULT = WSSAFECRT / 10000 
and so on . . . 

Back to top 


Escapa
Senior Member
Joined: 16 Feb 2007 Posts: 1399 Location: IL, USA




Hi Vidya,
below is the logic am giving to you
on that line you can build code
divvalue=1
temp=yourno
do until temp > = 1
{
divvalue=divvalue*10
temp=yourno/10
yourno=temp
}
eg if yourno is 1234 then divvalue will be 10000
if yourno is 0123 then divvalue will be 1000
if yourno is 0 then divvalue will be 1 ie no zeros ( ) 

Back to top 


mytags
New User
Joined: 28 Apr 2008 Posts: 63 Location: US




Hi
Using if condition is the easiest ways.As Dick said.
Thanks
Mytags 

Back to top 


Escapa
Senior Member
Joined: 16 Feb 2007 Posts: 1399 Location: IL, USA




but then it will be hard coded and only work for number for which if is coded
above is generic logic works for any no of digits 

Back to top 


dick scherrer
Site Director
Joined: 23 Nov 2006 Posts: 19270 Location: Inside the Matrix




Hello,
Quote: 
but then it will be hard coded and only work for number for which if is coded 
I believe you misunderstand. There will be only 1 value hardcoded for each 999, 99, and 9. This will run much more efficiently than the loop which will use many cpu cycles unnecessarily.
"Any number of digits" should not be an issue as the field only holds 4 digits.
The loop would not be obvious to many who read the code. It is best to implement code that is correct, easily maintainable, and does not use more system resource than needed. 

Back to top 


vidyaa
New User
Joined: 02 May 2008 Posts: 77 Location: chennai




Hi Dick,
Thank you so much and it works great. 

Back to top 


dick scherrer
Site Director
Joined: 23 Nov 2006 Posts: 19270 Location: Inside the Matrix




Hi Vidya,
You're welcome
Thank you for letting us know it is working.
d 

Back to top 


