0

Need idea on the below codes, on how to simplify. Codes below works good but is there a way I could enhance or shorten the codes making it dynamic?

    TYPES: BEGIN OF lty_dates,
             yesterday TYPE string,
             today     TYPE string,
             tomorrow  TYPE string,
           END OF lty_dates.

    DATA: it_table TYPE TABLE OF lty_dates.

    DO 3 TIMES.
      CASE lv_count.
        WHEN 1.
          it_table[ 1 ]-zyesterday = 'Result Yesterday'.
          it_table[ 2 ]-zyesterday = 'Result Yesterday'.
          it_table[ 3 ]-zyesterday = 'Result Yesterday'.

        WHEN 2.
          it_table[ 1 ]-ztoday = 'Result Today'.
          it_table[ 2 ]-ztoday = 'Result today'.
          it_table[ 3 ]-ztoday = 'Result Today'.

        WHEN 3.
          it_table[ 1 ]-ztommorrow = 'Result Tomorrow'.
          it_table[ 2 ]-ztommorrow = 'Result Tomorrow'.
          it_table[ 3 ]-ztommorrow = 'Result Tomorrow'.
      ENDCASE.

      lv_count = lv_count + 1.

    ENDDO.

My idea is something like the below pseudocodes. I would not want to perform CASE multiple times specially instances if fields for it_table would reach 100 (fields).

   DO 3 TIMES.
      ASSIGN 'ZTODAY' TO <dynamic_fieldname>.
      it_table[ 1 ]-<dynamic_fieldname> = <dynamic_result>.
      it_table[ 2 ]-<dynamic_fieldname> = <dynamic_result>.
      it_table[ 3 ]-<dynamic_fieldname> = <dynamic_result>.
    ENDDO.

Please help or light me up on this.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Czarinaaaaa29
  • 311
  • 2
  • 8
  • 23
  • What exactly do you want to make more dynamic? – vwegert Dec 14 '17 at 20:33
  • @vwegert I have similar mapping1 to get the results (yesterday, today, tomorrow)for row1. Another mapping2 to get results for row2, then another mapping3 to get the results for row3. So for every row, I will have different mappings to get the results but each mapping is applicable to get yesterday, today and tomorrow values per row. I think, the code provided assumes similar mapping for yesterday column, today column and tomorrow column, which does not satisfy my design... ASSIGN COMPONENT syntax might have something todo. i have to play around on it as well. – Czarinaaaaa29 Dec 15 '17 at 08:44

2 Answers2

2

You could use the command ASSIGN COMPONENT compname OF STRUCTURE structure TO <field_symbol>.

TYPES: BEGIN OF lty_dates,
         yesterday TYPE string,
         today     TYPE string,
         tomorrow  TYPE string,
       END OF lty_dates.

TYPES: BEGIN OF lty_result ,
         fieldname TYPE fieldname,
         result    TYPE string,
       END OF lty_result .

FIELD-SYMBOLS <data> TYPE any .

DATA: lt_table  TYPE TABLE OF lty_dates,
      lt_result TYPE TABLE OF lty_result.

lt_result = VALUE #( ( fieldname = 'yesterday'
                       result    = 'Result Yesterday' )
                     ( fieldname = 'today'
                       result    = 'Result Today' )
                     ( fieldname = 'tomorrow'
                       result    = 'Result Tomorrow' ) ).

lt_table = VALUE #( (  ) (  ) (  ) ). " 3 empty lines

LOOP AT lt_table ASSIGNING FIELD-SYMBOL(<table>) .
  LOOP AT lt_result ASSIGNING FIELD-SYMBOL(<result>) .
    ASSIGN COMPONENT <result>-fieldname OF STRUCTURE <table> TO <data> .
    <data> = <result>-result.
  ENDLOOP .
ENDLOOP .
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Michael Meyer
  • 2,179
  • 3
  • 24
  • 33
  • I have similar mapping1 to get the results (yesterday, today, tomorrow)for row1. Another mapping2 to get results for row2, then another mapping3 to get the results for row3. So for every row, I will have different mappings to get the results but each mapping is applicable to get yesterday, today and tomorrow values per row. I think, the code provided assumes similar mapping for yesterday column, today column and tomorrow column, which does not satisfy my design... ASSIGN COMPONENT syntax might have something todo. i have to play around on it as well. – Czarinaaaaa29 Dec 15 '17 at 08:43
1

Actually, your code creates this result table:

YESTERDAY           TODAY           TOMORROW
Result Yesterday    Result Today    Result Tomorrow
Result Yesterday    Result today    Result Tomorrow
Result Yesterday    Result Today    Result Tomorrow

Why not to use macro for this? Macro can perfectly fulfill your needs:

FIELD-SYMBOLS: <fvalue> TYPE ANY.
DEFINE put_values.
  ASSIGN COMPONENT &1 OF STRUCTURE &2 TO <fvalue>.
  <fvalue> = &3.
END-OF-DEFINITION.

it_table = VALUE #( (  ) (  ) (  ) ).

LOOP AT it_table ASSIGNING FIELD-SYMBOL(<fs_tab>).
  put_values 'yesterday' <fs_tab> 'Result Yesterday'.
  put_values 'today' <fs_tab> 'Result Today'.
  put_values 'tomorrow' <fs_tab> 'Result Tomorrow'.
ENDLOOP.

This will work if the number if the loop iterations are equal to lines of itab (as in your code).

Suncatcher
  • 10,355
  • 10
  • 52
  • 90