0
  1. I have an internal table with the following data (<fs_content>):
OFFER/005056B467AE1ED9B1962F12360477E9-A
OFFER/005056B467AE1ED9B1962F12360477E9-B
OFFER/005056B467AE1ED9B1962F12360477E9-C
OFFER/005056B467AE1ED9B1962F12360477E9-D
OFFER/005056B467AE1ED9B1962F12360477E9-E
  1. I have to search repeatedly values like this (V1):
OFFER-A
OFFER-B
OFFER-C
OFFER-M
OFFER-L
  1. I expect that the following values are identified, which match one line in the internal table (itab_v1_result):
OFFER-A
OFFER-B
OFFER-C

But as you can see in <fs_content> there's the same code 005056B467AE1ED9B1962F12360477E9, after OFFER/ until - symbol.

Now, I want to assign the rows from <fs_content> to field Symbol <fs_my_content> by comparing V1 value with each row in <fs_content>, but the problem is that V1 value is not completely same like <fs_content> rows.

I've tried to do something like this, but it's not working, <fs_my_content> is always empty:

READ TABLE <fs_content> ASSIGNING <fs_my_content> WITH KEY ('ATTR_NAME') = V1.

How can I get itab_v1_result to contain what I expect?

My minimal reproducible example:

TYPES:
  BEGIN OF ty_content,
    attr_name TYPE string,
  END OF ty_content.
FIELD-SYMBOLS:
  <fs_my_content> TYPE any,
  <fs_content>    TYPE ANY TABLE.
DATA:
  itab_content   TYPE STANDARD TABLE OF ty_content,
  itab_v1        TYPE STANDARD TABLE OF string,
  itab_v1_result TYPE STANDARD TABLE OF string,
  v1             TYPE string.

itab_content = VALUE #(
( attr_name = 'OFFER/005056B467AE1ED9B1962F12360477E9-A' )
( attr_name = 'OFFER/005056B467AE1ED9B1962F12360477E9-B' )
( attr_name = 'OFFER/005056B467AE1ED9B1962F123604D7E9-C' )
( attr_name = 'OFFER/005056B467AE1ED9B1962F12360477E9-D' )
( attr_name = 'OFFER/005056B467AE1ED9B1962F12360477E9-E' ) ).

itab_v1 = VALUE #(
( `OFFER-A` )
( `OFFER-B` )
( `OFFER-C` )
( `OFFER-M` )
( `OFFER-L` ) ).

ASSIGN itab_content TO <fs_content>.
LOOP AT itab_v1 INTO v1.
  READ TABLE <fs_content> ASSIGNING <fs_my_content> WITH KEY ('ATTR_NAME') = v1.
  IF sy-subrc = 0.
    APPEND v1 TO itab_v1_result.
  ENDIF.
ENDLOOP.

" Here, itab_v1_result is empty unfortunately!?
Suncatcher
  • 10,355
  • 10
  • 52
  • 90
user11823122
  • 99
  • 4
  • 16
  • Can you give an example what you expect where should point the field symbol if V1 = 'OFFER-C', is it the whole line, the value in the middle, or what? Could you also provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) please? Thank you (note that asking for a "useful link" is [off-topic](https://stackoverflow.com/help/on-topic) for Stack Overflow, simply because the answers should be complete - some links may be given only to support the answer). – Sandra Rossi Sep 06 '19 at 14:50
  • Sandra, thanks a lot for your answer. I'll try to give an example and edit the question, may be it would more understandable. – user11823122 Sep 09 '19 at 06:36
  • I have added the minimal reproducible example, so that people can quickly try and answer to you. – Sandra Rossi Sep 09 '19 at 07:39
  • Thanks a lot, Sandra. Your example is more clearer than mine. – user11823122 Sep 09 '19 at 07:43
  • 1
    Note that your question seems to be: "how to search a substring in an internal table". But your example uses a dynamic internal table, so I feel the question is not "minimal": using a statically-defined internal table would make the question more clear. – Sandra Rossi Sep 09 '19 at 07:52

3 Answers3

2

You cannot use any operators other than = in READ TABLE. But you can use them in a LOOP.

First you'd have to arrange your V1 in a way that CS can identify, so just use the '-X', which seems to be unique. Then you can use your condition in the LOOP clause.

offset = STRLEN( v1 ) - 2.
v2 = v1+offset(2).

LOOP AT itab1 ASSIGNING <fs_itab1> WHERE attribute_name CS v2.
  " do something
  " if you only want to do it for the first entry you find, then just EXIT afterwards
ENDLOOP.
Legxis
  • 886
  • 7
  • 19
  • 1
    Although I guess the main question is about how to "search a substring in an internal table", there is also a little complexity added to the question because of the dynamic internal table. I have added a minimal reproducible example to support the question so that it's easier to use real code. – Sandra Rossi Sep 09 '19 at 07:48
1

You are over-complicating the solution. Why not just use substring access?

LOOP AT itab_v1 INTO v1.
  LOOP AT itab_content ASSIGNING FIELD-SYMBOL(<content>).
    CHECK v1(5) = <content>-attr_name(5) AND substring( val = v1 off = strlen( v1 ) - 1 len = 1 ) = substring( val = <content>-attr_name off = strlen( <content>-attr_name ) - 1 len = 1 ).
    APPEND v1 TO itab_v1_result.
  ENDLOOP.
ENDLOOP.
Suncatcher
  • 10,355
  • 10
  • 52
  • 90
0

Thanks a lot to all of you for your variants of solution. It was very helpful for me.

Here's the solution of my problem.

  1. First of all we should loop at <fs_content> and assign it to new field-symbol <dynamic_content>.
  2. Then, we should get ATTR_NAME field from <dynamic_content> and assign it to another field-symbol <contact_attribute_name>.
  3. We'll use some function for working with STRING type value, because of this we'll assign <contact_attribute_name> to lv_attr_name.
  4. As we know (from task description) in lv_attr_name would be the values like: OFFER/005056B467AE1ED9B1962F12360477E9-A and so on. Because of this we'll find the position of first / by find() method from the beginning of lv_attr_name and put the value into lv_slash_position.

We repeat this operation for finding the position of first - after lv_slash_position and put the value into lv_dash_position.

After this two operation we'll use the replace() method and replace lv_dash_position - lv_slash_position to empty value. In the end we'll get OFFER/-A and put it into lv_attr_val_string.

In the end we'll compare lv_attr_val_string and v1, if lv_attr_val_string <> v1 we would not put it to the final itab itab_v1_result, else we'll do it.

LOOP AT <fs_content> ASSIGNING <dynamic_content>.
    ASSIGN COMPONENT 'ATTR_NAME' OF STRUCTURE <dynamic_content> TO <contact_attribute_name>.

    DATA(lv_attr_name) = CONV string( <contact_attribute_name> ).

    DATA(lv_slash_position) = find( val = lv_attr_val_string
                                    sub = '/'
                                    off = 0 ).

    IF lv_slash_position <> '1-'.
        DATA(lv_dash_position) = find( val = lv_attr_val_string
                                       sub = '-'
                                       off = lv_slash_position ).

        lv_attr_val_string = replace( val = lv_attr_val_string
                                      off = lv_slash_position
                                      len = ( lv_dash_position - lv_slash_position )
                                      with = '' ).
    ENDIF.

    IF lv_attr_val_string <> v1.
        APPEND v1 TO itab_v1_result.
        CONTINUE.
    ENDIF.
ENDLOOP.
Suncatcher
  • 10,355
  • 10
  • 52
  • 90
user11823122
  • 99
  • 4
  • 16