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

loop internal table to insert/replace

Former Member
0 Likes
2,641

Hi,

I have an internal table

X123 500 something something

something something something

X124 2345 something something

I need to loop the internal table to check between X123 and X124 if there is any text line in betweeen.

If yes, then replace with 'testing'.

If no text line between X123 and X124, then insert 'testing' in between.

Can give a hint.

Thanks

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,386

Try this:

The below code considers anything that does not start with X123 or X124 as text.

The logic used below is ... It would delete all text line between X123 and X124 ...

And insert a testing line in between....

I have tried this and it has worked.

LOOP AT itab.
  v_tabix = sy-tabix.
  IF itab+0(4) = 'X123'.
    v_start = 'X'.
    CONTINUE.
  ENDIF.
  IF v_start = 'X'.
    IF itab+0(4) NE 'X124'.
      DELETE itab INDEX v_tabix.
    ELSE.
      CLEAR: v_start .
      itab = 'testing'.
      v_tabix = v_tabix.
      INSERT itab INDEX v_tabix.
    ENDIF.
  ENDIF.
ENDLOOP.

14 REPLIES 14
Read only

nabheetscn
SAP Champion
SAP Champion
0 Likes
2,386

Will you be knowing the fileds to be compared before hand

Nabheet

Read only

0 Likes
2,386

Hi,

It is actually a string base on FPM_FILE.

Need to loop to detect in between X123 and X124 has a text line. if yes, change the text line to 'testing'. if no text line in between X123 and X124 then insert a text line 'testing'.

Thanks

Read only

0 Likes
2,386

can you please let us know the fields of internal table like

COLA COLB COLC

TEXT1 12 23322

nABHEET

Read only

0 Likes
2,386

hi,

as i said, it is based on FPM_FILE. and the text is in component line.

at the first 4 position, i can check X123 and X124.

now i not sure in the loop, how can i check in between X123 and X124 has text line in between as i need to work on it as per what i explained above.

thanks

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
2,386

Please read the available SAP documentation of the string functions. Your problem will be solved. read about REPLACE SECTION OF

Kesav

Read only

Former Member
0 Likes
2,386

Hi friend,

From your post i am getting that you need to compare two internal table and based on the internal table values you need to assign value 'Testing' to a text field.

I am proceeding my answer with that assumption,

Use one loop to loop one internal table.

Based on the value just use read statement to read the value from other table and compare the values and use modify statement modify the values in that internal table.

I think you can understand what i have mentioned to get any sample code just tell me the exact scenario i will help with a sample code if needed.

Thanks,

Sri Hari.

Read only

0 Likes
2,386

hi,

it is only 1 internal table.

thanks

Read only

0 Likes
2,386

Check this example. Please note that this can be written in a better way. As of now no time to think



types:begin of ty,
      field type string,
end of ty.

data:it type table of ty,
     wa type ty,
     start_pos type i,
     end_pos type i,
     diff type i.
field-symbols:<fs> type ty.

constants:start_val(4) type c value 'X123'.
constants:end_val(4) type c value 'X124'.

wa-field = 'X123 500 something something'.
append wa to it.
wa-field = 'something something something'.
append wa to it.
wa-field = 'X124 2345 something something'.
append wa to it.

loop at it assigning <fs>.
  if <fs>-field cs start_val.
    start_pos = sy-tabix.
  endif.
  if <fs>-field cs end_val.
    end_pos = sy-tabix.
   exit.
  endif.
endloop.

diff = end_pos - start_pos.
if diff > 1.
  wa-field = 'Testing'.
  start_pos = start_pos + 1.
  end_pos = end_pos - 1.
  loop at it assigning <fs> from start_pos.
    <fs>-field = 'Testing'.
    if sy-tabix = end_pos.
      exit.
    endif.
  endloop.
else.
  start_pos = start_pos + 1.
  insert initial line into it index start_pos.
  read table it assigning <fs> index start_pos.
  if sy-subrc = 0.
    <fs>-field = 'Testing'.
  endif.
endif.

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
2,386

I need to loop the internal table to check between X123 and X124 if there is any text line in betweeen.

If yes, then replace with 'testing'.

If no text line between X123 and X124, then insert 'testing' in between.

X123 500 something something "What about the text in this line. is to be replaced or kept as it is ?

something something something

X124 2345 something something

Read only

0 Likes
2,386

hi,

2 cases below. it is string and in a single internal table.

there are some other lines before X123 and after X124 which i cannot delete. must remain.

thanks

from

X123 500 something something

something something something

X124 2345 something something

to

X123 500 something something

testing

X124 2345 something something

from

X123 500 something something

X124 2345 something something

to

X123 500 something something

testing

X124 2345 something something

Read only

0 Likes
2,386

Hi friend,

Move the table data into a structure and then try to find whether there is any text in between the two fields and then change the structure and modify the table.

or

Make a duplicate of an internal table and then check the field 1 with field 4 or any of the duplicate table and then modify any one table based on the value.

I think this will solve your issue. Please revert back to me if your problem still exists.

Thanks,

Sri Hari

Read only

Former Member
0 Likes
2,386

anyways your aim is to have 'testing' in between X* lines...

so why dont you delete all the lines which do not start with X

delete itab where f1+0(1) NE 'X'.

after this,

start looping and keep inserting

Read only

Former Member
0 Likes
2,391

Try this:

The below code considers anything that does not start with X123 or X124 as text.

The logic used below is ... It would delete all text line between X123 and X124 ...

And insert a testing line in between....

I have tried this and it has worked.

LOOP AT itab.
  v_tabix = sy-tabix.
  IF itab+0(4) = 'X123'.
    v_start = 'X'.
    CONTINUE.
  ENDIF.
  IF v_start = 'X'.
    IF itab+0(4) NE 'X124'.
      DELETE itab INDEX v_tabix.
    ELSE.
      CLEAR: v_start .
      itab = 'testing'.
      v_tabix = v_tabix.
      INSERT itab INDEX v_tabix.
    ENDIF.
  ENDIF.
ENDLOOP.

Read only

0 Likes
2,386

Good Work ;)..But OP has to Confirm If there are more than one lines in between , whether he needs all the lines with a single line or all the lines in between needs to be replaced with 'Testing' in each line. In this case you have to modify your code.