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: 

String conctenation/merge using FOR loop inside REDUCE

Nitish2027
Participant
0 Kudos
708

Hi Experts,

I recently came across a similar question in the forum and wanted to build a logic for it using REDUCE and FOR.

I tried many things but I have a few doubts that I would like to get your opinion on.

Below is the code with comments on exact requirement and doubts as well.

**********************************************************************
    " To merge two strings as 2 characters of first and then 2 characters of second
    " and then again 2 characters of first string and so on, until the end of both the strings.
    " Examples: str1 = 'first', str2 = 'second' then result = 'fiserscotnd'.
    "           str1 = 'home', str2 = 'book' then result = 'hobomeok'.
    "           str1 = 'hello', str2 = 'darkness' then result = 'hedallrkoness'.
    "           str1 = 'super', str2 = 'star' then result = 'sustpearr'.


    DATA: p1 TYPE string VALUE 'home',
          p2 TYPE string VALUE 'book'.

    DATA(lv_len1) = strlen( p1 ).
    DATA(lv_len2) = strlen( p2 ).
    DATA(lv_len) = lv_len1 + lv_len2.

    "Expected Result is hobomeok for this case.

**********************************************************************
    " I have many doubts in case of FOR inside REDUCE. I tried this as my first try.
    " But I think here text1 if going through the complete FOR loop and the text2 is starting to
    " fill only after text1 has already stored the value 'home' by iterating through FOR twice
    " as the length is 4 chars. So when at last text gets filled, it gives the result 'homebook'.

    DATA(result1) = REDUCE string( INIT text = ``
                                       text1 = ``
                                       text2 = ``
                      FOR i = 0 THEN i + 2 WHILE i <= lv_len
                      NEXT text1 = COND #( WHEN i < lv_len1
                                          THEN text1 && substring( val = p1 off = i len = 2 )
                                          ELSE text1 )
                           text2 = COND #( WHEN i < lv_len2
                                          THEN text2 && substring( val = p2 off = i len = 2 )
                                          ELSE text2 )
                           text = text1 && text2
                                          ).
    out->write( result1 ). "homebook

**********************************************************************.
    " In my second try I get the correct result, but this works only when both the strings
    " are of same length. Because if one string has bigger length than the other, it creates
    " a runtime error as the offset in the substring function becomes higher than the length
    " of the shorter string.

    DATA(result2) = REDUCE string( INIT text = ``
                      FOR i = 0 THEN i + 2 WHILE i <= lv_len
                      NEXT text = COND #( WHEN i < lv_len1 AND i < lv_len2
                                          THEN text && substring( val = p1 off = i len = 2 )
                                                    && substring( val = p2 off = i len = 2 )
                                          ELSE text )
                                          ).
    out->write( result2 ). "hobomeok

**********************************************************************
    " In my third try I thought of managing the length of each string individually to remove
    " the run time error. But then in this case it always takes the first when and gives the
    " value stored in the first string only.

    DATA(result3) = REDUCE string( INIT text = ``
                      FOR i = 0 THEN i + 2 WHILE i <= lv_len
                      NEXT text = COND #( WHEN i < lv_len1
                                          THEN text && substring( val = p1 off = i len = 2 )
                                          WHEN i < lv_len2
                                          THEN text && substring( val = p2 off = i len = 2 )
                                          ELSE text )
                                          ).
    out->write( result3 ). "home

**********************************************************************
    " I know I'm close but just unable to figure this out. I think I should be able to do
    " this by using REDUCE and FOR.

**********************************************************************

It would be great if someone can guide me with some suggestion where I'm going wrong and I would try to implement the new logic.

Thanks 🙂

1 REPLY 1

Sandra_Rossi
Active Contributor
0 Kudos
575

You should first write the algorithm on "paper" and play it. It will be much easier after that.