Dorad
Dorad

Reputation: 3713

How to Iterate string_table and modify lines

For building a dynamic SELECT I am iterating over a field list and adding an alias.

However, that clears the list.

REPORT ZITER_TEST.

CONSTANTS lc_alias TYPE string VALUE 'ref'.

DATA lt_field TYPE string_table.

lt_field = VALUE string_table( 
                               ( CONV string( 'PERNR' ) )
                               ( CONV string( 'SUBTY' ) )
                             ).

" lt_field: ['PERNR','SUBTY']

lt_field = VALUE string_table( FOR <lv_tmp> IN lt_field
                                 ( lc_alias && `~` && <lv_tmp> )
                             ).
" lt_field: [] instead of: ['ref~PERNR','ref~SUBTY']

Upvotes: 0

Views: 1320

Answers (3)

Suncatcher
Suncatcher

Reputation: 10621

Looping table with field-symbol will be significantly faster than using temporary table in either form (LET or direct assign):

LOOP AT lt_field ASSIGNING FIELD-SYMBOL(<field>)
 <field> = lc_alias && `~` && <field>.
ENDLOOP.

Upvotes: 0

Sandra Rossi
Sandra Rossi

Reputation: 13639

The ABAP documentation of VALUE says:

In assignments to a data object, the target variable is used directly and no temporary data object is created. This variable is initialized or overwritten in full before the assignment of the values specified in the parentheses. Its original value, however, is still available in an optional LET expression.

So, instead of:

lt_field = VALUE string_table( FOR <lv_tmp> IN lt_field
                                 ( lc_alias && `~` && <lv_tmp> )
                             ).

Use:

lt_field = VALUE string_table( LET lt_field_temp = lt_field IN
                               FOR <lv_tmp> IN lt_field_temp
                                 ( lc_alias && `~` && <lv_tmp> )
                             ).

Upvotes: 4

Artur
Artur

Reputation: 125

You just have to use some temporary table. It works in my case:

REPORT ZITER_TEST.

CONSTANTS lc_alias TYPE string VALUE 'ref'.

DATA: lt_field_temp TYPE string_table,
      lt_field      TYPE string_table.

lt_field_temp = VALUE string_table(
                               ( CONV string( 'PERNR' ) )
                               ( CONV string( 'SUBTY' ) )
                             ).

lt_field = VALUE string_table( FOR <lv_tmp> IN lt_field_temp
                                 ( lc_alias && '~' && <lv_tmp> )
                             ).

Upvotes: 1

Related Questions