07-08-2022 7:33 AM
Hello
I would like to know if there is a simple/better way to accomplish following.
I have 2 char variables, lets say variable A and variable B and 1 result variable C.
variable A can contain following: abc; Aabc; abcA; def; Adef; defA
And variable B can contain the same: abc; Aabc; abcA; def; Adef; defA
But both variables get read from 2 different tables that have 1.000+ rows that are in random order.
I need to achieve following:
If variable A = abc and variable B = abc then C = 1.
If variable A = Aabc and variable B = abc then C = 1.
If variable A = abcA and variable B = abc then C = 1.
If variable A = abc and variable B = Aabc then C = 1.
If variable A = abc and variable B = abcA then C = 1.
and so on, only when A = *abc* and B = *def* (or visa versa) then C = 0.
At the moment I have code similar to
If A contains 'abc' then A = 'abc'.
If B contains 'abc' then B = 'abc'.
If A contains 'def' then A = 'def'.
If B contains 'def' then B = 'def'.
and then I can just say If A = B then C = 1.
But my tables contain a lot more then just abc and def, so the IF statement gets a lot bigger.
I was wondering if there was a way in ABAP to say:
If 3 characters of A match 3 characters from B then C = 1.
I tried it with contain and with chaining (regex), but since it can be the beginning, ending or middle that changes I couldn't find a solution for it.
Kind regards
Sven
07-08-2022 9:29 AM
It doesn't exactly reflect what you seem to ask, but possibly the "distance" function, which indicates the minimum number of character moves to go from one text to another one. So if you calculate length - distance, it could give you the number of same characters.
Otherwise, you'll probably need to code the algorithm with loops and so on.
Also, are you talking about 3 contiguous characters, or any order? Is it to be case sensitive or insensitive comparison? (not even talking about "locale"...)
07-08-2022 9:50 AM
sandra.rossi
Thank you for the info, haven't heard of the function I'll check it out.
To be more specific no it is not always 3 characters which makes it even more difficult. It is between 3-5 characters and the leading or ending characters that might change can be 1-2 characters, so more specific it could also be like: ABabcde or abcCD, so it is complex.
I'm not the best coder so I was wondering if there was a better solution, something like SQL where you can do 'variable A like *abc*', but with both being variables it seems a bit difficult to do.
Also yes it is case sensitive, if it makes any difference I could always convert them to lowercase, I don't think it matters to the business if it has all lowercase.
An other solution I was thinking about was making a table with all the words that I have to match, loop over them to do a contain on the variables, but I'm not sure if this would be more performant or not.
Kind regards
Sven
07-11-2022 12:10 PM
CLASS lcl_util DEFINITION.
PUBLIC SECTION.
CLASS-METHODS compare IMPORTING a TYPE string b TYPE string RETURNING VALUE(c) TYPE i.
ENDCLASS.
DATA(a) = |Xabc|.
DATA(b1) = |abcA|.
DATA(b2) = |xyz|.
DATA(c1) = lcl_util=>compare( a = a b = b1 ).
DATA(c2) = lcl_util=>compare( a = a b = b2 ).
cl_demo_output=>display( |C1 { c1 }, C2 { c2 }| ). "C1 1, C2 0
CLASS lcl_util IMPLEMENTATION.
METHOD compare.
DATA(i) = 0.
DO.
IF c = 1 OR ( strlen( a ) < ( i + 3 ) ).
EXIT.
ENDIF.
c = COND #( WHEN contains( val = b sub = a+i(3) ) THEN 1 ELSE 0 ).
i = i + 1.
ENDDO.
ENDMETHOD.
ENDCLASS.