Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

create range table from single values

JozsefSzikszai
Active Contributor
0 Likes
6,134

hi all,

I have a number (several thousands) of single values. I want to create a range table from these single values (as you can see there are gaps in the numbers). What is the best way to achieve that?

Values I have (in an internal table):

1

2

3

5

6

9

12

13

14

15

20

...

should be converted to:

I BT 1 3

I BT 5 6

I BT 9 9 (or I EQ 9 )

I BT 12 15

...

thanks

ec

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,974

Herewith is code for reference:


  TYPES: BEGIN OF ST_NUM,
           NUM TYPE I,
         END OF ST_NUM.
  DATA: IT_NUM TYPE TABLE OF ST_NUM,
        WA_NUM LIKE LINE OF IT_NUM.

  WA_NUM-NUM = 1.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 2.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 3.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 5.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 6.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 9.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 12.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 13.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 14.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 15.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 20.
  APPEND WA_NUM TO IT_NUM.

  DATA: NEXT TYPE SY-TABIX,
        WA_NEXT LIKE LINE OF IT_NUM,
        WA_CURR LIKE LINE OF IT_NUM,
        DIFF TYPE I.
  TYPES: BEGIN OF SEL,
           SIGN(1),
           OPTION(2),
           LOW TYPE I,
           HIGH TYPE I,
         END OF SEL.
  DATA:  S_SEL TYPE TABLE OF SEL,
         WA_SEL LIKE LINE OF S_SEL,
         JUMP  TYPE SY-TABIX.

  JUMP = 1.
  LOOP AT IT_NUM INTO WA_NUM.
    CHECK JUMP EQ SY-TABIX.
    NEXT = SY-TABIX + 1.
    READ TABLE IT_NUM INTO WA_NEXT INDEX NEXT.
    IF SY-SUBRC EQ 0.
      DIFF = WA_NEXT-NUM - WA_NUM-NUM.
      IF DIFF EQ 1.
        WA_SEL-LOW = WA_NUM-NUM.
        WA_SEL-SIGN = 'I'.
        WA_SEL-OPTION = 'BT'.
        WHILE DIFF EQ 1.
          WA_CURR = WA_NEXT.
          ADD 1 TO NEXT.
          READ TABLE IT_NUM INTO WA_NEXT INDEX NEXT.
          DIFF = WA_NEXT-NUM - WA_CURR-NUM.
        ENDWHILE.
        WA_SEL-HIGH = WA_CURR-NUM.
        APPEND WA_SEL TO S_SEL.
        CLEAR WA_SEL.
        JUMP = NEXT.
      ELSE.
        WA_SEL-LOW = WA_NUM-NUM.
        WA_SEL-SIGN = 'I'.
        WA_SEL-OPTION = 'EQ'.
        APPEND WA_SEL TO S_SEL.
        CLEAR WA_SEL.
        JUMP = NEXT.
      ENDIF.
    ELSE.
      WA_SEL-LOW = WA_NUM-NUM.
      WA_SEL-SIGN = 'I'.
      WA_SEL-OPTION = 'EQ'.
      APPEND WA_SEL TO S_SEL.
      CLEAR WA_SEL.
    ENDIF.
  ENDLOOP.

9 REPLIES 9
Read only

Former Member
0 Likes
1,974

Suppose

Values I have (in an internal table):

1

2

3

5

6

9

12

13

14

15

20

.....

.....

values are stored in a table: IT_value and in the field fld1.

now create a range table gr_value.

Loop at IT_value into wa_values.
   gr_value-low = wa_values-fld1.
   gr_value-sign = 'I'.
   gr_value-option = 'EQ'.
   append gr_value to gr_value.
   clear: wa_values, gr_value. 
endloop.

now you can use the range table gr_value/

Read only

0 Likes
1,974

hi Abhishek,

your 'solution' is:

I EQ 1

I EQ 2

I EQ 3

I EQ 5

this is not what I want, pls. read the question again, before answering.

thanks ec

Read only

Former Member
0 Likes
1,974

Hi,

LOOP AT itab INTO wa_itab.

l_index = sy-tabix + 1.

READ table itab into wa_itab2 INDEX l_index.

IF sy-subrc = 0.

IF <wa_itab2-f1 - wa_itab1-f1 > EQ 1.

Fill with EQ

ELSE

Fill with BT

ENDIF.

Hope you got the logic, just do some corrections.

Regards,

Atish

Read only

0 Likes
1,974

hi Atish,

good to start from but the main thing is when and how the low and high value of the range have to be handled and when to append...

ec

Read only

0 Likes
1,974

I think you got the answer..just on the way I thought :). Good your problem solved.

Regards,

Atish

Read only

Former Member
0 Likes
1,975

Herewith is code for reference:


  TYPES: BEGIN OF ST_NUM,
           NUM TYPE I,
         END OF ST_NUM.
  DATA: IT_NUM TYPE TABLE OF ST_NUM,
        WA_NUM LIKE LINE OF IT_NUM.

  WA_NUM-NUM = 1.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 2.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 3.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 5.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 6.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 9.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 12.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 13.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 14.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 15.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 20.
  APPEND WA_NUM TO IT_NUM.

  DATA: NEXT TYPE SY-TABIX,
        WA_NEXT LIKE LINE OF IT_NUM,
        WA_CURR LIKE LINE OF IT_NUM,
        DIFF TYPE I.
  TYPES: BEGIN OF SEL,
           SIGN(1),
           OPTION(2),
           LOW TYPE I,
           HIGH TYPE I,
         END OF SEL.
  DATA:  S_SEL TYPE TABLE OF SEL,
         WA_SEL LIKE LINE OF S_SEL,
         JUMP  TYPE SY-TABIX.

  JUMP = 1.
  LOOP AT IT_NUM INTO WA_NUM.
    CHECK JUMP EQ SY-TABIX.
    NEXT = SY-TABIX + 1.
    READ TABLE IT_NUM INTO WA_NEXT INDEX NEXT.
    IF SY-SUBRC EQ 0.
      DIFF = WA_NEXT-NUM - WA_NUM-NUM.
      IF DIFF EQ 1.
        WA_SEL-LOW = WA_NUM-NUM.
        WA_SEL-SIGN = 'I'.
        WA_SEL-OPTION = 'BT'.
        WHILE DIFF EQ 1.
          WA_CURR = WA_NEXT.
          ADD 1 TO NEXT.
          READ TABLE IT_NUM INTO WA_NEXT INDEX NEXT.
          DIFF = WA_NEXT-NUM - WA_CURR-NUM.
        ENDWHILE.
        WA_SEL-HIGH = WA_CURR-NUM.
        APPEND WA_SEL TO S_SEL.
        CLEAR WA_SEL.
        JUMP = NEXT.
      ELSE.
        WA_SEL-LOW = WA_NUM-NUM.
        WA_SEL-SIGN = 'I'.
        WA_SEL-OPTION = 'EQ'.
        APPEND WA_SEL TO S_SEL.
        CLEAR WA_SEL.
        JUMP = NEXT.
      ENDIF.
    ELSE.
      WA_SEL-LOW = WA_NUM-NUM.
      WA_SEL-SIGN = 'I'.
      WA_SEL-OPTION = 'EQ'.
      APPEND WA_SEL TO S_SEL.
      CLEAR WA_SEL.
    ENDIF.
  ENDLOOP.

Read only

0 Likes
1,974

hi Hong,

seems to be perfect...

thanks

ec

Read only

0 Likes
1,974

Also check this one

DATA:

BEGIN OF itab OCCURS 0,

index TYPE sy-index,

END OF itab.

RANGES:

r_index FOR sy-index.

itab-index = 1.

APPEND itab.

itab-index = 2.

APPEND itab.

itab-index = 3.

APPEND itab.

itab-index = 5.

APPEND itab.

itab-index = 6.

APPEND itab.

itab-index = 9.

APPEND itab.

itab-index = 12.

APPEND itab.

itab-index = 13.

APPEND itab.

itab-index = 14.

APPEND itab.

itab-index = 15.

APPEND itab.

itab-index = 16.

APPEND itab.

*itab-index = 20.

*APPEND itab.

DATA:

w_index TYPE sy-tabix,

w_temp_index TYPE sy-tabix.

SORT itab BY index.

READ TABLE itab INDEX 1.

r_index-sign = 'I'.

r_index-option = 'BT'.

r_index-low = w_temp_index = itab-index.

LOOP AT itab FROM 2.

w_index = itab-index - w_temp_index.

IF w_index = 1.

r_index-high = itab-index.

ELSE.

IF r_index-high IS INITIAL.

r_index-high = r_index-low.

ENDIF.

APPEND r_index.

CLEAR r_index-high.

r_index-low = itab-index.

ENDIF. " IF itab-index

w_temp_index = itab-index.

ENDLOOP.

IF r_index-high IS INITIAL.

r_index-high = r_index-low.

ENDIF.

APPEND r_index.

loop at r_index.

write:/ r_index-low, r_index-high.

endloop.

Message was edited by:

Rajesh

Read only

0 Likes
1,974

I like this. Excellent!!