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: 

Retrieving the union of letters from two or more character strings

Former Member
0 Kudos

Dear Gurus,

Need your help in writing ABAP logic for the below requirement.

Assume we have Var1 = 'ABCD' , Var2 = 'CDEF' and Var3 = 'EFGH'. The requirement is to populate Var4 with union of the letters from the above three strings. That means, Var4 should be equivalent to 'ABCDEFGH' after the logic is executed. Can any body help in writing this logic.

Appreciate your quick help.

Thanks

Aryan

2 REPLIES 2

Former Member
0 Kudos

hi,

For dynamic values you could go for Field Symbols.

for example,

Field-symbols: <fs> type any.

concatenate var1 var2 var3 into <fs> with no space.

<fs> will hav ABCDEFGH.

rewards if useful.

Regards,

swati

Former Member
0 Kudos

Here's an algorithm that might work for you.. I'm sure there are plenty more!

report zlocal_jc_union.

parameters:
  p_var1(10)            type c default 'ABCD',
  p_var2(10)            type c default 'CDEF',
  p_var3(10)            type c default 'EFGH'.

start-of-selection.
  perform logic.

*&---------------------------------------------------------------------*
*&      Form  logic
*&---------------------------------------------------------------------*
form logic.
* "Work out unique letters and keep them in sequence.
  data:
    l_union(300)        type c,
    l_byte(1)           type c,
    l_offset            type i.

  concatenate
    p_var1
    p_var2
    p_var3 into l_union. "append them together

  format reset.
  write: / 'Before:', l_union(80).
*" then remove duplicates, one at a time...
  do.
    l_byte = l_union+l_offset(1).
    do.
      replace l_byte with space into l_union.
      if not sy-subrc is initial. "no more to replace
        exit.
      endif.
    enddo.
    l_union+l_offset(1) = l_byte. "replace original value
    condense l_union no-gaps.
    write: / 'Offset', l_offset, l_union(80).

    add 1 to l_offset.
    if l_offset = strlen( l_union ). "run out of bytes
      exit.
    endif.
  enddo.

endform.                    "logic