问题
Is there any way to declare char variable (or maybe string) with the length which is calculated dynamically? This coding clarifies what I want:
DATA: len TYPE i,
a TYPE i,
b TYPE i.
len = a + b.
DATA: var(len) TYPE с.
Do not propose solutions with CREATE DATA and/or field symbols: I've tried them but they are not applicable in my case. The intention of such declaration is adding leading zeroes and/or apply other logic to this string. Therefore string is to be declared strictly!
Any proposals?
回答1:
Under the restrictions you impose (without specifying any understandable reasons besides "trust me"), this isn't possible. It's also completely pointless to even try this. Either you know the length at compile time or you don't. If you need to treat the lenght as a variable, then there's no point in trying to generate data types dynamically. Use a STRING and take care of the leading zeroes manually. It's not that hard.
EDIT 1: It's perfectly possible to use SHIFT, OVERLAY and TRANSLATE with STRING variables, at least in reasonably recent releases.
EDIT 2: If you need complex patterns, use regular expressions - report DEMO_REGEX_TOY and the ABAP documentation should provide you with enough information.
回答2:
Dezmond, pretty long time ago but I guess this is the solution to your question:
DATA:
lo_len TYPE REF TO data.
FIELD-SYMBOLS:
<lv_len> TYPE any.
lv_length = 10.
CREATE DATA lo_len TYPE c LENGTH lv_length.
ASSIGN lo_len->* TO <lv_len>.
Cheers, Gunter
回答3:
Dynamic data declaration like that is not possible as far as I'm aware. The way I've done the variable leading zeroes thing is by overwriting my "add_leading_zeroes" method in child classes, moving my string to a properly typed variable, adding zeroes and moving it back into the main class...
you could also specify a method that accepts your string and the name of a datatype... use RTTS to get the length of that type and use it to add zeroes.
来源:https://stackoverflow.com/questions/12052797/character-variable-with-dynamic-length