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

identifying consecutive values in internal table.

Former Member
0 Likes
904

Hi all,

I have an internal table with field A.

A

-


2

4

5

6

7

10

12

13

14

-


the values 4-7 are consecutive.

I need to print output as

2

4 to 7

10

12 to 14

Please help me to find the logic for implementing this. Thank you.

Thanks

Pranati.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
547

Hi please look at the following code. I am not sitting on an sap system and hence there might be small bugs as i have not tested it at all. But this should give you atleast a basic idea of the logic and u should be able to do it from henceforth.


types: begin of ty_itab,
       field type int4,
       end   of ty_itab.

data: li_main_tab type standard table of ty_itab,
      lx_first_no type ty_itab,
      lx_loop_no  type ty_itab,
      lx_prev_no  type ty_itab.
* Now populate data however you are doing it.
* the above data statements are only for your reference.

sort li_main_tab by field.
read table li_main_tab
     index 1 
     into lx_first_no-field.
write:/ lx_first_no-field.
loop at li_main_tab into lx_loop_no.
  l_diff = lx_loop_no-field - l_prev_no-field.
  if l_diff NE 1.
    if l_gap NE c_x.
      write:/ lx_prev_no-field.
      l_gap = c_null.
    else.
      write: /c_first_no ' to ' c_prev_no.
      lx_first_no-field = lx_loop_no-field.
      lx_prev_no-field  = lx_loop_no-field.
    endif.
  else.
    lx_prev_no-field = lx_loop_no-field.
  endif.
endloop.

Do let me know if there are any issues.

2 REPLIES 2
Read only

Former Member
0 Likes
548

Hi please look at the following code. I am not sitting on an sap system and hence there might be small bugs as i have not tested it at all. But this should give you atleast a basic idea of the logic and u should be able to do it from henceforth.


types: begin of ty_itab,
       field type int4,
       end   of ty_itab.

data: li_main_tab type standard table of ty_itab,
      lx_first_no type ty_itab,
      lx_loop_no  type ty_itab,
      lx_prev_no  type ty_itab.
* Now populate data however you are doing it.
* the above data statements are only for your reference.

sort li_main_tab by field.
read table li_main_tab
     index 1 
     into lx_first_no-field.
write:/ lx_first_no-field.
loop at li_main_tab into lx_loop_no.
  l_diff = lx_loop_no-field - l_prev_no-field.
  if l_diff NE 1.
    if l_gap NE c_x.
      write:/ lx_prev_no-field.
      l_gap = c_null.
    else.
      write: /c_first_no ' to ' c_prev_no.
      lx_first_no-field = lx_loop_no-field.
      lx_prev_no-field  = lx_loop_no-field.
    endif.
  else.
    lx_prev_no-field = lx_loop_no-field.
  endif.
endloop.

Do let me know if there are any issues.

Read only

Former Member
0 Likes
547

Hi,

I have solved this problem. Here is the code which has worked for me.....

l_t_runids is the internal table which has the values i have mentioned in the question.

<b>ix = 1.

sort l_t_runids.

delete adjacent duplicates from l_t_runids.

loop at l_t_runids.

l_t_runids-index = ix.

ix = ix + 1.

modify l_t_runids.

endloop.

clear count.

ix = 1.

loop at l_t_runids.

var1 = l_t_runids-runid.

cx = ix + 1.

read table l_t_runids index cx.

var2 = l_t_runids-runid.

var3 = var1 + 1.

if var2 = var3.

count = count + 1.

if count = 1.

l_t_runid-runid = var1.

append l_t_runid.

l_t_runid-runid = var2.

append l_t_runid.

else.

l_t_runid-runid = var2.

append l_t_runid.

endif.

elseif count > 0.

describe table l_t_runid lines line.

read table l_t_runid index line.

hrunid = l_t_runid-runid.

line = 1.

read table l_t_runid index line.

lrunid = l_t_runid-runid.

clear l_t_runid.

refresh l_t_runid.

clear line.

write :/20 lrunid, 'to' , hrunid.

clear count.

else.

write :/20 var1.

endif.

ix = ix + 1.

endloop.</b>