*&---------------------------------------------------------------------*
*& Report TASK #02
*&---------------------------------------------------------------------*
*& Description: 'Let's have some ABAP fun' contest - Task #2
* Merge two char variables into one and write it to the
* list, so that spaces are preserved.
* Write the hash sign after this merged variable,
* so that it's apparent where the variable's content ends.
*
*& Author : Miroslav Oprsteny
*& Date : 2011-10-20
*&---------------------------------------------------------------------*
report task_#02.
data:
c1 type c length 5 value 'A ',
c2 type c length 5 value 'B ',
lr_data type ref to data, " reference to variable (dynamic memory allocation during runtime)
l_len1 type i, " length of variable c1
l_len2 type i, " length of variable c2
l_total_len type i. " total length of output variable
field-symbols:
<fs_out>. " field symbol used to manipulate with data
describe field c1 output-length l_len1. " get length of given variables
describe field c2 output-length l_len2.
l_total_len = l_len1 + l_len2 + 1. " get total length of output variable
create data lr_data type c length l_total_len. " dynamic creation of a variable during runtime
assign lr_data->* to <fs_out>. " assign allocated memory area to field symbol
<fs_out> = c1. " fill data using offsets
<fs_out>+l_len1 = c2. " fill data using offsets
l_total_len = l_total_len - 1. " get last position (= total_length - 1)
<fs_out>+l_total_len = '#'. " fill data using offsets
write <fs_out>.
* SOLUTION 2 - not accepted due to usage of structured variable
DATA:
c1 TYPE char5 VALUE 'A ',
c2 LIKE c1 VALUE 'B ',
begin of l_out,
c1 like c1,
c2 like c2,
c3(1) value '#',
end of l_out.
l_out-c1 = c1.
l_out-c2 = c2.
write l_out.
* SOLUTION 3 - not accepted as it is not good practise to hardcode solution for just one usage
DATA:
c1 TYPE char5 VALUE 'A ',
c2 LIKE c1 VALUE 'B ',
c3 TYPE char20,
s3 TYPE string,
s4 LIKE s3,
s5 LIKE s3.
WRITE:/ 'Target is STRING, Spaces are preserved:'.
CONCATENATE space space INTO s3 SEPARATED BY c1.
CONCATENATE space space INTO s4 SEPARATED BY c2(2).
CONCATENATE s3 s4 INTO s5.
WRITE:/ s5 NO-GAP, 'End'. SKIP 2.
* SOLUTION 4 - accepted but not very universal
Data:
c1 TYPE char5 VALUE 'A ',
c2 LIKE c1 VALUE 'B ',
l_out TYPE string.
TRANSLATE c1 using ' #'.
TRANSLATE c2 using ' #'.
CONCATENATE c1 c2 into l_out.
TRANSLATE l_out using '# '.
CONCATENATE l_out '#' into l_out.
write l_out.
* SOLUTION 5 - Not working on all systems (including this one) but most elegant
DATA:
c1 TYPE c LENGTH 5 VALUE 'A ',
c2 TYPE c LENGTH 5 VALUE 'B ',
s TYPE string.
CONCATENATE c1 c2 '#' INTO s RESPECTING BLANKS.
WRITE:/ s.
* SOLUTION 6 (using back apostrophes) - Not accepted as it is using literals and not given variables
DATA:
result TYPE c LENGTH 11.
CONCATENATE `A ` `B ` '#' INTO result.
WRITE result.
Share it with your friends:
Related