2016 May 06 7:48 AM
Dear All Experts
I have stuck up on one issue in my development.
we have something called designing pattern in our textile industry
it is a alphanumeric string in following format.
eg.
{ [1A,2B,2C]3,1B,4C}4 ,[1A,4B]4 n so on
here a b c d denotes yarn no.
number preceding each alphabet denotes how many times this yarn will be used in pattern
number after ']' brackaet denotes how many times entire string will be repeated
and no after '}' bracket denotes how many two or more pattern with this brackets will be repeated
now i want to calculate how many times certain yarn is there in the string and addition of all the numerical values in string.
in example above YARN A will be there 1*]3*}4 +1*]4 i.e 16 times B will be 2*]3}4 + 1*]1}*4 + 4]*4 i.e 44 times n C will be 2*]3*}4 + 4*]1}*4 i.e 40 times
and total is 16 + 44+ 40 i.e 100.
is there any logic, function module or method available in abap to decode and calculate such string.
Regards
Gaurav Joshi.
2016 May 06 10:00 AM
Hi Gaurav,
I wrote code based on your scenario to get value for Yarn 'A'. Same way you can get value for 'B' & 'C'.
Check below code:
DATA: L_STRING TYPE STRING,
L_VALUE TYPE F,
L_RESULT TYPE I.
L_STRING = '{[1A,2B,2C]3,1B,4C}4,[1A,4B]4'.
REPLACE ALL OCCURRENCES OF ',' IN L_STRING WITH ' + % '.
REPLACE ALL OCCURRENCES OF '{' IN L_STRING WITH ' ( %'.
REPLACE ALL OCCURRENCES OF '[' IN L_STRING WITH ' ( %'.
REPLACE ALL OCCURRENCES OF '}' IN L_STRING WITH ' ) * % '.
REPLACE ALL OCCURRENCES OF ']' IN L_STRING WITH ' ) * %'.
*..Pass '1' for A and for rest characters '0'
REPLACE ALL OCCURRENCES OF 'A' IN L_STRING WITH ' * 1 %'.
REPLACE ALL OCCURRENCES OF 'B' IN L_STRING WITH ' * 0 %'.
REPLACE ALL OCCURRENCES OF 'C' IN L_STRING WITH ' * 0 %'.
REPLACE ALL OCCURRENCES OF '%' IN L_STRING WITH SPACE. "space after operators
CALL FUNCTION 'EVAL_FORMULA'
EXPORTING
FORMULA = L_STRING
PROGRAM = SY-REPID
ROUTINE = 'VAR_GET'
IMPORTING
VALUE = L_VALUE
EXCEPTIONS
DIVISION_BY_ZERO = 1
EXP_ERROR = 2
FORMULA_TABLE_NOT_VALID = 3
INVALID_EXPRESSION = 4
INVALID_VALUE = 5
LOG_ERROR = 6
PARAMETER_ERROR = 7
SQRT_ERROR = 8
UNITS_NOT_VALID = 9
MISSING_PARAMETER = 10
OTHERS = 11.
IF SY-SUBRC = 0.
MOVE L_VALUE TO L_RESULT.
WRITE:/ L_RESULT.
ENDIF.
2016 May 06 8:37 AM
Hi Gaurav,
As I am aware there is no such standard function module or class available. You have to achieve it by custom coding. You can make use of statements REPLACE ALL OCCURRENCES, DESCRIBE FIELD, SPLIT.
2016 May 06 8:50 AM
The simplest solution in my opinion is to go with a sequence of replace operations with regex.
I hope you are familiar with regex.
For example:
replace all occurrences of regex '\b[1-9]B' with ''.
deletes all entries of B out of the string, the same you have to do for C.
Then the following commands for the Multiplikations
Replace all occurrences of regex '[' with '('.
Replace all occurrences of regex ']' with ')*'.
Finally if you have transformed your Formular (command, multioccurrences,...) step by step to a matematical correct formula for one garn you can evaluate this resultwith the Sap standard formula module eval_formula which takes care of the brackets.
Of course a full formula parser would be robuster but a lot of more effort. Hope this helps, Andreas
2016 May 06 9:34 AM
Thanks folks.. most of the things are tried out actual strings are far more complicated than mention n example.. but still will try out.. if anything pls keep updating
Regards
Gaurav
2016 May 06 10:00 AM
Hi Gaurav,
I wrote code based on your scenario to get value for Yarn 'A'. Same way you can get value for 'B' & 'C'.
Check below code:
DATA: L_STRING TYPE STRING,
L_VALUE TYPE F,
L_RESULT TYPE I.
L_STRING = '{[1A,2B,2C]3,1B,4C}4,[1A,4B]4'.
REPLACE ALL OCCURRENCES OF ',' IN L_STRING WITH ' + % '.
REPLACE ALL OCCURRENCES OF '{' IN L_STRING WITH ' ( %'.
REPLACE ALL OCCURRENCES OF '[' IN L_STRING WITH ' ( %'.
REPLACE ALL OCCURRENCES OF '}' IN L_STRING WITH ' ) * % '.
REPLACE ALL OCCURRENCES OF ']' IN L_STRING WITH ' ) * %'.
*..Pass '1' for A and for rest characters '0'
REPLACE ALL OCCURRENCES OF 'A' IN L_STRING WITH ' * 1 %'.
REPLACE ALL OCCURRENCES OF 'B' IN L_STRING WITH ' * 0 %'.
REPLACE ALL OCCURRENCES OF 'C' IN L_STRING WITH ' * 0 %'.
REPLACE ALL OCCURRENCES OF '%' IN L_STRING WITH SPACE. "space after operators
CALL FUNCTION 'EVAL_FORMULA'
EXPORTING
FORMULA = L_STRING
PROGRAM = SY-REPID
ROUTINE = 'VAR_GET'
IMPORTING
VALUE = L_VALUE
EXCEPTIONS
DIVISION_BY_ZERO = 1
EXP_ERROR = 2
FORMULA_TABLE_NOT_VALID = 3
INVALID_EXPRESSION = 4
INVALID_VALUE = 5
LOG_ERROR = 6
PARAMETER_ERROR = 7
SQRT_ERROR = 8
UNITS_NOT_VALID = 9
MISSING_PARAMETER = 10
OTHERS = 11.
IF SY-SUBRC = 0.
MOVE L_VALUE TO L_RESULT.
WRITE:/ L_RESULT.
ENDIF.
2016 May 06 10:14 AM
2016 May 06 10:24 AM
Awesome work Pranay.
Gaurav,
Adding to this if you want to make A/B/C dynamic,
DO this simple code and store the values in string tab and call Gaurav's code in loop.
lw_input = { [1A,2B,2C]3,1B,4C}4 ,[1A,4B]4.
lw_length = strlen ( lw_input ).
WHILE (lw_index >= lw_length).
lw_index = sy-index.
assign component lw_index of lw_input into <fs_value>.
if <FS_value> CA sy-abcde.
lw_string = <fs_value>.
append lw_string to lt_string.
ENDWHILE.
After this, call the above code in this loop.
2016 May 06 1:35 PM
Hi Gaurav,
If your problem is solved then it's ok.
if not then please find the below code snippet.
Following Pranay's code I am sending you the code. find it as below.
TYPES: BEGIN OF TY_TAB,
YARN(4),
END OF TY_TAB,
BEGIN OF TY_FINAL,
YARN(4),
TOTAL TYPE I,
END OF TY_FINAL.
DATA: IT_TAB TYPE TABLE OF TY_TAB,
WA_TAB TYPE TY_TAB,
IT_FINAL TYPE TABLE OF TY_FINAL,
WA_FINAL TYPE TY_FINAL.
DATA: LEN TYPE I,
PT TYPE I VALUE 0,
STRING TYPE STRING,
STRING1 TYPE STRING,
TOTAL TYPE I,
L_VALUE TYPE F.
PARAMETERS: P_YARN TYPE STRING.
START-OF-SELECTION.
STRING = P_YARN.
LEN = STRLEN( STRING ).
DO LEN TIMES.
WA_TAB-YARN = STRING+PT(1).
APPEND WA_TAB TO IT_TAB.
CLEAR WA_TAB.
PT = PT + 1.
ENDDO.
STRING1 = STRING.
DELETE IT_TAB WHERE YARN EQ ' ' OR YARN = ','.
SORT IT_TAB BY YARN.
DELETE ADJACENT DUPLICATES FROM IT_TAB COMPARING ALL FIELDS.
LOOP AT IT_TAB INTO WA_TAB WHERE YARN CA SY-ABCDE.
STRING = STRING1.
IF WA_TAB-YARN CA SY-ABCDE.
WA_FINAL-YARN = WA_TAB-YARN.
REPLACE ALL OCCURRENCES OF ',' IN STRING WITH ' + % '.
REPLACE ALL OCCURRENCES OF '{' IN STRING WITH ' ( %'.
REPLACE ALL OCCURRENCES OF '[' IN STRING WITH ' ( %'.
REPLACE ALL OCCURRENCES OF '}' IN STRING WITH ' ) * % '.
REPLACE ALL OCCURRENCES OF ']' IN STRING WITH ' ) * %'.
REPLACE ALL OCCURRENCES OF WA_TAB-YARN IN STRING WITH ' * 1 %'.
REPLACE ALL OCCURRENCES OF REGEX '([[:alpha:]])' IN STRING WITH ' * 0 %'.
REPLACE ALL OCCURRENCES OF '%' IN STRING WITH SPACE.
CALL FUNCTION 'EVAL_FORMULA'
EXPORTING
FORMULA = STRING
PROGRAM = SY-REPID
ROUTINE = 'VAR_GET'
IMPORTING
VALUE = L_VALUE
EXCEPTIONS
DIVISION_BY_ZERO = 1
EXP_ERROR = 2
FORMULA_TABLE_NOT_VALID = 3
INVALID_EXPRESSION = 4
INVALID_VALUE = 5
LOG_ERROR = 6
PARAMETER_ERROR = 7
SQRT_ERROR = 8
UNITS_NOT_VALID = 9
MISSING_PARAMETER = 10
OTHERS = 11.
IF SY-SUBRC = 0.
MOVE L_VALUE TO TOTAL.
WA_FINAL-TOTAL = TOTAL.
APPEND WA_FINAL TO IT_FINAL.
CLEAR: WA_FINAL, TOTAL, L_VALUE, WA_TAB.
ENDIF.
ENDIF.
ENDLOOP.
LOOP AT IT_FINAL INTO WA_FINAL.
WRITE:/ WA_FINAL-YARN, WA_FINAL-TOTAL.
CLEAR WA_FINAL.
ENDLOOP.