Quantcast
Channel: Tweaks
Viewing all articles
Browse latest Browse all 66

ABAP String Operation – Identify & Manipulate Negative Amount in Long String with Separators

$
0
0

In this high tech world of RESTful ABAP Programming, CDS, BOPF, BRF+, Fiori Elements etc, you might be smiling on reading this ancient String Operation topic at SAPYard. We completely understand your though process and even agree with it. But whatever technology SAP might come up with, clients always have some requirement, where we need fall back to basics. So, we are not ashamed to publish this article which will show the tip for those poor ABAP developers who have not yet seen the much hyped modern SAP System. More over, this is a real project scenario and the solution might be useful for someone in need in some corner of this SAP world.

Introduction:

As an ABAPer we all have worked with String in our Program and everyone is well aware of string operations in ABAP. Sometime we come across situation that we need to play with string in ABAP program.

Requirement:

To identify negative amount values in a long string and move negative sign from right to left of value. The negative sign can come in any place. It is not fixed.

For example, in below image negative amount values are at any place in long string.

Logic:

  • Identify the separators.

Here separators are ‘#’. Sometimes it may be ‘;’ or ‘,’.

  • Identify total number of separators in a string and their position.

This can be done by below statement

RESULT_TAB will hold position of ‘#’ in the string.

For example :

We need OFFSET column from this table.

  • We have to define two position pointers to hold high and low position value.

High value will point to 2nd row and Low value will point to 1st row OFFSET value in the table.

For example :

 in this TEXT position and values are as below

PositionValue
0#
14
27
30
4.
55
60
7
8#

I need Low postion value to hold ‘1’ and High position value to hold ‘8’

  • Calculate the difference between High and Low value position.

 That means the number is 7 digit long.

  • Read this 7 digit long value in one variable called VALUE.
  • Once we get the value, we can play with it like moving negative sign in front of the value.

First we have to check whether it has negative sign?

For this, we checked rightmost value is negative or not? If we found that the VALUE has negative sign at right, then we moved it to Left(as per user’s requirement)

  • The final output is

Also Read: Utility Program to Auto Format the Texts into Meaningful Sentence

Complete Program:

REPORT ZSAT_TEST_STRING.

DATA :  SIGN        TYPE STRING VALUE '-',
        SEPARATOR   TYPE STRING VALUE '#',
        TEXT        TYPE STRING,
        TEXT1       TYPE STRING,
        VALUE       TYPE STRING,
        LV_STRLEN   TYPE I,
        LV_INDEX    TYPE I,
        LV_POS      TYPE I,
        LV_OFFSET_H TYPE I,
        LV_OFFSET_L TYPE I,
        LV_DIFF     TYPE I,
        LV_DIFF1    TYPE I,
        RESULT_TAB  TYPE MATCH_RESULT_TAB,
        WA_TAB      LIKE LINE OF RESULT_TAB,
        WA_TAB1     LIKE LINE OF RESULT_TAB,
        WA_TAB2     LIKE LINE OF RESULT_TAB.

***For test case string value is taken directly.

TEXT = '#470.50-##SA#20191130#40#42.57-#UAH###0034821037#20191219#20191231#20191231#100.75-'.

TEXT1 = TEXT.

CLEAR LV_STRLEN.
LV_STRLEN = STRLEN( TEXT ).

REFRESH RESULT_TAB[].

FIND ALL OCCURRENCES OF SEPARATOR IN TEXT RESULTS RESULT_TAB.

*FIND ALL OCCURRENCES OF CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB IN TEXT RESULTS RESULT_TAB.
IF NOT RESULT_TAB[] IS INITIAL .
  SORT RESULT_TAB BY OFFSET.
***Appending Last Line in Result_Tab in case last value is negative
  CLEAR WA_TAB2.
  READ TABLE RESULT_TAB INTO WA_TAB2 INDEX 1.
  WA_TAB2-OFFSET = LV_STRLEN.
  APPEND WA_TAB2 TO RESULT_TAB.

***Looping Result Tab
  LOOP AT RESULT_TAB ASSIGNING FIELD-SYMBOL(<FS_TAB>).
    IF SY-TABIX = 1.
      CONTINUE.
    ENDIF.

    CLEAR : LV_INDEX, LV_POS, LV_OFFSET_H, LV_OFFSET_L, VALUE, LV_DIFF, LV_DIFF1.

***To extract the VALUE between two separators.
    LV_INDEX = SY-TABIX.
    LV_POS = <FS_TAB>-OFFSET - 1.
    LV_OFFSET_H = <FS_TAB>-OFFSET.
    LV_INDEX = LV_INDEX - 1.
    CLEAR WA_TAB.
    READ TABLE RESULT_TAB INTO WA_TAB INDEX LV_INDEX.
    IF SY-SUBRC = 0.
      LV_OFFSET_L = WA_TAB-OFFSET.
    ENDIF.
    LV_OFFSET_L = LV_OFFSET_L + 1.
    LV_DIFF = LV_OFFSET_H - LV_OFFSET_L.

    VALUE = TEXT+LV_OFFSET_L(LV_DIFF).

***To move negative sign in front of amount value.
    LV_DIFF1 = LV_DIFF - 1.
    IF LV_DIFF1 GT 2.
      IF VALUE+LV_DIFF1(1) = SIGN.
        CALL FUNCTION 'CLOI_PUT_SIGN_IN_FRONT'
          CHANGING
            VALUE = VALUE.
        REPLACE TEXT+LV_OFFSET_L(LV_DIFF) IN TEXT WITH VALUE.
      ENDIF.
    ENDIF.
  ENDLOOP.
ENDIF.
WRITE : / 'Input String'.
WRITE : / TEXT1.
WRITE : / 'Output String'.
WRITE : / TEXT.

This is my first article at SAPYard. I am planning to share all new stuff I learn in my project here. Your comments and feedback will surely help me share quality content. Please feel free to write the good, bad and ugly part of this tuturial.

cOMMENTS pLEASE!!!

Do join 6700+ SAP Technical Consultants in this Telegram SAP Technical Discuss Group. Ask, Answer and Learn is our Motto. You need to install Telegram App first in your mobile or desktop and then click the joining link.

Please SUBSCRIBE to SAPYard’s Youtube Channel for Free End to End SAP Video Course and Training.

Please follow our LinkedIn Page, LinkedIn Group, Facebook Page, Twitter and Instagram.

Do not forget to SUBSCRIBE to our YouTube Channel for Free Courses and Unconventional Interesting Videos.

Save our number +1-646-727-9273 and send us a Whatsapp message ‘LEARN’ to be part of our Learning Community.

Check some more Code Snippets at SAPYard


Viewing all articles
Browse latest Browse all 66

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>