View previous topic :: View next topic
|
Author |
Message |
magesh23586
Active User
Joined: 06 Jul 2009 Posts: 213 Location: Chennai
|
|
|
|
Hi I have requirement.
I have to get three value from a line then divide it by two, replace it in the same position with right aligned, in this some of the values have negative which has to be placed in same position as below
Example
Input
Code: |
Command ===> Scroll ===> CSR
=COLS> ---+----7----+----8----+----9----+----0----+----1----+----2----+----3---
000047 150.00- 150.00 0.00
|
Output should be
Code: |
Command ===> Scroll ===> CSR
=COLS> ---+----7----+----8----+----9----+----0----+----1----+----2----+----3---
000047 75.00- 75.00 0.00
|
Advance thanks |
|
Back to top |
|
|
Akatsukami
Global Moderator
Joined: 03 Oct 2009 Posts: 1788 Location: Bloomington, IL
|
|
|
|
And you would like us to...? |
|
Back to top |
|
|
Pedro
Global Moderator
Joined: 01 Sep 2006 Posts: 2547 Location: Silicon Valley
|
|
|
|
The slash character is used to perform division, for example:
|
|
Back to top |
|
|
TheMFKid
New User
Joined: 20 Nov 2013 Posts: 91 Location: India
|
|
|
|
You can use a combination of functions(ex: POS/SUBSTR/Parse, RIGHT) to achieve this.
What is the problem you are facing in coding? |
|
Back to top |
|
|
magesh23586
Active User
Joined: 06 Jul 2009 Posts: 213 Location: Chennai
|
|
|
|
Thanks guys for the kind information.
I have done the following code to achieve this.
Code: |
PARSE VAR A STR1 STR2 STR3
/********/
IF POS('-',STR1)>0 THEN
DO
SUB1 = LENGTH(STR1)
SUB1 = SUB1 - 1
STR1 = LEFT(STR1,SUB1)
STR1 = STR1 / 2
STR1 = FORMAT(STR1,,2)
STLEN = LENGTH(STR1)
STR1 = STR1'-'
END
ELSE
DO
STR1 = STR1 / 2
STR1 = FORMAT(STR1,,2)
STLEN = LENGTH(STR1)
END
STR1 = 'RPLR ' STR1
STLEN = 97 - (STLEN + 5)
VAR1 = SPACE(STR1,STLEN)
/***********/
IF POS('-',STR2)>0 THEN
DO
SUB1 = LENGTH(STR2)
SUB1 = SUB1 - 1
STR2 = LEFT(STR2,SUB1)
STR2 = STR2 / 2
STR2 = FORMAT(STR2,,2)
STLEN = LENGTH(STR2)
STR2 = STR2'-'
END
ELSE
DO
STR2 = STR2 / 2
STR2 = FORMAT(STR1,,2)
STLEN = LENGTH(STR2)
END
STR2 = 'RPLR ' STR2
STLEN = 18 - (STLEN + 5)
VAR2 = SPACE(STR2,STLEN)
/*********/
IF POS('-',STR3)>0 THEN
DO
SUB1 = LENGTH(STR3)
SUB1 = SUB1 - 1
STR3 = LEFT(STR3,SUB1)
STR3 = STR3 / 2
STR3 = FORMAT(STR3,,2)
STLEN = LENGTH(STR3)
STR3 = STR3'-'
END
ELSE
DO
STR3 = STR3 / 2
STR3 = FORMAT(STR3,,2)
STLEN = LENGTH(STR3)
END
STR3 = 'RPLR ' STR3
STLEN = 18 - (STLEN + 5)
VAR3 = SPACE(STR3,STLEN)
VARF = VAR1 '' VAR2 '' VAR3
|
Finally find all replace 'RPLR' with ' ' . |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
You could have saved a bit of typing by using something like
Code: |
rec = " 150.00- 150.00 0.00 "
parse var rec str.1 str.2 str.3
off = 20
out = copies(" ",off)
do i = 1 to 3
neg = " "
if pos("-", str.i ) > 0 then do
neg = "-"
str.i = translate(str.i," ","-")
end
str.i = strip(str.i) / 2
str.i = format(str.i,,2) || neg
out = out || right(str.i,18)
end
say ">>>>>" || out || "<<<<<"
|
|
|
Back to top |
|
|
magesh23586
Active User
Joined: 06 Jul 2009 Posts: 213 Location: Chennai
|
|
|
|
This is Fantastic, Thank you enrico. |
|
Back to top |
|
|
|