Application Development 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: 

Consolidating consecutive numbers!

Former Member
0 Kudos
342

Hi!

I have an internal table with 2 fields.

For e.g.

A | B

2152 | 2177

2246 | 2259

2260 | 2299

2300 | 2329

2330 | 2337

2338 | 2345

2346 | 2350

2397 | 2407

The numbers from B to the immediate next row of A has to be consecutive; if not it forms the next sequence (till it is not consecutive).

In the second row 2246 is not the consecutive number of 2177. So the first sequence is 2152 - 2177. The following rows are in sequence upto the second last row. So the sequence is 2246 - 2350. And the last sequence is 2397 - 2407.

I would need the output as

2152 - 2177

2246 - 2350

2397 - 2407.

Any help would be appreciated. Points would be rewarded for any help rendered! Thanks!

Regards,

Jonathan.

1 ACCEPTED SOLUTION

Former Member
0 Kudos
115

hi,

I changed the code a little bit. Try with this new one.

data: begin of itab occurs 0,
        a type i,
        b type i,
      end of itab.

data: v_low like itab-a, v_high like itab-a,
      v_temp like itab-a,
      v_tabix like sy-tabix,
      v_next like sy-tabix,
      v_rec type i, v_chng type c value 'X'.

start-of-selection.

  itab-a = '123'. itab-b = '345'. append itab . clear itab.
  itab-a = '346'. itab-b = '390'. append itab . clear itab.
  itab-a = '392'. itab-b = '399'. append itab . clear itab.
  itab-a = '400'. itab-b = '845'. append itab . clear itab.

  describe table itab lines v_rec.

  loop at itab.
    if v_chng = 'X'.
      v_low = itab-a.
    endif.
    v_high = itab-b.
    v_tabix = sy-tabix.
    v_next = v_tabix + 1.
    if v_rec = v_tabix.

      write 😕 v_low, '-', v_high.

    else.
      read table itab index v_next.
      v_temp = v_high + 1.
      if itab-a = v_temp.
        v_high = itab-b.
        v_chng = ' '.
      else.
        write 😕 v_low, '-', v_high.
        v_chng = 'X'.
      endif.
    endif.
  endloop.

Regards,

Sailaja.

Message was edited by: Sailaja N.L.

6 REPLIES 6

Former Member
0 Kudos
116

hi,

I changed the code a little bit. Try with this new one.

data: begin of itab occurs 0,
        a type i,
        b type i,
      end of itab.

data: v_low like itab-a, v_high like itab-a,
      v_temp like itab-a,
      v_tabix like sy-tabix,
      v_next like sy-tabix,
      v_rec type i, v_chng type c value 'X'.

start-of-selection.

  itab-a = '123'. itab-b = '345'. append itab . clear itab.
  itab-a = '346'. itab-b = '390'. append itab . clear itab.
  itab-a = '392'. itab-b = '399'. append itab . clear itab.
  itab-a = '400'. itab-b = '845'. append itab . clear itab.

  describe table itab lines v_rec.

  loop at itab.
    if v_chng = 'X'.
      v_low = itab-a.
    endif.
    v_high = itab-b.
    v_tabix = sy-tabix.
    v_next = v_tabix + 1.
    if v_rec = v_tabix.

      write 😕 v_low, '-', v_high.

    else.
      read table itab index v_next.
      v_temp = v_high + 1.
      if itab-a = v_temp.
        v_high = itab-b.
        v_chng = ' '.
      else.
        write 😕 v_low, '-', v_high.
        v_chng = 'X'.
      endif.
    endif.
  endloop.

Regards,

Sailaja.

Message was edited by: Sailaja N.L.

former_member186741
Active Contributor
0 Kudos
115

I have used select-options and ranges as it's easier to test than internal tables, but the logic is identical:

REPORT znrw_consolidate_table .

SELECT-OPTIONS s_numbs FOR sy-tabix.

RANGES r_numbs FOR sy-tabix.

DATA:

v_next LIKE sy-tabix.

START-OF-SELECTION.

write:/ 'IN'.

LOOP AT s_numbs.

write:/ s_numbs-low,s_numbs-high.

  • first time in...

IF r_numbs IS INITIAL.

*... copy output from input

r_numbs = s_numbs.

  • if current input A continues the sequence...

elseIF s_numbs-low = v_next.

*... update the output B number

r_numbs-high = s_numbs-high.

*... when the sequence is broken...

ELSE.

*... put into the output table

APPEND r_numbs.

*... now move current input to output area

r_numbs = s_numbs.

ENDIF.

*... set up the next number in the sequence

v_next = s_numbs-high + 1.

AT LAST.

*... at the very end add the final output entry

APPEND r_numbs.

ENDAT.

ENDLOOP.

write:/ 'OUT'.

loop at r_numbs.

write:/ r_numbs-low,r_numbs-high.

ENDLOOP.

Former Member
0 Kudos
115

Hi Jonathan,

You can try this code -

TYPES : BEGIN OF t_itab,

a TYPE i,

b TYPE i,

END OF t_itab.

DATA : i_itab1 TYPE STANDARD TABLE OF t_itab,

i_itab2 TYPE STANDARD TABLE OF t_itab,

wa_itab1 TYPE t_itab,

wa_itab2 TYPE t_itab,

w_temp_low TYPE i,

w_temp_high TYPE i,

w_diff TYPE i.

wa_itab1-a = 2152.

wa_itab1-b = 2177.

APPEND wa_itab1 TO i_itab1.

CLEAR : wa_itab1.

wa_itab1-a = 2246.

wa_itab1-b = 2259.

APPEND wa_itab1 TO i_itab1.

CLEAR : wa_itab1.

wa_itab1-a = 2260.

wa_itab1-b = 2299.

APPEND wa_itab1 TO i_itab1.

CLEAR : wa_itab1.

wa_itab1-a = 2300.

wa_itab1-b = 2329.

APPEND wa_itab1 TO i_itab1.

CLEAR : wa_itab1.

wa_itab1-a = 2330.

wa_itab1-b = 2337.

APPEND wa_itab1 TO i_itab1.

CLEAR : wa_itab1.

wa_itab1-a = 2338.

wa_itab1-b = 2345.

APPEND wa_itab1 TO i_itab1.

CLEAR : wa_itab1.

wa_itab1-a = 2346.

wa_itab1-b = 2350.

APPEND wa_itab1 TO i_itab1.

CLEAR : wa_itab1.

wa_itab1-a = 2397.

wa_itab1-b = 2407.

APPEND wa_itab1 TO i_itab1.

CLEAR : wa_itab1.

LOOP AT i_itab1 INTO wa_itab1.

WRITE 😕 wa_itab1-a, wa_itab1-b.

CLEAR : wa_itab1.

ENDLOOP.

WRITE :/,/,/.

IF NOT i_itab1 IS INITIAL.

CLEAR : wa_itab1.

LOOP AT i_itab1 INTO wa_itab1.

w_temp_low = wa_itab1-a.

w_diff = w_temp_low - w_temp_high.

IF w_diff NE 1.

IF NOT w_temp_high IS INITIAL.

wa_itab2-b = w_temp_high.

APPEND wa_itab2 TO i_itab2.

ENDIF.

wa_itab2-a = w_temp_low.

ENDIF.

w_temp_high = wa_itab1-b.

ENDLOOP.

ENDIF.

wa_itab2-b = w_temp_high.

APPEND wa_itab2 TO i_itab2.

CLEAR : wa_itab2.

LOOP AT i_itab2 INTO wa_itab2.

WRITE 😕 wa_itab2-a, wa_itab2-b.

CLEAR : wa_itab2.

ENDLOOP.

Do reward points if helpful,

Regards,

Saurabh

Former Member
0 Kudos
115

loop at itab.

at first.

clear iseq.

move itab-a to iseq-start.

move itab-b to iseq-end.

modify iseq.

endat.

check sy-tabix <> 1.

describe table iseq lines lrec.

read table iseq index lrec.

lnext = iseq-end + 1.

if lnext = itab-a.

iseq-end = itab-b.

modify iseq transporting b index lrec.

else.

iseq-start = itab-a.

iseq-end = itab-b.

modify iseq.

endif.

endloop.

<b>iseq has the sequence !!</b>

Message was edited by: Anurag Bankley

Former Member
0 Kudos
115

Hi Jonathan,

Is your problem solved?

If so please reward points and close the question.

Regards,

Saurabh

Former Member
0 Kudos
115

Thanks people! Issue resolved!!

Regards,

Jonathan.