2019 Sep 24 9:58 AM
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
2019 Sep 25 7:25 AM
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.
2019 Sep 24 10:01 AM
Your question is still not clear, can you help to understand better?
2019 Sep 24 10:10 AM
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.
2019 Sep 24 10:51 AM
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.
2019 Sep 24 10:58 AM
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?
2019 Sep 24 11:08 AM
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?
2019 Sep 24 12:30 PM
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
2019 Sep 24 12:32 PM
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...
2019 Sep 24 3:09 PM
Alright guys, I solved my problem with some loops/wa/read tables and a lot of IFs. Nevertheless thank you for your help!
2019 Sep 25 7:25 AM
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.
| User | Count |
|---|---|
| 4 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |