seen this syntax in some or other programming language: for (i=1; i<=3; i++).Why would ABAP stay behind?
But old habits die heard. The lazy developers who were comfortable with traditional ABAP programming find it challenging to use the new syntax. In this article today, we would discuss one requirement which we can easily achieve in old ABAP, but needed some help for the new syntax.
This post is again the result of the question we had in our SAP Technical Telegram Group. One of our very active and helpful members, Wesley Massini had a program where he was reading data from a file with fixed length. It was working fine and with no issue. But later on, the business decided to separate the data with underscore “_” and the file data is not fixed any more. They want to separate the data into columns at the delimiter underscore. This delimiter can be anything viz comma(,), tilds (~), pipe (|) etc.
Sounds an easy requirement right? But can you do using the new ABAP Syntax? ?
Also Take – Free Video Course on New Syntax in ABAP
Existing Code for Fixed Length Data
The data in the file would look like this:
It is a file with heterogeneous data. The data in the first row and second row are different. For example, the first two fields (yellow and blue) in both the rows are Plant and Material. But the third field in red is Vendor is first row and while it is an indicator in second row. Similarly fourth field in green is a custom type in first row and some category in second row. The idea is, all the data rows in the file for type first should go to one internal table and all rows like that of second type should go into another internal table.
We used SWITCH statement in new syntax and separated the rows based on the length of the data in the file (which is fixed). Row type one has 26 characters while row type two has 18 characters. Also, we do not need to show the extension .txt.
As author and ABAPer Paul Hardy says, CDS is ABAP in Steriods. We say, not only CDS, the new ABAP itself looks like super active ABAP. ?
Check the above screenshot. We have In-line data declaration, usage of VALUE, FOR Loop in internal table, SWITCH and #. Do a F4 on each of these keywords and try to understand the concept.
For our case, we have know the TYPE, therefore they come after the VALUE operator. Also after SWITCH there is #. The table types are declared like below.
In order to display the output, we can write the below syntax.
How to Handle Blank Rows?
We are not sure, how to avoid the blank rows in both the tables using the modern syntax. We used a DELETE statement explicitly after the data was populated in the internal tables.
If you know how to remove the blank lines in the FOR LOOP with SWITCH, please provide the solution in the comments section. We will update this article with your solution.
Also Read – My First Program in S/4HANA
Earlier our file was fixed length. With the business change, we started receiving file with a delimiter underscore “_”. So, we did not need to worry about the length of each fields. But again, we struggled to figure out the best way to SPLIT at “_” using new ABAP.
After some research and with our new knowledge, we achieved the functionality using STRLEN, SUBSTRING_BEFORE, SUBSTRING_AFTER , sub, occ string functions along with the VALUE and FOR Loop and COND keywords.
Note: The key word occ is for Occurrence. You might see a negative number for occ = -1. If occ is positive value, the occurrences are counted from the left while if occ is a negative number, it is counted from the right side.
Similarly check the keyword sub. It is for Substring. Sub looks for the characters in the string.
<code>DATA result TYPE string.
result = substring( val = 'ABCDEFGH' off = 2 len = 2 ).
result = substring_from( val = 'ABCDEFGH' sub = 'CD' ).
result = substring_after( val = 'ABCDEFGH' sub = 'CD' ).
result = substring_before( val = 'ABCDEFGH' sub = 'CD' ).
result = substring_to( val = 'ABCDEFGH' sub = 'CD' ).</code>
The above snippet is from SAP Help to show the usage of SUB and different SUBSTRING functions. The output for each result is “CD”, “CDEFGH”, “EFGH”, “AB”, and “ABCD” in the same order.
The above code also gave a blank line and we were forced to use the DELETE statement explicitly. Also, we had COND in the syntax which could be avoided. We achieved the business requirement, but we were still wondering if there is a better way to achieve it. Without the DELETE statement and COND. And guess what? Stephan gave a flawless solution yet again!!
Also Check – How I Created My First SAP OData Services
Solution 2 from Stephan
Stephan recommended to put an identifier in the beginning of the data row for the file. For our case, it is VM and PM. Also, with his new solution logic there is no need to use COND.
Check the solution above. He has used WHERE clause in the FOR Loop. There is no need for WHEN and THEN. Also, the STRLEN check of each row is prevented along with COND #.
* Table Type declaration
* My recommendation is to put an identifier at the beginning of the filename like in this sample.
* With this logic there is no need for usage of COND like in the first sample.
* Add both table data for display
** Show the output
Download SPLIT in New ABAP Program for all the three methods shown in this article and practice it.
If you have a better solution, please do share. Your feedback and comments are welcome. Please let them coming.
5450+ SAP Technical Practitioners from 6 Continents of the SAP World. Install Telegram app and click this link to join – Telegram SAP Technical Discuss Group.
Check Some Useful Code Snippets
- Dynamic Where Condition usage in Database queries
- UNIX Command in SAP ABAP
- Send e-mail with subject line greater than 50 characters
- Pop Up Screen with Selection Option using FM POPUP_WITH_TABLE_DISPLAY
- String wrap
- Write Application Log
- Sample program to attach any file from application server to any Business Object
- Sample program to unlock the editor of a program.
- Why are developers so fond of ‘REUSE_ALV_GRID_DISPLAY’?
- DELETING rows of the internal table within the LOOP. Is it a Taboo? A big NO NO?
- Creating Dynamic Internal Table
- Selection Screen in SAP
- How to update custom field of PRPS table
- Get Latitude and Longitude of any place using Google Map API in SAP
- GPS like tool in SAP using Google Map API
- Know Who is doing What in your SAP System
- OOPs Report Using Splitter and ALV Tree Combination
- ALV with an Editable Row
- Extensive Tips and Tricks for Interactive SAP ALV
- How to Get Accurate Pricing in SD for Customer and Material?
- T-Guard: Transport Dependency Validation Tool
- Utility Program to Auto Format the Texts into Meaningful Sentence
- ABAP for SAP HANA. Part XX. ALV Report On SAP HANA – Opportunities And Challenges
- Selective Handling of the Buttons in ALV Grid Toolbar
- Working with Shell Scripts in SAP ABAP Environment
- CDS Part 7. Basic Expressions & Operations Available for CDS View – II
- Sudoku Solver and Generator Tool Using Vanilla SAP ABAP
- Calculator in SAP using New ABAP Syntax
- SO10 Enhancement to Add Texts to the Transport Request Automatically
- How to pass Reversal Date & Reason to BAPI_ACC_DOCUMENT_POST?
- ISU – Bankruptcy Overview and Write-Off Process using BAPI_CTRACDOCUMENT_WRITEOFF
- How to Create or Update Variant for a Report from another Program?
- A to Z of OLE Excel in ABAP 7.4
- SORTing Algorithm – Interview Question
- SOLMAN – Mail Forms Custom Enhancement Guide
- SORTing Algorithm – Performance Comparision
- How to Get Data from Web Page using SAP ABAP?
- Dynamic Code Processor
- Useful Trick to Search “Text” in Smartforms/Text modules
- Dynamically Download Data From Any SAP Table in ABAP-740 – Part 1
- How to SPLIT Data in FOR LOOP Using Modern ABAP Syntax?
- Dynamic Table Download in ABAP 751 – Part 2