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

reference fields with a variable

Former Member
0 Likes
575

Hi guys,

I have the following code:

data: begin of stdata,

zlis01 type p decimals 2,

zlis02 type p decimals 2,

zlis03 type p decimals 2,

zlis04 type p decimals 2,

zlis05 type p decimals 2,

zlis06 type p decimals 2,

zlis07 type p decimals 2,

zlis08 type p decimals 2,

zlis09 type p decimals 2,

zlis10 type p decimals 2,

....etc, until zlis40.

end of stdata.

How can I reference each field in a loop sentence using a variable, without having to write the same code for each field?

Is there a way to reference each field with a string variable? Like

concatenate zlis '01' into v_index.

stdata-v_index = some_value.

I have to write a lot of code that is the same for each field.

Thanks

1 ACCEPTED SOLUTION
Read only

uwe_schieferstein
Active Contributor
0 Likes
524

Hello Javier

The problem is that you cannot say:

  IF <fs_campo> = 0.   " comparing floating point with integer
...
  ENDIF.

Because <fs_campo> is of type P (with decimals) whereas 0 is an integer.

Here is a sample coding that works:

*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_FIELD_SYMBOL_1
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zus_sdn_field_symbol_1.

DATA: BEGIN OF stdata,
zlis01 TYPE p DECIMALS 2,
zlis02 TYPE p DECIMALS 2,
zlis03 TYPE p DECIMALS 2,
zlis04 TYPE p DECIMALS 2,
zlis05 TYPE p DECIMALS 2,
zlis06 TYPE p DECIMALS 2,
zlis07 TYPE p DECIMALS 2,
zlis08 TYPE p DECIMALS 2,
zlis09 TYPE p DECIMALS 2,
zlis10 TYPE i,               " integer !!!
END OF stdata.

FIELD-SYMBOLS:
 <gd_value>  TYPE ANY.


START-OF-SELECTION.

  stdata-zlis01 = '1.5'.
  stdata-zlis05 = '0.4'.
  stdata-zlis09 = '10.0'.


  DO.
    ASSIGN COMPONENT syst-index OF STRUCTURE stdata TO <gd_value>.
    IF ( syst-subrc NE 0 ).
      EXIT.
    ENDIF.

    CASE syst-index.
      WHEN '1'.
        IF ( <gd_value> = '0' ).  " comparing floating point values
          WRITE: / syst-index, 'is initial (0)'.
        ENDIF.

      WHEN '2'.
        IF ( <gd_value> = '0' ).
          WRITE: / syst-index, 'is initial (0)'.
        ENDIF.

      WHEN '10'.
        IF ( <gd_value> = 0 ).  " comparing integer values
          WRITE: / syst-index, 'is initial (0)'.
        ENDIF.

      WHEN OTHERS.
    ENDCASE.

    WRITE: / syst-index, 'Value =', <gd_value>.
  ENDDO.

END-OF-SELECTION.

Regards

Uwe

4 REPLIES 4
Read only

Former Member
0 Likes
524

hi javier,

Yes there are way to suit your requirement.

But can you be clear as what value you want to pass to each parameter. Do you want to pass same value to all 40 fields or there is some different value to each field. If it is different value then any way you need to assign different 40 statements so no need to go for any logic.

But still you want logic you can do like this

data l_index(2) type n.

field-symbols <fs>.

data l_field type string.

DO 40 times.

l_index = sy-index.

concatenate 'STDATA-ZSLIS' l_index into l_field.

condense l_field.

assign l_field to <fs>.

ENDDO.

Reward points if useful.

Regards,

Atish

Read only

0 Likes
524

hello Atish,

It seem is works, but how can I read the value contained in <fs>. I am trying to do this

if <fs_campo> = 0.

....

endif.

but during run-time, the system got an error: Unable to interpret "STDATA-ZLIS01" as a number.

Read only

Former Member
0 Likes
524

You may be able to use DO ... VARYING. From the help:


DATA: BEGIN OF WORD, 
        ONE   VALUE 'E', 
        TWO   VALUE 'x', 
        THREE VALUE 'a', 
        FOUR  VALUE 'm', 
        FIVE  VALUE 'p', 
        SIX   VALUE 'l', 
        SEVEN VALUE 'e', 
        EIGHT VALUE '!', 
      END   OF WORD, 
      LETTER1, LETTER2. 
DO VARYING LETTER1 FROM WORD-ONE NEXT WORD-THREE 
   VARYING LETTER2 FROM WORD-TWO NEXT WORD-FOUR. 
  WRITE: LETTER1, LETTER2. 
  IF LETTER2 = '!'. 
    EXIT. 
  ENDIF. 
ENDDO

Rob

Read only

uwe_schieferstein
Active Contributor
0 Likes
525

Hello Javier

The problem is that you cannot say:

  IF <fs_campo> = 0.   " comparing floating point with integer
...
  ENDIF.

Because <fs_campo> is of type P (with decimals) whereas 0 is an integer.

Here is a sample coding that works:

*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_FIELD_SYMBOL_1
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zus_sdn_field_symbol_1.

DATA: BEGIN OF stdata,
zlis01 TYPE p DECIMALS 2,
zlis02 TYPE p DECIMALS 2,
zlis03 TYPE p DECIMALS 2,
zlis04 TYPE p DECIMALS 2,
zlis05 TYPE p DECIMALS 2,
zlis06 TYPE p DECIMALS 2,
zlis07 TYPE p DECIMALS 2,
zlis08 TYPE p DECIMALS 2,
zlis09 TYPE p DECIMALS 2,
zlis10 TYPE i,               " integer !!!
END OF stdata.

FIELD-SYMBOLS:
 <gd_value>  TYPE ANY.


START-OF-SELECTION.

  stdata-zlis01 = '1.5'.
  stdata-zlis05 = '0.4'.
  stdata-zlis09 = '10.0'.


  DO.
    ASSIGN COMPONENT syst-index OF STRUCTURE stdata TO <gd_value>.
    IF ( syst-subrc NE 0 ).
      EXIT.
    ENDIF.

    CASE syst-index.
      WHEN '1'.
        IF ( <gd_value> = '0' ).  " comparing floating point values
          WRITE: / syst-index, 'is initial (0)'.
        ENDIF.

      WHEN '2'.
        IF ( <gd_value> = '0' ).
          WRITE: / syst-index, 'is initial (0)'.
        ENDIF.

      WHEN '10'.
        IF ( <gd_value> = 0 ).  " comparing integer values
          WRITE: / syst-index, 'is initial (0)'.
        ENDIF.

      WHEN OTHERS.
    ENDCASE.

    WRITE: / syst-index, 'Value =', <gd_value>.
  ENDDO.

END-OF-SELECTION.

Regards

Uwe