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

Use FOR instead of a LOOP AT

Former Member
0 Likes
4,522

I want to process each item in an itab - classic style:

LOOP AT itab INTO data(row).
  object->method( row ).
ENDLOOP.

Is there any way to do this with a FOR loop? I've tried so many options but the syntax check fails:

FOR row IN item object->method( row ).
DATA(t) = VALUE yresults_tt(
  FOR row IN itab object->method( row ).
).

Tried with and without LET etc.

1 ACCEPTED SOLUTION
Read only

MateuszAdamus
Active Contributor
4,178

Hello marccawood

It works for me as shown below.

CLASS lcl_class DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS:
      test
        IMPORTING iv_string        TYPE string
        RETURNING VALUE(rv_string) TYPE string.
ENDCLASS.

CLASS lcl_class IMPLEMENTATION.
  METHOD test.
    rv_string = |NEW: { iv_string }|.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  TYPES:
    BEGIN OF yls_string,
      value TYPE string,
    END OF yls_string,
    ylt_strings TYPE SORTED TABLE OF yls_string WITH NON-UNIQUE KEY value.

  DATA:
    ls_string TYPE yls_string,
    lt_strings TYPE ylt_strings.

  ls_string-value = |String 1|.
  APPEND ls_string TO lt_strings.
  ls_string-value = |String 2|.
  APPEND ls_string TO lt_strings.

  WRITE / 'Old'.
  LOOP AT lt_strings INTO ls_string.
    WRITE / ls_string-value.
  ENDLOOP.

  DATA(lt_results) = VALUE ylt_strings(
    FOR ls_string_tmp IN lt_strings
    (
      value = lcl_class=>test( ls_string_tmp-value )
    )
  ).

  WRITE / 'New'.
  LOOP AT lt_results INTO ls_string.
    WRITE / ls_string-value.
  ENDLOOP.

Result.

Kind regards,

Mateusz

8 REPLIES 8
Read only

Sandra_Rossi
Active Contributor
0 Likes
4,178

You use "FOR" with VALUE and other constructor operators only to construct an internal table. What does method return? A line to append to the constructed internal table?

Read only

Former Member
0 Likes
4,178

I don't need an internal table but the method does return a boolean which I could map to an internal table. Could you share an example since I could not find one example where a method is used to provide the value for a line in a table.

Tried many variations on this:

DATA(resulttab) = VALUE YRESULT_TT(
FOR DATA(row) IN itab ( res = object->method( row ) ) ).

Where YRESULT is a structure with one element res of type boolean.

Read only

MateuszAdamus
Active Contributor
4,179

Hello marccawood

It works for me as shown below.

CLASS lcl_class DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS:
      test
        IMPORTING iv_string        TYPE string
        RETURNING VALUE(rv_string) TYPE string.
ENDCLASS.

CLASS lcl_class IMPLEMENTATION.
  METHOD test.
    rv_string = |NEW: { iv_string }|.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  TYPES:
    BEGIN OF yls_string,
      value TYPE string,
    END OF yls_string,
    ylt_strings TYPE SORTED TABLE OF yls_string WITH NON-UNIQUE KEY value.

  DATA:
    ls_string TYPE yls_string,
    lt_strings TYPE ylt_strings.

  ls_string-value = |String 1|.
  APPEND ls_string TO lt_strings.
  ls_string-value = |String 2|.
  APPEND ls_string TO lt_strings.

  WRITE / 'Old'.
  LOOP AT lt_strings INTO ls_string.
    WRITE / ls_string-value.
  ENDLOOP.

  DATA(lt_results) = VALUE ylt_strings(
    FOR ls_string_tmp IN lt_strings
    (
      value = lcl_class=>test( ls_string_tmp-value )
    )
  ).

  WRITE / 'New'.
  LOOP AT lt_results INTO ls_string.
    WRITE / ls_string-value.
  ENDLOOP.

Result.

Kind regards,

Mateusz

Read only

Mohamed_Mukhtar
Active Contributor
0 Likes
4,178

Hi Marc Cawood,

Please find the below psuedo code. Hope it helps.

TYPES:
  BEGIN OF ty_customer,
    customer TYPE char10,
    name     TYPE char30,
    city     TYPE char30,
    route    TYPE char10,
  END   OF ty_customer.
TYPES: tt_customers TYPE SORTED TABLE OF ty_customer
          WITH UNIQUE KEY customer.


CLASS lcl_test_for DEFINITION.
  PUBLIC SECTION.
  METHODS get_customer IMPORTING im_cust TYPE char10
                       RETURNING VALUE(ex_cust) TYPE char10 .
ENDCLASS.

CLASS lcl_test_for IMPLEMENTATION.
  METHOD get_customer.
    ex_cust = im_cust.
  ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA O_lcl_test_for TYPE REF TO lcl_test_for.
CREATE OBJECT o_lcl_test_for.
* Appending table t_customers
DATA(t_customers) =
VALUE tt_customers(
  ( customer = 'C0001' name = 'Test Customer 1'  city = 'NY'  route = 'R0001' )
  ( customer = 'C0002' name = 'Customer 2'       city = 'LA'  route = 'R0003' )
  ( customer = 'C0003' name = 'Good Customer 3'  city = 'DFW' route = 'R0001' )
  ( customer = 'C0004' name = 'Best Customer 4'  city = 'CH'  route = 'R0003' )
  ( customer = 'C0005' name = 'So So Customer 5' city = 'NY'  route = 'R0001' )
  ( customer = 'C0006' name = 'new Customer 5'   city = 'CA'  route = 'R0001' )
).

*Populating new table using FOR
DATA(t_newcustomers) = VALUE tt_customers( FOR wa_cust IN t_customers
                                              (  customer = o_lcl_test_for->get_customer( im_cust = wa_cust-customer )
                                                 name     = wa_cust-name
                                                 city     = wa_cust-city
                                                 route    = wa_cust-city ).
Read only

BhargavaTanguturi
Active Participant
4,178

Try with REDUCE;

Data(lv_dummy) = REDUCE CHAR1(

INIT CHAR1 TYPE abap_bool

FOR row IN itab

NEXT lv_valid = object->method( row )

).

https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-us/abenconstructor_expression_reduce.htm

https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-us/abenreduce_simple_abexa.htm

Read only

0 Likes
4,178

Also a nice solution thanks!

Read only

pfefferf
Active Contributor
4,178

Following is a very simple example for your case (described in the comments above). Of course some adaptions will be necessary for your case, but the example should show you the general point.

CLASS zzfpcl_dummy DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    INTERFACES if_oo_adt_classrun.

  PROTECTED SECTION.
  PRIVATE SECTION.
    TYPES: BEGIN OF test_in,
             i TYPE i,
           END OF test_in,
           t_test_in TYPE TABLE OF test_in WITH EMPTY KEY,
           BEGIN OF test_res,
             b TYPE abap_boolean,
           END OF test_res,
           t_test_res TYPE TABLE OF test_res WITH EMPTY KEY.

    METHODS some_method
      IMPORTING in         TYPE test_in
      RETURNING VALUE(res) TYPE abap_boolean.

ENDCLASS.

CLASS zzfpcl_dummy IMPLEMENTATION.

  METHOD if_oo_adt_classrun~main.

    DATA(lt_data) = VALUE t_test_in( ( i = 1 ) ( i = 2 ) ).

    DATA(lt_result) = VALUE t_test_res( FOR ls_data IN lt_data ( b = me->some_method( ls_data ) ) ).

    out->write( lt_result ).

  ENDMETHOD.

  METHOD some_method.
    IF in-i > 1.
      res = abap_true.
    ELSE.
      res = abap_false.
    ENDIF.
  ENDMETHOD.

ENDCLASS.
Read only

Former Member
0 Likes
4,178

Thanks for all the examples. It seems in all cases the FOR must be part of a VALUE assignment.

My mistake was the DATA(row) declaration which seems to be implicit so this works now:

DATA(resulttab) = VALUE yresults_tt(
FOR row IN itab ( res = object->method( row ) )
).

Where yresults is a structure with a single boolean element/field called res.