Supported by

Modify one field in all lines of an internal table

What I’m about to show you is not exactly new. It has even been used in Abapinho before. But since there is still a lot of people out there using LOOP to change a single field of an internal table, I thought it would be worth talking about it.

Say you have an internal table with one million and two hundred thousand entries and want to change the field ICON so that it has ‘@FM@‘ in all lines.

Instead of doing this:

LOOP AT lt_data ASSIGNING <data>.
  <data>-icon = '@FM@'.

Try doing this instead:

ls_data-icon = '@FM@‘.
MODIFY lt_data FROM ls_data TRANSPORTING icon WHERE icon <> ‘DONALD DUCK’.

You can obviously replace the condition value with any other Disney character.

Thank you Sérgio Serra for suggesting that it could be interesting talking about this.
Thank you Andrew Becraft for the photo.

Greetings from Abapinho.

Um comentário a “Modify one field in all lines of an internal table”

  1. Leandro Diz:

    Muito boa essa dica. Uma outra dica boa que poderia dar é do uso do comando CONCATENATE LINES OF pois ainda vejo muitos abaps usando da maneira antiga, criei um exemplo se quiser publicar fique a vontade:

    *& Programa exemplo do CONCATENATE LINES OF
    *& Autor: Leandro Gambim

    REPORT zteste_gambim_lines NO STANDARD PAGE HEADING.

    TYPES: BEGIN OF ty_output,
    field1 TYPE char10,
    field2 TYPE char10,
    field3 TYPE char10,
    END OF ty_output.

    DATA: gt_output_1 TYPE TABLE OF ty_output,
    gt_output_2 TYPE TABLE OF ty_output,
    lw_output_1 TYPE ty_output,
    lv_output_1 TYPE string,
    lv_output_2 TYPE string.


    ” preenche a primeira tabela
    DO 5 TIMES.
    CLEAR lw_output_1.
    lw_output_1-field1 = ‘- Campo 01’.
    lw_output_1-field2 = ‘- Campo 02’.
    lw_output_1-field3 = ‘- Campo 03′.
    APPEND lw_output_1 TO gt_output_1.

    ” equaliza as duas tabelas
    gt_output_2 = gt_output_1.

    ” opção 1, ler a tabela e ir concatenando
    LOOP AT gt_output_1 INTO lw_output_1.
    CONCATENATE lv_output_1 lw_output_1-field1 lw_output_1-field2 lw_output_1-field3
    INTO lv_output_1 SEPARATED BY space.

    ” opção 2, concatenar diretamente
    CONCATENATE LINES OF gt_output_2 INTO lv_output_2 SEPARATED BY space.

    ” Exibe os dados em tela
    WRITE: /’Opção 1:’, lv_output_1.
    WRITE: /’Opção 2:’, lv_output_2.

Deixe um comentário

About Abapinho
Abapinho runs on WordPress
Articles (RSS) e Comments (RSS).