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: 

Merge an internal table into a string stream

MarkusKlein
Active Contributor
0 Kudos

Hello,

i need to merge an internal table (2 column) layout:

ELEMENT

VALUE

into one string stream.

E.g.:

ELEMENT | VALUE

test1 | 1111111111111111111

test1 | 2222222222222222222

result should be:

test1 | 11111111111111111112222222222222222222 (= type string!)

too bad you cant use offset while working with a field of type string and im sure there is a ABAP class which does do this.

regards,

Markus

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Try this Markus:


REPORT ztest NO STANDARD PAGE HEADING LINE-SIZE 255.

DATA: BEGIN OF itab OCCURS 0,
        element TYPE string,
        value   TYPE string,
      END   OF itab.

DATA: BEGIN OF itab2 OCCURS 0,
        element TYPE string,
        value   TYPE string,
      END   OF itab2.

DATA: next LIKE sy-tabix VALUE 1,
      old_element LIKE itab-element.

PERFORM load_itab.
sy-subrc = 0.

DO.
  READ TABLE itab INDEX next.
  IF sy-subrc <> 0.
    EXIT.
  ELSE.
    IF itab-element <> old_element.
      IF next <> 1.
        APPEND itab2.
        CLEAR itab2.
      ENDIF.
      itab2-element = itab-element.
      old_element = itab-element.
    ENDIF.
    CONCATENATE itab2-value itab-value INTO itab2-value.
    next = next + 1.
  ENDIF.
ENDDO.
IF itab2-element <> space.
  APPEND itab2.
ENDIF.

break rburbank.

*&---------------------------------------------------------------------*
*&      Form  load_itab
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM load_itab.

  REFRESH: itab,
           itab2.
  CLEAR  : itab,
           itab2.

  itab-element = 'test1'.
  itab-value   = '1111111111111111111'.
  APPEND itab.

  itab-element = 'test1'.
  itab-value   = '2222222222222222222'.
  APPEND itab.

  itab-element = 'test2'.
  itab-value   = '3333333333333333333'.
  APPEND itab.

  itab-element = 'test2'.
  itab-value   = '4444444444444444444'.
  APPEND itab.

ENDFORM.                    " load_itab

Rob

5 REPLIES 5

Former Member
0 Kudos

hey markus,

you use this code:

data : var1(4),

var2(20).

data : var3 type string.

loop at itab.

var1 = itab-element.

var2 = itab-value.

concatenate var1 var2 into var3.

write : / var3.

endloop.

hope this solves your query.

Regards,

Kunal.

0 Kudos

Hi Markus,

I think first you have to read the internal table which condition where var1 are same.

Then concatenate the var1, var2 & var3.

You can do in single statement with loop if it supports where condition.

shylesh

Former Member
0 Kudos

hi Markus,

Here is the pseudo-code

sort jtab by Element.

jtab[] = itab[].

loop at itab.

read table jtab with key element = itab-element Binary Search.

concatinate itab-value jtab-value into variable.

endloop.

Regards,

Tanveer.

If your question is answered, please close the thread and mark helpful answers.

Former Member
0 Kudos

Try this Markus:


REPORT ztest NO STANDARD PAGE HEADING LINE-SIZE 255.

DATA: BEGIN OF itab OCCURS 0,
        element TYPE string,
        value   TYPE string,
      END   OF itab.

DATA: BEGIN OF itab2 OCCURS 0,
        element TYPE string,
        value   TYPE string,
      END   OF itab2.

DATA: next LIKE sy-tabix VALUE 1,
      old_element LIKE itab-element.

PERFORM load_itab.
sy-subrc = 0.

DO.
  READ TABLE itab INDEX next.
  IF sy-subrc <> 0.
    EXIT.
  ELSE.
    IF itab-element <> old_element.
      IF next <> 1.
        APPEND itab2.
        CLEAR itab2.
      ENDIF.
      itab2-element = itab-element.
      old_element = itab-element.
    ENDIF.
    CONCATENATE itab2-value itab-value INTO itab2-value.
    next = next + 1.
  ENDIF.
ENDDO.
IF itab2-element <> space.
  APPEND itab2.
ENDIF.

break rburbank.

*&---------------------------------------------------------------------*
*&      Form  load_itab
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM load_itab.

  REFRESH: itab,
           itab2.
  CLEAR  : itab,
           itab2.

  itab-element = 'test1'.
  itab-value   = '1111111111111111111'.
  APPEND itab.

  itab-element = 'test1'.
  itab-value   = '2222222222222222222'.
  APPEND itab.

  itab-element = 'test2'.
  itab-value   = '3333333333333333333'.
  APPEND itab.

  itab-element = 'test2'.
  itab-value   = '4444444444444444444'.
  APPEND itab.

ENDFORM.                    " load_itab

Rob

0 Kudos

Thx for all the responses

I solved it this way:


...
l_index = 1.
loop at lt_itab1 into ls_itab1 where element = xyz.
  if l_index > 1.
    concatenate l_string ls_itab1-value into l_string.
  else.
    l_string = ls_itab1-value.
  endif.
  l_index = l_index + 1.
endloop.

regards,

Markus