<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic String conctenation/merge using FOR loop inside REDUCE in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/string-conctenation-merge-using-for-loop-inside-reduce/m-p/12571045#M2008491</link>
    <description>&lt;P&gt;Hi Experts,&lt;/P&gt;
  &lt;P&gt;I recently came across a similar question in the forum and wanted to build a logic for it using REDUCE and FOR.&lt;/P&gt;
  &lt;P&gt;I tried many things but I have a few doubts that I would like to get your opinion on. &lt;/P&gt;
  &lt;P&gt;Below is the code with comments on exact requirement and doubts as well.&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;**********************************************************************
    " 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 &amp;lt;= lv_len
                      NEXT text1 = COND #( WHEN i &amp;lt; lv_len1
                                          THEN text1 &amp;amp;&amp;amp; substring( val = p1 off = i len = 2 )
                                          ELSE text1 )
                           text2 = COND #( WHEN i &amp;lt; lv_len2
                                          THEN text2 &amp;amp;&amp;amp; substring( val = p2 off = i len = 2 )
                                          ELSE text2 )
                           text = text1 &amp;amp;&amp;amp; text2
                                          ).
    out-&amp;gt;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 &amp;lt;= lv_len
                      NEXT text = COND #( WHEN i &amp;lt; lv_len1 AND i &amp;lt; lv_len2
                                          THEN text &amp;amp;&amp;amp; substring( val = p1 off = i len = 2 )
                                                    &amp;amp;&amp;amp; substring( val = p2 off = i len = 2 )
                                          ELSE text )
                                          ).
    out-&amp;gt;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 &amp;lt;= lv_len
                      NEXT text = COND #( WHEN i &amp;lt; lv_len1
                                          THEN text &amp;amp;&amp;amp; substring( val = p1 off = i len = 2 )
                                          WHEN i &amp;lt; lv_len2
                                          THEN text &amp;amp;&amp;amp; substring( val = p2 off = i len = 2 )
                                          ELSE text )
                                          ).
    out-&amp;gt;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.

**********************************************************************&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;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. &lt;/P&gt;
  &lt;P&gt;Thanks &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 05 Sep 2022 06:28:37 GMT</pubDate>
    <dc:creator>Nitish2027</dc:creator>
    <dc:date>2022-09-05T06:28:37Z</dc:date>
    <item>
      <title>String conctenation/merge using FOR loop inside REDUCE</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/string-conctenation-merge-using-for-loop-inside-reduce/m-p/12571045#M2008491</link>
      <description>&lt;P&gt;Hi Experts,&lt;/P&gt;
  &lt;P&gt;I recently came across a similar question in the forum and wanted to build a logic for it using REDUCE and FOR.&lt;/P&gt;
  &lt;P&gt;I tried many things but I have a few doubts that I would like to get your opinion on. &lt;/P&gt;
  &lt;P&gt;Below is the code with comments on exact requirement and doubts as well.&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;**********************************************************************
    " 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 &amp;lt;= lv_len
                      NEXT text1 = COND #( WHEN i &amp;lt; lv_len1
                                          THEN text1 &amp;amp;&amp;amp; substring( val = p1 off = i len = 2 )
                                          ELSE text1 )
                           text2 = COND #( WHEN i &amp;lt; lv_len2
                                          THEN text2 &amp;amp;&amp;amp; substring( val = p2 off = i len = 2 )
                                          ELSE text2 )
                           text = text1 &amp;amp;&amp;amp; text2
                                          ).
    out-&amp;gt;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 &amp;lt;= lv_len
                      NEXT text = COND #( WHEN i &amp;lt; lv_len1 AND i &amp;lt; lv_len2
                                          THEN text &amp;amp;&amp;amp; substring( val = p1 off = i len = 2 )
                                                    &amp;amp;&amp;amp; substring( val = p2 off = i len = 2 )
                                          ELSE text )
                                          ).
    out-&amp;gt;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 &amp;lt;= lv_len
                      NEXT text = COND #( WHEN i &amp;lt; lv_len1
                                          THEN text &amp;amp;&amp;amp; substring( val = p1 off = i len = 2 )
                                          WHEN i &amp;lt; lv_len2
                                          THEN text &amp;amp;&amp;amp; substring( val = p2 off = i len = 2 )
                                          ELSE text )
                                          ).
    out-&amp;gt;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.

**********************************************************************&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;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. &lt;/P&gt;
  &lt;P&gt;Thanks &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Sep 2022 06:28:37 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/string-conctenation-merge-using-for-loop-inside-reduce/m-p/12571045#M2008491</guid>
      <dc:creator>Nitish2027</dc:creator>
      <dc:date>2022-09-05T06:28:37Z</dc:date>
    </item>
    <item>
      <title>Re: String conctenation/merge using FOR loop inside REDUCE</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/string-conctenation-merge-using-for-loop-inside-reduce/m-p/12571046#M2008492</link>
      <description>&lt;P&gt;You should first write the algorithm on "paper" and play it. It will be much easier after that.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Sep 2022 07:33:19 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/string-conctenation-merge-using-for-loop-inside-reduce/m-p/12571046#M2008492</guid>
      <dc:creator>Sandra_Rossi</dc:creator>
      <dc:date>2022-09-05T07:33:19Z</dc:date>
    </item>
  </channel>
</rss>

