View previous topic :: View next topic |
Author |
Message |
shuko Beginner
Joined: 08 Nov 2005 Posts: 73 Topics: 20
|
Posted: Thu Apr 17, 2014 9:11 am Post subject: Cobol between |
|
|
Hello
What I am trying to do is to get the amount from the internal table
if input year is a between nDevYrFrom and nDevyrTo.
if the input nDevYr = 10 for example, I should get the amount 300 and if is 1 amount will be 100
Actually input nDevYr a between nDevYrFrom and nDevYearTo
Howe can I code this in Cobol
Working Storage
Code: |
03 sX5nC1 OCCURS 10 INDEXED BY Ridx.
05 cPc PIC X(15) VALUE SPACE.
05 cProcdat PIC X(08) VALUE SPACE.
05 sX5nRate OCCURS 60 INDEXED BY Pidx2.
07 nAmountTab COMP-2 VALUE ZERO.
07 nDevYrFrom PIC 9(03) VALUE ZERO.
07 nDevYrTo PIC 9(03) VALUE ZERO.
|
My internal table with values
Code: |
Amount nDevYrFrom nDevyrTo
--------------------------------
100 1 1
300 2 100
|
My attempt below did not get the required result. Index Ridx is searched in table sX5nC1. This works but the search in the second table does not.
SEARCH sX5nRate
AT END
Display 'Not found'
WHEN nRskDevYrTo(Ridx, Pidx2) <= nDevYr
MOVE nAmountTab (Ridx, Pidx2) TO nAmount
END-SEARCH
Thank you
shuko |
|
Back to top |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12375 Topics: 75 Location: San Jose
|
Posted: Thu Apr 17, 2014 11:26 am Post subject: |
|
|
shuko,
Your are checking nRskDevYrTo variable and the table variable is defined as nDevYrTo.
SEARCH SX5Nrate increments the lowest-level index only. Hence if X is set to 1 initially, the SEARCH will perform a look-up on items in warehouse SXN5nc1, that is (1, 1) through (1, 10). To search all entries, the SEARCH itself must be executed from a PERFORM ... VARYING
ex:
Code: |
PERFORM VARYING RIDX FROM 1 BY 1 UNTIL RIDX > 10
PERFORM VARYING PIDX2 FROM 1 BY 1 UNTIL PIDX2 > 60
IF NDEVYRFROM(RIDX, PIDX2) <= W-SEARCH-KEY
AND NDEVYRTO(RIDX, PIDX2) >= W-SEARCH-KEY
MOVE nAmountTab (Ridx, Pidx2) TO nAmount
END-IF
END-PERFORM
END-PERFORM
|
_________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
|
William Collins Supermod
Joined: 03 Jun 2012 Posts: 437 Topics: 0
|
Posted: Thu Apr 17, 2014 11:38 am Post subject: |
|
|
You are new to COBOL.
You probably will never need to use COMP-1 or COMP-2 in your COBOL coding. COMP or COMP-3 are going to give you exactly what you want, unless you need values outside about 10**+/-31 (which it doesn't look like from what you have shown).
The IBM Enterprise COBOL manuals are really thorough. If you consult the manual for SEARCH you'll find out what you have not done, which you need to do. |
|
Back to top |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12375 Topics: 75 Location: San Jose
|
Posted: Thu Apr 17, 2014 11:55 am Post subject: |
|
|
shuko,
I forgot to add the SEARCH syntax in earlier post
Code: |
PERFORM 1000-SEARCH-RATE VARYING RIDX FROM 1 BY 1
UNTIL RIDX > 10 OR MATCH-FOUND = 'Y'
1000-SEARCH-RATE.
SET PIDX2 TO 1
SEARCH SX5NRATE
WHEN NDEVYRTO (RIDX, PIDX2) >= W-SEARCH-KEY
MOVE nAmountTab (Ridx, Pidx2) TO nAmount
MOVE 'Y' TO MATCH-FOUND
END-SEARCH
.
|
_________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
|
shuko Beginner
Joined: 08 Nov 2005 Posts: 73 Topics: 20
|
Posted: Fri Apr 18, 2014 5:23 am Post subject: |
|
|
Thank you Kolusu
I did it like this as I read somewhere that a SEARCH is faster than a PERFORM varying. Maybe for a small table it does not make a difference.
Code: |
SET Ridx TO 1
SEARCH sX5nC1
AT END DISPLAY "Not found."
WHEN cPc (Ridx) = <search key>
AND cProcdat (Ridx) = <search key>
SET Pidx2 TO 1
SEARCH sX5nRate
AT END DISPLAY " not found"
WHEN nRskDevYrToCache(Ridx, Pidx2) >= nDevYr
DISPLAY "found " nAmount (Ridx, Pidx2)
END-SEARCH
END-SEARCH |
|
|
Back to top |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12375 Topics: 75 Location: San Jose
|
Posted: Fri Apr 18, 2014 11:06 am Post subject: |
|
|
shuko wrote: | Thank you Kolusu
I did it like this as I read somewhere that a SEARCH is faster than a PERFORM varying. Maybe for a small table it does not make a difference. |
Shuko,
A Binary Search ( SEARCH ALL) might be advantageous but if you are doing serial search(SEARCH), then it wouldn't be any different from PERFORM verb unless you are subscripts. You are using Index variables, so it should be the same performance wise. |
|
Back to top |
|
|
|
|