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

How can I search for a string/variable after an expression

Former Member
0 Likes
2,686

Hey guys,

I'm working on a program which optimize reports as a project from my university. So it scans reports and try to fix them automatically. I will try to explain my problem with an example:

[random code]

read table l_t009b into wa_t009b with key bdatj = l_jah
bumon = l_mon binary search.

[random code]

So my question is, how can I find 'bdatj' and 'bumon' ? I tried something like this:

READ REPORT v_name INTO source_rep.

FIND ALL OCCURRENCES OF REGEX '(WITH KEY=?)' IN TABLE source_rep IGNORING CASE RESULTS results_tab.

But to be honest i don't know how to get the keys without 'knowing the name of them'.

Have someone an idea?

Greetings

ABAP Development

1 ACCEPTED SOLUTION
Read only

DoanManhQuynh
Active Contributor
2,387

I think you could search the read statement and from there derive the column name:

READ REPORT 'your report' INTO sources.
*sources = VALUE #( ( line = 'read table l_t009b into wa_t009b with key bdatj = l_jah' )
*                   ( line = '                                          xxxxx = aaaaa' )
*                   ( line = '                                          bumon = l_mon binary search.' )
*                 ).
FIND ALL OCCURRENCES OF REGEX '(read table).*(with key)(.*\=)' IN TABLE sources
  IN CHARACTER MODE
  IGNORING CASE
  RESULTS code_lines.
" concatenate multiple key
LOOP AT code_lines INTO DATA(code_line).
  count = code_line-line.
  temp_result = VALUE #( src_line = code_line-line
                         tab_name = segment( val = condense( shift_left( val    = sources[ code_line-line ]-line
                                                                         places = code_line-submatches[ 1 ]-offset + code_line-submatches[ 1 ]-length ) )
                                             index = 1
                                             sep   = ` ` )
                         code_line = substring( val = sources[ code_line-line ]-line
                                                off = code_line-submatches[ 3 ]-offset ) ).
  APPEND temp_result TO temp_results ASSIGNING FIELD-SYMBOL(<line>).
  WHILE count <= lines( sources ).
    IF shift_left( val = <line>-code_line places = strlen( <line>-code_line ) - 1 ) = '.'.
      EXIT.
    ELSE.
      count = count + 1.
      <line>-code_line  = |{ <line>-code_line } { condense( sources[ count ]-line ) }|.
    ENDIF.
  ENDWHILE.
ENDLOOP.
" get key column
LOOP AT temp_results INTO temp_result.
  CLEAR: collect_tab.
  DO.
    TRY .
        collect_tab = VALUE #( BASE collect_tab ( segment( val   = condense( temp_result-code_line )
                                                           index = sy-index
                                                           sep   = ` ` ) ) ).
      CATCH cx_sy_strg_par_val.
        EXIT.
    ENDTRY.
  ENDDO.
  LOOP AT collect_tab INTO DATA(word) WHERE table_line = '='.
    final = VALUE #( BASE final ( src_line  = temp_result-src_line
                                  tab_name  = temp_result-tab_name
                                  code_line = collect_tab[ sy-tabix - 1 ] ) ).
  ENDLOOP.
ENDLOOP.

Hey guys,

I'm working on a program which optimize reports as a project from my university. So it scans reports and try to fix them automatically. I will try to explain my problem with an example:

[random code]

read table l_t009b into wa_t009b with key bdatj = l_jah
bumon = l_mon binary search.

[random code]

So my question is, how can I find 'bdatj' and 'bumon' ? I tried something like this:

READ REPORT v_name INTO source_rep.

FIND ALL OCCURRENCES OF REGEX '(WITH KEY=?)' IN TABLE source_rep IGNORING CASE RESULTS results_tab.

But to be honest i don't know how to get the keys without 'knowing the name of them'.

Have someone an idea?

Greetings

ABAP Development

9 REPLIES 9
Read only

former_member1716
Active Contributor
0 Likes
2,387

Your question is still not clear, can you help to understand better?

Read only

Sandra_Rossi
Active Contributor
0 Likes
2,387

If your question is about regular expressions, you don't need to ask in ABAP forum, you have lots of resources for regular expressions all around the web.

Read only

Former Member
0 Likes
2,387

Thanks for the fast answers. Sorry that my question is unclear.

I wanted to make a report which is able to find the used keys in a 'READ TABLE' expression. So I'm trying to scan a customer abap report, search for 'read tables ... with key' and trying to find out which keys this 'read table' uses. So I am able to insert a line where I sort the table by the keys before reading the table.

Should look like this after finding the keys:

233    l_tag =  wa_itab_st_zhf01-datum+6(2).
234    l_jah =  wa_itab_st_zhf01-datum(4).
235    sort l_t009b by bdatj bumon.
236    read table l_t009b into wa_t009b with key bdatj  = l_jah
237                                              bumon  = l_mon binary search.
238
239    if wa_t009b-butag GE wa_itab_st_zhf01-datum+6(2).

But my problem is right now I don't know how I can find the keys bdatj and bumon without looking by myself, because i want that my program should find them and insert the 'sort...' line automatically.

Hope it's better to understand now.

Read only

former_member1716
Active Contributor
2,387

You Cannot find Key fields as per your requirement, there is not way you can perform a Sort Automatically.

The rule is simple, in case you are using the Binary search in Read Statement then it is mandatory that you sort the Internal table with Key fields used in Read Statement.

If you don't use Binary search then you can Read the table with Key not sorted as well, but this is not a effective way coding.

The best practice is sort the internal table with any fields (They need not be Key Fields always) and use the same field in Read Statement to use Binary search.

By the way what is your purpose to know the Key field in internal table?

Read only

Former Member
0 Likes
2,387

Well, I'm trying to make a tool , based on the information from the code inspector, who is able to correct automatically a report to make it hana ready. And I thought the error code 'bin_search' was the easiest one to begin with. Any idea how I can solve it?

Read only

FredericGirod
Active Contributor
0 Likes
2,387

You should have a look of all the class delivered with ATC & also the AOC (atc code provide in the git)

it will not fixe your code, but there is a lot of tool to scan the code & identify the issue

Read only

Sandra_Rossi
Active Contributor
0 Likes
2,387

I still think that you are looking for the right regular expression (provided that each ABAP statement is stored in one line because a regular expression applies to only one line at a time). But doing all this automatically requires spending a lot of time, and unless you are developing a tool for many companies, I don't see what gain to create it automatically compared to manually. Moreover, adding a SORT right before a binary search may be counter-productive if the read occurs inside a loop...

Read only

Former Member
0 Likes
2,387

Alright guys, I solved my problem with some loops/wa/read tables and a lot of IFs. Nevertheless thank you for your help!

Read only

DoanManhQuynh
Active Contributor
2,388

I think you could search the read statement and from there derive the column name:

READ REPORT 'your report' INTO sources.
*sources = VALUE #( ( line = 'read table l_t009b into wa_t009b with key bdatj = l_jah' )
*                   ( line = '                                          xxxxx = aaaaa' )
*                   ( line = '                                          bumon = l_mon binary search.' )
*                 ).
FIND ALL OCCURRENCES OF REGEX '(read table).*(with key)(.*\=)' IN TABLE sources
  IN CHARACTER MODE
  IGNORING CASE
  RESULTS code_lines.
" concatenate multiple key
LOOP AT code_lines INTO DATA(code_line).
  count = code_line-line.
  temp_result = VALUE #( src_line = code_line-line
                         tab_name = segment( val = condense( shift_left( val    = sources[ code_line-line ]-line
                                                                         places = code_line-submatches[ 1 ]-offset + code_line-submatches[ 1 ]-length ) )
                                             index = 1
                                             sep   = ` ` )
                         code_line = substring( val = sources[ code_line-line ]-line
                                                off = code_line-submatches[ 3 ]-offset ) ).
  APPEND temp_result TO temp_results ASSIGNING FIELD-SYMBOL(<line>).
  WHILE count <= lines( sources ).
    IF shift_left( val = <line>-code_line places = strlen( <line>-code_line ) - 1 ) = '.'.
      EXIT.
    ELSE.
      count = count + 1.
      <line>-code_line  = |{ <line>-code_line } { condense( sources[ count ]-line ) }|.
    ENDIF.
  ENDWHILE.
ENDLOOP.
" get key column
LOOP AT temp_results INTO temp_result.
  CLEAR: collect_tab.
  DO.
    TRY .
        collect_tab = VALUE #( BASE collect_tab ( segment( val   = condense( temp_result-code_line )
                                                           index = sy-index
                                                           sep   = ` ` ) ) ).
      CATCH cx_sy_strg_par_val.
        EXIT.
    ENDTRY.
  ENDDO.
  LOOP AT collect_tab INTO DATA(word) WHERE table_line = '='.
    final = VALUE #( BASE final ( src_line  = temp_result-src_line
                                  tab_name  = temp_result-tab_name
                                  code_line = collect_tab[ sy-tabix - 1 ] ) ).
  ENDLOOP.
ENDLOOP.