Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Variable Length Files and ABAP

Former Member
0 Likes
553

I have a comma delimited file that is of a variable length.

The first 5 fields are fixed, but then I have a counter field and then 2 fields that repeat for each counter.

So if the counter field was 3, I’d have 6 additional fields, but if the counter was 5, I’d have 10 additional fields.

The goal is to end up with 3 records (since the counter was 3) with the 5 fixed fields then the 2 fields respectively.

Such as if it looks like this:

Abc,abc,abc,abc,abc,003,1a,1b,2a,2b,3a,3c

DCE,abc,abc,abc,abc,005,1a,1b,2a,2b,3a,3c,4a,4b,5a,5b

I need it to look like this or at least be able to read it similar to this.

Abc,abc,abc,abc,abc,003,1a,1b

Abc,abc,abc,abc,abc,003,2a,2b,

Abc,abc,abc,abc,abc,003,3a,3c

DCE,abc,abc,abc,abc,005,1a,1b

DCE,abc,abc,abc,abc,005,2a,2b

DCE,abc,abc,abc,abc,005,3a,3c

DCE,abc,abc,abc,abc,005,4a,4b

DCE,abc,abc,abc,abc,005,5a,5b

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
463

Pam, you can try something like this.



report zrich_0001.

data: record type string.
data: counter type i.
data: index type i.
data: iout type table of string with header line.
data: irec type table of string with header line.

record = 'DCE,abc,abc,abc,abc,005,1a,1b,2a,2b,3a,3c,4a,4b,5a,5b'.

split record at ',' into table irec.

index  = 6.
read table irec index index.
counter = irec.

do counter times.

  clear iout.

  loop at irec.
    check sy-tabix <= 6.
    if sy-tabix = 1.
      iout = irec.
    else.
      concatenate iout irec into iout separated by ','.
    endif.
  endloop.

  do 2 times.
    index  = index + 1.
    read table irec index index.
    concatenate iout irec into iout separated by ','.
  enddo.

  append iout.

enddo.


loop at iout.
  write:/ iout.
endloop.

Regards,

Rich Heilman

2 REPLIES 2
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
464

Pam, you can try something like this.



report zrich_0001.

data: record type string.
data: counter type i.
data: index type i.
data: iout type table of string with header line.
data: irec type table of string with header line.

record = 'DCE,abc,abc,abc,abc,005,1a,1b,2a,2b,3a,3c,4a,4b,5a,5b'.

split record at ',' into table irec.

index  = 6.
read table irec index index.
counter = irec.

do counter times.

  clear iout.

  loop at irec.
    check sy-tabix <= 6.
    if sy-tabix = 1.
      iout = irec.
    else.
      concatenate iout irec into iout separated by ','.
    endif.
  endloop.

  do 2 times.
    index  = index + 1.
    read table irec index index.
    concatenate iout irec into iout separated by ','.
  enddo.

  append iout.

enddo.


loop at iout.
  write:/ iout.
endloop.

Regards,

Rich Heilman

Read only

Former Member
0 Likes
463

Hi,

I guess rich has written more efficient program anyways this is what I could do.

data : begin of i_line occurs 0,

line(100),

end of i_line.

data : begin of i_format occurs 0,

txt1(5),

txt2(5),

txt3(5),

txt4(5),

txt5(5),

txt6(5),

txt7(5),

txt8(5),

end of i_format.

data : comma type c,

off TYPE i,

moff TYPE i,

mlen TYPE i,

count type i,

len(3),

n type i.

start-of-selection.

i_line-line = 'Abc,abc,abc,abc,abc,003,1a,1b,2a,2b,3a,3c'.

append i_line.

clear i_line.

i_line-line = 'DCE,abc,abc,abc,abc,005,1a,1b,2a,2b,3a,3c,4a,4b,5a,5b'.

append i_line.

clear i_line.

comma = ','.

loop at i_line.

off = 0.

count = 0.

moff = 0.

while count lt 5.

count = count + 1.

find comma in section OFFSET off OF

i_line-line

MATCH OFFSET moff

MATCH LENGTH mlen.

if sy-subrc = 0.

off = moff + mlen.

write : / 'moff',moff,

'mlen', mlen..

endif.

endwhile.

moff = moff + 1.

write : i_line-line+moff(3) to len no-zero.

write 😕 'The len is ', len.

i_format-txt1 = i_line+0(3).

i_format-txt2 = i_line+4(3).

i_format-txt3 = i_line+8(3).

i_format-txt4 = i_line+12(3).

i_format-txt5 = i_line+16(3).

i_format-txt6 = i_line+20(3).

n = 24.

do len times.

i_format-txt7 = i_line+n(2).

n = n + 3.

i_format-txt8 = i_line+n(2).

n = n + 3.

append i_format.

enddo.

endloop.

loop at i_format.

write 😕 i_format.

endloop.

end-of-selection.

Regards

Vick