cancel
Showing results for 
Search instead for 
Did you mean: 

Reading last 3 characters of the contents inside a variable in ABAP

maleodillet
Participant
0 Kudos

Hi Experts,

I have an enhancement request where I need to read the last '3' characters of all the individual IDs contained inside a variable LV_VALIDATION_TEAM.

Additionally, I will have to take those "Last 3 characters" of each ID and run them through a established set of IF statement conditions.

Currently my code does not recognize that there are multiple IDs contained within the variable and only reads the last 3 character of the last record. I believe I need to loop my current code along with my IF conditions but I am clueless onto how to do it. Any help is greatly appreciated. Thanks

abo
Active Contributor

First time I see sandra.rossi in the user-defined tags 😄

Sandra_Rossi
Active Contributor
c5e08e0478aa4727abc4482f5be390b29 hours ago

Not first time, unfortunately. I didn't subscribe to this tag. I would like to submit the idea to subscribe to not receive questions if some tags are indicated 😄

FredericGirod
Active Contributor
0 Kudos

Excelent ! 😄

Accepted Solutions (0)

Answers (4)

Answers (4)

maleodillet
Participant
0 Kudos

Hi all,

Thank you very much for your time and shared knowledge.

As per your your suggestions, I ended up writing all of the "last 3 characters" code and "evaluations" right inside the method GET_OWNER_PROPERTY_VALIDATION.

*******************************************************************************
TYPES: BEGIN OF lty_str,
last3 TYPE string,
END OF lty_str.

DATA: lv_string TYPE string,
lv_length TYPE string,
lt_table TYPE TABLE OF lty_str,
ls_table LIKE LINE OF lt_table.

lv_string = lv_last3.
SPLIT lv_string AT ',' INTO: DATA(str1) DATA(str2),

TABLE DATA(itab).
LOOP AT itab INTO DATA(wa).
lv_length = strlen( wa ).
DATA(last_3) = lv_length - 3.
ls_table-last3 = wa+last_3(3).
APPEND ls_table TO lt_table.

ENDLOOP.
******************************************************************************************
DATA: wa2 TYPE lty_str.

IF ls_stage EQ '1'.
LOOP AT lt_table INTO wa2 WHERE last3 = '_PL' OR last3 ='_AD'.
IF sy-subrc = 0.
e_validation_team = abap_true.
ENDIF.
ENDLOOP.
ELSEIF ls_stage EQ '2'.
LOOP AT lt_table INTO wa2 WHERE last3 ='_A1' OR last3 ='_AD'.
IF sy-subrc = 0.
e_validation_team = abap_true.
ENDIF.
ENDLOOP.
ELSEIF ls_stage EQ '3'.
LOOP AT lt_table INTO wa2 WHERE last3 ='_A2' OR last3 ='_AD'.
IF sy-subrc = 0.
e_validation_team = abap_true.
ENDIF.
ENDLOOP.
ELSEIF ls_stage EQ '4'.
LOOP AT lt_table INTO wa2 WHERE last3 ='_A3' OR last3 ='_AD'.
IF sy-subrc = 0.
e_validation_team = abap_true.
ENDIF.
ENDLOOP.
ELSE.
e_validation_team = abap_false.
ENDIF.

I know i may not be optimal but so far is working.

The only issue that I have pending is on how to capture the final result of those last IF statements in a variable.

I'd like to know what "last 3 characters" fulfilled the IF conditions when the e_validation_team = abap_true.

EX- Last3 = '_PL', Last3 ='_AD',Last3 = '_A3'.....

Sandra_Rossi
Active Contributor

Not optimal and wrong. Never test IF sy-subrc = 0 right after LOOP AT, it's completely useless. And do an EXIT.

            ...
            LOOP AT lt_table INTO wa2 WHERE last3 = '_PL' OR last3 ='_AD'.
              e_validation_team = abap_true.
              EXIT.
            ENDLOOP.
            ...
Also, as you mention the solution which works for you, you could avoid splitting after concat_lines_of (solution from LOOP AT a table with with another table to get all matched ID's listed | SAP Community), just don't use concat_lines_of.You'll end with that:
  DATA(validation_teams) = 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 validation_teams.<br>    ENDIF.
  ENDLOOP.

  " example next line: if lv_stage = '2', check if validation_teams has line ending with _A1 or _AD:
  DATA(lookup_team_name) = SWITCH string( lv_stage
     WHEN '1' THEN '(?:_PL|_AD)$'
     WHEN '2' THEN '(?:_A1|_AD)$'
     WHEN '3' THEN '(?:_A2|_AD)$'
     WHEN '4' THEN '(?:_A3|_AD)$' ).

  FIND REGEX lookup_team_name IN TABLE validation_teams.
  IF sy-subrc = 0.
    e_validation_team = abap_true.
  ENDIF.
maleodillet
Participant
0 Kudos

Hi Sandra,

Thank you so much for the suggestion on the unnecessary "IF SY_SUBRC=0" statement. I did remove it and works great.

Also, I instead did implemented your latest code and it works flawlessly, so this is the code I have as for now:

          DATA(validation_teams) = 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 validation_teams.
ENDIF.
ENDLOOP.

DATA(lookup_team_name) = SWITCH string( ls_stage
WHEN '1' THEN '(?:_PL|_AD)$'
WHEN '2' THEN '(?:_A1|_AD)$'
WHEN '3' THEN '(?:_A2|_AD)$'
WHEN '4' THEN '(?:_A3|_AD)$' ).

FIND REGEX lookup_team_name IN TABLE validation_teams.
IF sy-subrc = 0.
e_validation_team = abap_true.
ENDIF.

back to my later question onto how to capture the "last 3" characters that were matched, is there a way to capture in a variable or string the actual last three characters that were found during the "FIND REGEX lookup_team_name IN TABLE validation_teams."?

For example:

for this particular scenario from above, I know for a fact that "_PL" was the matching last three characters so is there a way to capture that in a variable to be read and not just get the "E_VALIDATION_TEAM = ABAP_TRUE"?

Thanks you very much in advance!

Sandra_Rossi
Active Contributor
0 Kudos

You can get "submatches" of the Regular Expression, by using registration (...) ; (?:...) was "without registration".

          DATA(lookup_team_name) = SWITCH string( ls_stage
             WHEN '1' THEN '(_PL|_AD)$'
             WHEN '2' THEN '(_A1|_AD)$'
             WHEN '3' THEN '(_A2|_AD)$'
             WHEN '4' THEN '(_A3|_AD)$' ).

          FIND REGEX lookup_team_name IN TABLE validation_teams SUBMATCHES DATA(submatch).

With IN TABLE, it will return the submatch of the first matching line.

Sandra_Rossi
Active Contributor
0 Kudos

Better change the method GET_OWNER_PROPERTY_VALIDATION2 to return a table of validation teams.

METHODS get_owner_property_validation2
  IMPORTING
    i_appset_id TYPE ...
    ...
  EXPORTING
    e_validation TYPE ...
    e_validation_teams TYPE string_table. " <========

then it'll be easier to implement the logic you want.

In your code, I'm not sure whether you really want to use ELSEIF, shouldn't it be OR?

If it's OR, you could use this code:

IF lv_validation = abap_true.
  DATA(expected_end_of_team_name) = SWITCH string( lv_stage
     WHEN '1' THEN '_PL'
     WHEN '2' THEN '_A1'
     WHEN '3' THEN '_A2'
     WHEN '4' THEN '_A3'
     ELSE          '_AD' ).
  FIND REGEX |{ expected_end_of_team_name }$| IN TABLE e_validation_teams.
  IF sy-subrc = 0.
    " one validation team is found whose name ends as expected
maleodillet
Participant
0 Kudos

Hi Sandra,

Thanks a lot for your answer and apologies for the tag. I just thought that since you helped in my last post, which is directly related to this one, I may have been easier to get a second opinion from you. I won't happen again!

As per your answer. Thanks again! I took some of your suggestions and reworked it into something else, a little longer but works for now.

Sandra_Rossi
Active Contributor
0 Kudos

Don't confuse a tag (= a topic of interest, not someone) and mentioning someone this way maleodillet

venkateswaran_k
Active Contributor
0 Kudos

Hi

LV_VALIDATION_TEAM.

Assuming your variable LV_VALIDATION_TEAM contains IDs separated by Space. "like 'id_123 id_A1 id_A3 id_PL id_AD'.

If it is not change your class method get_owner_property_validation2 to have the ids separated by a space or unique char say #.

Step 1.

Change your method class method get_owner_property_validation2 to return a able of IDs instead of single variable concatenated Ids with space or #

Then Loop at the table returned.

If not possible to change the class, then use SPLIT statement on that LV_VALIDATION_TEAM and put the cut items into a table and loop at that table.

former_member808116
Participant
0 Kudos

I hope this helps you

TYPES: BEGIN OF LTY_STR,
COL1 TYPE STRING,
COL2 TYPE STRING,
END OF LTY_STR.

DATA: LV_STRING TYPE STRING,
LV_LENGTH TYPE STRING,
LT_TABLE TYPE TABLE OF LTY_STR,
LS_TABLE LIKE LINE OF LT_TABLE.

LV_STRING = 'Bui_01 Cong_02 Quynh_03 From_04 Viet_05 Nam_06'.


SPLIT LV_STRING AT SPACE INTO: DATA(STR1) DATA(STR2),
TABLE DATA(ITAB).
LOOP AT ITAB INTO DATA(WA).
LV_LENGTH = STRLEN( WA ).
DATA(LAST_3) = LV_LENGTH - 3.

LS_TABLE-COL1 = WA.
LS_TABLE-COL2 = WA+LAST_3(3).
APPEND LS_TABLE TO LT_TABLE.
CLEAR: LS_TABLE, WA.

ENDLOOP.
BREAK QUYNHBC.

former_member808116
Participant
0 Kudos

You can covert Lv_validation to Internal table and Loop Iternal table.

maleodillet
Participant
0 Kudos
Hi Quynh Bui Cong,

Thanks a lot for your response. I used 90% of your code to fulfill my requirement. You Rock!

I am just missing one last piece related to my IF statement conditions and how to store the result in a variable or string that I can read.

I added my code right below if you'd so kind to take a look at it I will very much appreciate it.