cancel
Showing results for 
Search instead for 
Did you mean: 

LOOP AT a table with with another table to get all matched ID's listed

maleodillet
Participant
0 Kudos

Hi Gurus,

I have an issue with my code. I am trying to LOOP AT the contents of one table up against the contents of another table and retrieve all the matching results into a variable that I can further evaluate, this is the code:

METHOD get_owner_property_validation2.
TYPES: BEGIN OF lty_team,
team_mbr(20) TYPE c,
END OF lty_team,

BEGIN OF ty_string,
str(25) TYPE c,
END OF ty_string.

DATA: lt_teams TYPE STANDARD TABLE OF uj_team_id,
lt_teams_mbr TYPE STANDARD TABLE OF lty_team,
ls_teams_mbr TYPE lty_team,
lt_leading_teams TYPE STANDARD TABLE OF uj_team_id,
ls_leading_teams TYPE uj_team_id,
lv_uj_user_id TYPE uj_user_id,
lo_user_dao TYPE REF TO cl_uje_user_dao,
lo_dim_reader TYPE REF TO cl_uja_dim,
dyn_table TYPE REF TO data,
lt_dim_data TYPE REF TO data,
ls_mbr TYPE uj_attr_name,
lt_attr_list TYPE uja_t_attr_name,
lv_member TYPE ujw_owner_mgr,
it_string TYPE TABLE OF ty_string,
wa_string TYPE ty_string.

FIELD-SYMBOLS: <fs_t_txt_data> TYPE STANDARD TABLE,
<fs_s_txt_data> TYPE any,
<lv_val> TYPE any.

TRY.
CREATE OBJECT lo_dim_reader
EXPORTING
i_appset_id = i_appset_id
i_dimension = i_dimension.
CATCH cx_uja_admin_error .
ENDTRY.

ls_mbr = i_member.
APPEND ls_mbr TO lt_attr_list.

TRY.
CALL METHOD lo_dim_reader->if_uja_dim_data~read_mbr_data
EXPORTING
it_attr_list = lt_attr_list
IMPORTING
er_data = lt_dim_data.
CATCH cx_uja_admin_error.
ENDTRY.

TRY.
CALL METHOD lo_dim_reader->if_uja_dim_data~read_mbr_data
EXPORTING
it_attr_list = lt_attr_list
IMPORTING
er_data = lt_dim_data.
CATCH cx_uja_admin_error.
ENDTRY.

ASSIGN lt_dim_data->* TO <fs_t_txt_data>.

LOOP AT <fs_t_txt_data> ASSIGNING <fs_s_txt_data>.
ASSIGN COMPONENT 'ID' OF STRUCTURE <fs_s_txt_data> TO <lv_val>.
IF <lv_val> IS ASSIGNED.
IF <lv_val> = i_id.
UNASSIGN <lv_val>.
ASSIGN COMPONENT i_member OF STRUCTURE <fs_s_txt_data> TO <lv_val>.
lv_member = <lv_val>.

REPLACE ALL OCCURRENCES OF '[' IN lv_member WITH ''.
REPLACE ALL OCCURRENCES OF ']' IN lv_member WITH ''.

REFRESH it_string.
SPLIT lv_member AT ',' INTO TABLE it_string.

lv_uj_user_id = sy-uname.

CREATE OBJECT lo_user_dao
EXPORTING
i_appset_id = i_appset_id.

TRY.
CALL METHOD lo_user_dao->get_user_team_list
EXPORTING
i_user_id = lv_uj_user_id
IMPORTING
et_teams = lt_teams
et_leading_teams = lt_leading_teams.
CATCH cx_uje_exception .
ENDTRY.

IF lt_teams[] IS NOT INITIAL.
lt_teams_mbr[] = lt_teams[].
LOOP AT it_string INTO wa_string .
READ TABLE lt_teams_mbr INTO ls_teams_mbr WITH KEY team_mbr = wa_string-str.
IF sy-subrc = 0.
e_validation = abap_true.
E_VALIDATION_TEAM = wa_string-str.
EXIT.
ENDIF.
ENDLOOP.
ENDIF.
ENDIF.
ENDIF.
ENDLOOP.
ENDMETHOD.

I need to loop through it_string and find all the records that match the records contained within lt_teams_mbr and then store those results in a separate structure where I can read the each one of the records apply an extra set of conditions to get a final result.

Any help will be greatly appreciated. Thanks in advance.

former_member186338
Active Contributor

Can you explain your business requirements? What do you want to achieve in general?

Sandra_Rossi
Active Contributor

I don't understand your question either, there is almost everything you need in your code:

          LOOP AT it_string INTO wa_string .
            READ TABLE lt_teams_mbr  INTO ls_teams_mbr WITH KEY team_mbr = wa_string-str.
            IF sy-subrc = 0.
              e_validation = abap_true.
              E_VALIDATION_TEAM = wa_string-str.
              EXIT.
            ENDIF.
          ENDLOOP.

If you want to store result in a structure instead of E_VALIDATION and E_VALIDATION_TEAM, just initialize the structure.

What exactly are you asking?

maleodillet
Participant
0 Kudos

Hi Sandra, thanks for your time.

I do agree with your statement, the problem that i am having is seen right below:

My code is supposed to show me all match records between lt_teams_mbr and it_string and then store those in wa_string

my string WA_STRING, after the loop must contain 2 matched results from the Loop and Read statements

but again the wa_string is only showing the first match

Thanks

Ryan-Crosby
Active Contributor

Hi Maleo,

You are only getting the first match because of the EXIT statement in your sy-subrc = 0 block after the READ statement. That tells the system to exit your LOOP on the it_string table.

Regards,

Ryan Crosby

Sandra_Rossi
Active Contributor

Well, I understand what you say, but I don't understand how you can imagine that your current code can store data from 2 lines into WA_STRING, because currently it's initialized at each loop (and as Ryan mentioned, you leave the loop after the first match). I guess you want E_VALIDATION_TEAM to return "UNODC_PL, UNODC_A2"?

Is it what you are looking for?

          DATA(it_matched_string) = VALUE string_table( ).
          LOOP AT it_string INTO wa_string .
            READ TABLE lt_teams_mbr  INTO ls_teams_mbr WITH KEY team_mbr = wa_string-str.
            IF sy-subrc = 0.
              e_validation = abap_true.
              APPEND wa_string-str TO it_matched_string.
            ENDIF.
          ENDLOOP.
          E_VALIDATION_TEAM = concat_lines_of( table = it_matched_string sep = `, ` ).
maleodillet
Participant
0 Kudos

Hi Ryan,

Yes I did notice that, the problem is that if I removed the EXIT it the shows me the last matching ID but I does not show both of the matching IDs. Do I need to change the data type of my WA_STRING to hold all possible matches from the loop? Or is there additional code that I need to implement based on your experience?

Accepted Solutions (1)

Accepted Solutions (1)

maleodillet
Participant

Hi Sandra,

Your response worked beautifully. Thanks a lot!

Sandra_Rossi
Active Contributor
0 Kudos

Please post the code as text instead of image, so that one can easily answer by testing your code.

          DATA(it_matched_string) = VALUE string_table( ).
          LOOP AT it_string INTO wa_string .
            READ TABLE lt_teams_mbr  INTO ls_teams_mbr WITH KEY team_mbr = wa_string-str.
            IF sy-subrc = 0.
              e_validation = abap_true.
              APPEND wa_string-str TO it_matched_string.
            ENDIF.
          ENDLOOP.
          E_VALIDATION_TEAM = concat_lines_of( table = it_matched_string sep = `, ` ).

Answers (0)