Gratis demo

Dynamic variable replacement in a standard text

Aart Troost, 11 mei 2016

In today’s blog I would like to share a code example about how to dynamically replace text variables in a Standard (SO10) text. Every time I have to use text while I’m programming I want to make this text maintainable so I do not have to hardcode this text. When this text is not hardcoded it’s also easier to make changes to the text by someone else then the developer who created the logic.

For this blog I created the following text containing the variables LV_COMPANY, LV_DATE, LV_CITY and LV_MEDIUM. By the end of the blog we would have an elegant way to replace variables in a SO10 text.

The part I focused on while programming is on how to dynamically replace the variables I used in the standard text. Instead of setting every variable individually with FM ‘SET_TEXTSYMBOL’ I would just like to find all the variables used in the standard text and try to replace them with variables available in the used program.

To read the standard text into local table lt_lines I use FM ‘READ_TEXT’, as you can see the text contains the variables.

I’ve tried to find a FM to just give me all the variables used in these four lines but could not find one so I decided to write some logic the find them. First I will loop over the four lines and look for all variables in each line. E.g. for line 2 the code ‘find all occurrences of  ‘&’ in ls_line-tdline results lt_result.’ will find four occurences of the symbol ‘&’.

We can use this result to find the two variables used in line 2. The check ‘check sy-tabix mod 2 = 1.’ is used to only start at the beginning ‘&’ of each variable.

To find the variable LV_DATE in line 2, you can use FM ‘GET_TEXTSYMBOL’ and feed offset 35 to the start_offset variable. By now the value of the variable ‘lv_textsymbol’ will be ‘LV_DATE’. With this value we can try to find the corresponding value of the variable used in the program.

So the above code will result in the next values for LV_DATE.

After setting all the variables we will call FM ‘TEXT_SYMBOL_REPLACE’ to transfer the text symbols to the standard text we started with.

So now you’ll have a way to dynamically replace the variables in a standard text in your code. For those who want to use (and or improve) this logic here’s the complete code:

data ls_header        type thead.
data lt_lines         type standard table of tline.
data ls_line          type tline.
data lv_textsymbol    type string.
data lt_result        type match_result_tab.
data ls_result        type match_result.

field-symbols <fs_textsymbol> type any.

* SO10 variables
data lv_company       type string value ‘Acorel’.
data lv_date          type string value ‘05.10.2016’.
data lv_city          type string value ‘Amersfoort’.
data lv_medium        type string value ‘blog’.

call function ‘READ_TEXT’
    id                      ‘ST’
language                ‘N’
    header                  ls_header
lines                   lt_lines
id                      1
language                2
others                  8.
if sysubrc <> 0.
* Implement suitable error handling here

call function ‘INIT_TEXTSYMBOL’.

loop at lt_lines into ls_line.
clear lv_textsymbol.
clear lt_result.

  find all occurrences of ‘&’ in ls_linetdline results lt_result.

  loop at lt_result into ls_result.

    check sytabix mod 1.

    call function ‘GET_TEXTSYMBOL’
line         ls_linetdline

    if lv_textsymbol is not initial.
unassign <fs_textsymbol>.
assign (lv_textsymbolto <fs_textsymbol>.
:= |&{ lv_textsymbol }&|.
if <fs_textsymbol> is assigned.
call function ‘SET_TEXTSYMBOL’
header       ls_header
value        <fs_textsymbol>
strlen<fs_textsymbol> )
            replace      .


call function ‘TEXT_SYMBOL_REPLACE’
header ls_header
lines  lt_lines.

Receive our weekly blog by email?
Subscribe here: