Page 1 of 1

How to add var in REXX

PostPosted: Wed Mar 23, 2011 7:19 pm
by Mann_B
Hi

I have add comp-3 var in RExx. I am getting the field from a file using SUBSTR...Now I have substr(comp-3).But when am trying to add 2 such fields am getting error saying Bad arthimetic operation

Can anyone help me in how to add .Do we have any conversion?

Re: How to add var in REXX

PostPosted: Wed Mar 23, 2011 8:10 pm
by BillyBoyo
If you search this forum I think you'll find some stuff to help. You'll need conversion. There are some excellent examples and suggestions. Ideally, you'd even have done it before asking the question. Also, a manual. Online help, if you have it (I don't know about REXX in TSO).

Re: How to add var in REXX

PostPosted: Wed Mar 23, 2011 8:20 pm
by Mann_B
I tried giving like this
AMOUNT = (C2X(SUBSTR(REC1.RECID,POS2,8)))
but am getting an error saying Incorrect call...
here REC1.RECID,POS2,8--ths is a comp-3 var...

Re: How to add var in REXX

PostPosted: Wed Mar 23, 2011 8:42 pm
by Akatsukami
Mann_B wrote:I tried giving like this
AMOUNT = (C2X(SUBSTR(REC1.RECID,POS2,8)))
but am getting an error saying Incorrect call...
here REC1.RECID,POS2,8--ths is a comp-3 var...

I would check that POS2 and RECID have valid values; TRACE the logic if necessary.

C2X will give you the hexadecimal string representing the characters; i.e., if your packed decimal (COMP-3 in COBOLese, which should be avoided unless actually referring to COBOL) data are X'000000000000123C', (C2X(SUBSTR(REC1.RECID,POS2,8))) will return "000000000000123C". This will be treated as a character string in Rexx, and if used in a computational statement will cause the error noted in your first post.

There is no direct conversion in native Rexx (although there are conversion functions that can be purchased as add-ons). You'll need to write your own function, something like (untested code with all error checking omitted):
C2P:  procedure
arg string pos len .
foo = c2x(substr(string,pos,len))
bar = substr(foo,1,(2*len)-1)
 
if (substr(foo,2*len)="D") then
  bar = bar * -1

return bar

Re: How to add var in REXX

PostPosted: Thu Mar 24, 2011 1:09 am
by NicC
I am not sure if I have posted this before - I know others have posted similar so your initial search - you DID search didn't you (as per the rules) - should have found one.
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/         
/* P2D function to convert a FIXED DEC field to ZONED DECIMAL        */         
/*                                                                   */         
/* Argument : Packed decimal field to be converted. Returns field in */         
/*            zoned decimal format. The calling routine will have to */         
/*            determine the decimal position. E.g. if the FIXED DEC  */         
/*            field contains 100.99 this is stored as '10099C'x and  */         
/*            the called procedure will divide the returned string,  */         
/*            10099, by 100 to get 100.99                            */         
/* Examples:                                                         */         
/*   1: stdat = Right(P2D(stdat),5,0)        - fixed dec 5,0         */         
/*   2: chamt = P2D(chamt)                   - fixed dec 11,2        */         
/*      chamt = Format(chamt / 100,,2)                               */         
/*   3: chamt = Format(P2D(chamt) / 100,,2)  - same as exmample      */         
/*-------------------------------------------------------------------*/         
p2d: Procedure                                                                 
                                                                               
   px = C2X(Arg(1))                 /* Convert value to a hex string */         
   lpx = Length(px)                 /* and get its length            */         
   nd = Digits()                    /* save current Digits setting   */         
   Numeric Digits lpx               /* Ensure enough Digits          */         
   Parse Upper Var px d =(lpx) s    /* Split px into digits and sign */         
   If Datatype(d,'W')               /* Only digits 0-9 in digits?    */         
   Then Do                                                                     
      pn = Pos(s,'BDACEF')          /* Determine sign                */         
      If pn > 2 Then Return +d      /* A,C,E or F is positive        */         
      If pn > 0 Then Return -d      /* B and D are negative          */         
   End                                                                         
   Else Nop                         /* Unsigned value                */         
   Numeric Digits nd                /* Restore Digits value          */         
                                                                               
Return px                                                                       

Re: How to add var in REXX

PostPosted: Mon Mar 28, 2011 7:28 pm
by Mann_B
Thank You


I got the result..its almost the same way

Re: How to add var in REXX

PostPosted: Mon Mar 28, 2011 7:31 pm
by Mann_B
AMOUNT = left((C2X(SUBSTR(REC1.RECID,POS2,8))) ,15)
IF SUBSTR(AMOUNT,16,1) = 'D' THEN
DO
SAY 'NEGATIVE VALUE CURRENT' AMOUNT
AMOUNTA = 0 - AMOUNTA
SAY 'AMOUNT A' AMOUNTA
END
AMOUNTB = LEFT(C2X(AMOUNT.CNT),15)

IF SUBSTR(C2X(AMOUNT.CNT),16,1) = "D" THEN
DO
SAY'NEGATIVE VALUE ARRAY'
AMOUNTL = AMOUNTA + AMOUNTB

and then the revesre conversion