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

dynamic columns

luscruz
Participant
0 Likes
513

Hi all,

Suppose i have this code:

<b>loop at table into line.

....

if line-column1 eq 'something'.

....

endloop.</b>

with that i search if the column line-column1 is equal to 'something', and if it is i do something. That works fine, but now i'm facing a problem.

The column that i need to search may be other of about 60 columns. It's the column i get in a string. For instance:

<b>data: text type string.

text = 'column1'.</b>

Is anyway to somehow get the value of a column by a string? In other words:

- if <b>text='column1'</b> the validation should look as <b>if line-column1 eq 'something'.</b>

- if <b>text='column2'</b> the validation should look as <b>if line-column2 eq 'something'.</b>

If the table had few columns a simple case/when, or even if/else would work, but this is a table with more than 60 columns, so it's an option i'm trying to avoid.

Most likelly i need to store the value of the column i want to search in a string and then use it to compare in IF, but i don't know how.

Some people told me that it might me done with field-symbols and appends, but since i'm kind of newbie in abap world i'm not managing to do that.

Can anyone help me with this?

Thanks in advanced for any help.

1 ACCEPTED SOLUTION
Read only

former_member186741
Active Contributor
0 Likes
480

field-symbols <col> type any.

loop at table into line.

assign component (text) of structure line to <col>.

if <col> eq 'something'.

....

endloop.

3 REPLIES 3
Read only

uwe_schieferstein
Active Contributor
0 Likes
480

Hello Luis

You are already quite close to the solution to your problem.

DATA:
  ld_column_1   TYPE string,
  ld_column_2   TYPE string.

FIELD-SYMBOLS:
  <ls_line>     TYPE any,
  <ld_column>   TYPE any.

  ld_column_1 = 'COL_1'.  " upper case !!!
  ld_column_2 = 'COL_2'.  " upper case !!!

LOOP AT itab ASSIGNING <ls_line>.
* First column
  UNASSIGN: <ld_column>.
  ASSIGN COMPONENT (ld_column_1) OF STRUCTURE <ls_line>
    TO <ld_column>.
  IF ( <ld_column> IS ASSIGNED ).
*   Do something if condition is fulfilled
    IF ( <ld_column> eq 'something' ).
    ...
    ENDIF. 
  ENDIF.

* Second column
  UNASSIGN: <ld_column>.
  ASSIGN COMPONENT (ld_column_2) OF STRUCTURE <ls_line>
    TO <ld_column>.
  IF ( <ld_column> IS ASSIGNED ).
*   Do something if condition is fulfilled
    IF ( <ld_column> eq 'something' ).
    ...
    ENDIF. 
  ENDIF.  

ENDLOOP.

Regards

Uwe

Read only

former_member186741
Active Contributor
0 Likes
481

field-symbols <col> type any.

loop at table into line.

assign component (text) of structure line to <col>.

if <col> eq 'something'.

....

endloop.

Read only

0 Likes
480

Thank you both, you were very helpfull !!

Just having a problem, but already saw what was...

In line:

.

Thanks for the great help !!

Luís Cruz

Message was edited by: Luís Cruz