BMW3000
BMW3000

Reputation: 111

How do I split multiple times in abap?

I want to split my code into a workarea and append this to a an internal table for a later perform.

But sometimes the text contains more than 3 numbers for example 3;5;3;6;2;5 but its always 3,6,9,12... number. How can I solve the problem that I want to loop 3 times, then the next 3 numbers and so on?

DATA: text(100) type c,
it_1 TYPE STANDART TABLE LIKE text,
it_2 TYPE STANDART TABLE LIKE text,
it_3 TYPE STANDART TABLE LIKE text,
string(100) TYPE c.


text = '123;2;2'.

SPLIT text AT ';' INTO  wa_1-c1 wa_1-c2 wa_1-c3.

APPEND wa_1-c1 to it_1.
APPEND wa_1-c2 to it_2.
APPEND wa_1-c3 to it_3.

LOOP at it_1 INTO string.
PERFORM task using string.
ENDLOOP.

Upvotes: 1

Views: 19158

Answers (3)

AmNoOne
AmNoOne

Reputation: 94

You can try using Sy-tabix if you want to control the iterations three times and since you are saving the text values in 3 different internal tables.

DATA: text(100) type c,
it_1 TYPE STANDARD TABLE OF text,
it_2 TYPE STANDARD TABLE OF text,
it_3 TYPE STANDARD TABLE OF text,
string(100) TYPE c.

text = '123;2;2'.

SPLIT text AT ';' INTO  TABLE it_1.

LOOP at it_1 INTO string WHERE sy-tabix = 3.
WRITE : string.
ENDLOOP.
 if sy-tabix = 3.
   LOOP AT it_2 INTO string WHERE sy-tabix = sy-tabix+3.
     "do the next loop    
     ENDLOOP.
   ENDIF.

Upvotes: -1

József Szikszai
József Szikszai

Reputation: 5071

This will split the string in 3-er blocks, while overwriting it with the rest:

WHILE text IS NOT INITIAL.
  SPLIT AT ';'
        INTO wa_1-c1
             wa_1-c2
             wa_1-c3
             text.
  APPEND: wa_1-c1 to it_1,
          wa_1-c2 to it_2,
          wa_1-c3 to it_3.
ENDWHILE.

Please note, the string variable text will be initial at the end, if its original value is still needed, than you can define another string, copy the value and use that one for the split.

Upvotes: 1

Bryan Cain
Bryan Cain

Reputation: 1776

You should use the INTO TABLE addition to the split keyword rather than hard coding the fields.

DATA: text_s TYPE string.

text_s = '123;2;2'.

DATA: text_tab TYPE TABLE OF string.

SPLIT text_s AT ';' INTO TABLE text_tab.

LOOP AT text_tab ASSIGNING FIELD-SYMBOL(<line>).

  "do whatever on each token here

ENDLOOP.

Upvotes: 5

Related Questions