Application Development and Automation 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: 
Read only

Logic within Loop

Former Member
0 Likes
493

Hi I have a table with following columns.

WBS, fiscal year, month1, month2 till month12.

In selection screen, user will enter WBS, year and particular month.

If user enters 5, my program should find out the value from ITAB-MONTH5.

If user enters 12, my program should find out the value from ITAB-MONTH12.

loop at itab.

endloop.

How to incorporate this logic?

1 ACCEPTED SOLUTION
Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
466

When thinking about dynamically addressing internal table fields always think ASSIGN COMPONENT

DATA: v_month TYPE char2,
      v_field TYPE fieldname.

FIELD-SYMBOLS: <val> TYPE ANY.

v_month = p_month.

SHIFT v_month LEFT DELETING LEADING '0'.
CONCATENATE 'MONTH' v_month INTO v_field.
CONDENSE v_field NO-GAPS.

LOOP AT itab.
  ASSIGN COMPONENT v_field OF STRUCTURE itab TO <val>.
ENDLOOP.

BR,

Suhas

4 REPLIES 4
Read only

Former Member
0 Likes
466

Hi Pawan,

Use case statement inside the loop.

Loop at itab.

Case Month.

When '1'.

Move itab-month1 to xyz.

when '2'

move itab-month2 to xyz.

''

''

when '12'.

move itab-month12 to xyz.

endcase.

endloop.

Hope this works.

Thanks,

Venkat

Read only

Former Member
0 Likes
466

Hi,

Say ur inputs like this

P_WBS

P_YEAR

P_MONTH

The logic would be like this,

Read table ITAB into WA_TAB with key WBS = P_WBS

YEAR = p_YEAR

CASE P_MONTH.

WHEN '1'.

MOVE WA_TAB-MONTH1 TO V_MONTH_VALUE

WHEN '2'

MOVE WA_TAB-MONTH2 TO V_MONTH_VALUE

-

-

-

-

WHEN '12'

MOVE WA_TAB-MONTH12 TO V_MONTH_VALUE

WHEN OTHERS.

Now V_MONTH_VALUE has ur required value.

Regards,

Selva

Read only

bbalci
Contributor
0 Likes
466

Hi Pavan,

Check this code :

REPORT  zform1.

PARAMETERS : p_fnum(2) TYPE n.
      

FIELD-SYMBOLS <f_field> TYPE any.
data lv_fname(20).

START-OF-SELECTION.


  LOOP AT itab.
    
    CONCATENATE 'ITAB-MONTH' p_fnum INTO lv_fname.    "<--- get related field by parameter p_fnum 
    ASSIGN COMPONENT lv_fname OF STRUCTURE itab to <f_field>.
    
    if <f_field> EQ ....    " <-- use in your if condition
      
        APPEND itab2. " <--- say itab2 is your search result table
    endif.
    
    


  ENDLOOP.

Edited by: Bulent Balci on Aug 17, 2010 1:22 PM

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
467

When thinking about dynamically addressing internal table fields always think ASSIGN COMPONENT

DATA: v_month TYPE char2,
      v_field TYPE fieldname.

FIELD-SYMBOLS: <val> TYPE ANY.

v_month = p_month.

SHIFT v_month LEFT DELETING LEADING '0'.
CONCATENATE 'MONTH' v_month INTO v_field.
CONDENSE v_field NO-GAPS.

LOOP AT itab.
  ASSIGN COMPONENT v_field OF STRUCTURE itab TO <val>.
ENDLOOP.

BR,

Suhas