Mastering ABAP: In-Depth Analysis of String Operations and Internal Tables for Efficient SAP Development

 Welcome back everyone, we were discussing about String operations in our last blog, So let’s continue it.


String Operations : TRANSLATE

  • The purpose of translate is to convert the string to uppercase or lowercase..

  • Syntax : TRANSLATE <string> TO UPPER CASE / LOWER CASE.

  • In the above syntax : TRANSLATE = keyword, <string> = the string which needs to be converted, TO = keyword, UPPER CASE / LOWER CASE = keyword.

    ***********************************************************
    *Start of Program
    DATA : lv_input(50) type C value 'Welcome to Home',
           lv_input1(50) type c value 'welcome to home'.
    
    translate lv_input to LOWER CASE.
    "Display the first input.
    write : 'First input after translating to lower case : ',
          / lv_input.
    
    TRANSLATE lv_input1 TO UPPER CASE.
    *Displaying the second input.
    write :/ 'Second input after translating too upper case : ',
           / lv_input1.
    
    *End of Program
    **************************************************************
    


    Output :-




String Operations : SHIFT

  • The purpose of shift is to shift the contents of a string.

  • It shifts the string by a number of places.

  • Syntax :- shift string by n places <mode>. {by default mode if left}.

  • In the above syntax : shift = keyword, string = string which needs to be shifted, by = keyword, n = number, places = keyword, <mode> = left/right/circular.

    *************************************************************
    *Start of Program
    DATA : lv_input1(10) TYPE c VALUE '0123456789',
           lv_input2(10) TYPE c VALUE '0123456789',
           lv_input3(10) TYPE c VALUE '0123456789'.
    
    "Applying shift to left.
    SHIFT lv_input1 BY 5 PLACES LEFT. " by default left
    WRITE : 'Left : ',
          / lv_input1.
    
    *Applying shift to right.
    SHIFT lv_input2 BY 5 PLACES RIGHT.
    WRITE :/ 'Right :',
           / lv_input2.
    
    *Applying shift to circular
    SHIFT lv_input3 BY 5 PLACES CIRCULAR.
    WRITE :/ 'Circular : ',
           / lv_input3.
    
    *End of Program
    *************************************************************
    


    Output :-



    • Since length is 10, in first case first five elements gets removed and in second case last five elements were removed because of shift operation.

String Operations :- Substring Processing

  • Substring is a part of the string or small set of characters from the string.

  • Depends upon the requirement we need to process the substring.

  • Syntax :- Target_variable = Source_variable [+] [starting position of substring](length of the substring).

    **********************************************************
    *Start of Program
    DATA : lv_value(50) type c value '91-040-1234567890'.
    DATA : lv_country(2) type c.
    DATA : lv_city(3) type c.
    DATA : lv_number(10) type c.
    
    *Fetching Country Code
    lv_country = lv_value+0(2).
    write : 'Country Code : ',
          / lv_country.
    
    *Fetching the city Code
    lv_city = lv_value+3(3).
    write :/ 'City Code : ',
           / lv_city.
    
    *Mob no
    lv_number = lv_value+7(10).
    write :/ 'Mobile number : ',
           / lv_number.
    
    *End of Program
    ***********************************************************
    


    Output :-




Internal Tables :-

  • Internal table is a temporary storage of data on application layer.
  • Internal Table stores any number of records at run time.
  • A very important use of internal tables is for storing and formatting data from a database table within a program.

Requirement :-

Q. Suppose the user wants us to write a program to take order number as input and to display description of order from order header table as an output.



Ans. Now we know that our data is stored in database layer. So whenever we will fetch the data from database layer, we will have to store it at some places. Internal table is the place where actual data will be stored at the run time on the application layer.

Work Area :-

  • Work area is also a temporary storage of data on application layer.
  • Work area are single rows of data.
  • It is used to process the data in an internal table, one line at a time.

Practical Implementation :-

  • Create a executable program in ABAP Editor.



  • Steps :- Create a type structure → define the input variable and define the internal table and work area.

    ****************************************************
    *Start of Program
    *Creating a structure.
    TYPES : BEGIN OF lty_header,
              order_number TYPE zar_order_number,
              payment_mode TYPE zar_payment_mode,
            END OF lty_header.
    
    *Defining Order number
    DATA : lv_order_no TYPE zar_order_number.
    
    *Defining internal table and work area for above structure
    DATA : lt_header type table of lty_header, "lt denotes local internal table
           ls_header type lty_header.  "ls denotes local work area
    
    *End of Program
    *************************************************************
    



Internal Table Operations :-

  • Append :- It is used to insert data at the last of the internal table.
  • Delete :- It is used to delete the records from the internal table.
  • Modify :- It is used to modify the records of the internal table.
  • Loop :- It is used to read the records one by one from the internal table.
  • Read table :- It is used to read the first matching from the internal table.
  • Clear, Refresh :- It is used to clear the contents of the internal table.
  • Collect :- It is used to make sum of amount values based upon unique character values.

Append Implementation :-

  • It is used to insert data at the last of the internal table.
  • Values in SAP always passes from right to left.
  • It is always very important to clear the work area after transfer of data into internal table.
  • Must check how data is getting stored in debugging mode.
********************************
ls_header-order_number = 1.
ls_header-payment_mode = 'C'.
APPEND ls_header TO lt_header.
clear ls_header.

ls_header-order_number = 2.
ls_header-payment_mode = 'N'.
APPEND ls_header TO lt_header.
clear ls_header.

ls_header-order_number = 3.
ls_header-payment_mode = 'D'.
APPEND ls_header TO lt_header.
clear ls_header.
**********************************

So, that’s enough for today.

We will continue with internal table operations in the next blog.

Thanx alot for being a part of this wonderful journey.

Comments

Popular posts from this blog

Understanding Different Types of SAP Function Modules: Normal, RFC, and Update

Unlocking SAP ABAP Secrets: Interactive Classical Reports with GET CURSOR, At User Command, and a Comparison of HIDE Statements

Mastering ABAP: A Step-by-Step Guide to Function Modules and Groups for Modularized Programming