Application Development 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: 

Problem while splitting a string based on ' comma '

Former Member
0 Kudos
351

Hi,

I'm trying to upload a file in .csv format into an internal table. For this i'm declaring an internal table it_iflat and a work area wa_xflat as shown below.

DATA: it_iflat type table of string,

wa_xflat type string.

Now, once i have data in it_iflat..i loop at each record and split it at the occurance of a comma. as shown below.

DATA: it_irec type table of string with header line.

loop at it_iflat into wa_xflat.

clear it_irec.

refresh it_irec.

split wa_xflat at ',' into table it_irec.

This is working fine when the <b>last column</b> of the excel sheet is populated. But when this field is left blank in the excel sheet..the total number of values that i get in it_irec is one less than the actual.

Is this happening because the last column doesn't have a value and since its after the last comma..its getting neglected?

Could anyone please let me know how to resolve this.

Thanks in advance

Harsha.

7 REPLIES 7

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos
153

If there is a value in the last column, then the value should appear in the last column of your itab.

Maybe try it this way instead.




report zrich_0001.


data: it_iflat type table of string,
      wa_xflat type string.


data: begin of it_irec occurs 0,
      fld1(10) type c,
      fld2(10) type c,
      fld3(10) type c,
      end of it_irec.

clear it_irec.   refresh it_irec.

loop at it_iflat into wa_xflat.
  clear it_irec.
  split wa_xflat at ',' into it_irec-fld1
                             it_irec-fld2
                             it_irec-fld3.
  append it_itrec.
endloop.



Regards,

Rich Heilman

Message was edited by: Rich Heilman

Former Member
0 Kudos
153

Hi,

Thats right. If there is a value in the last column..it appears in the last column of the int table it_irec.

But my problem is...say we have a file like this

col1 col2 col3 col4

row1 A B C D

row2 E F G <blank>

Now when the first row is split and populated into it_irec table.. the sy-tfill value is 4

and when row2 is split and populated in the table it_irec..sy-tfill value is 3..

I need a proper value in sy-tfill because i would be using this value to do further operations.

I was thinking of checking for the value in the last column and if its blank ..then increment sy-tfill by one and save it in a variable and then go ahead with rest of the processing.

I would like to know ..if there is any other way to acheive this.

Thanks,

Harsha.

0 Kudos
153

I don't really know what the "rest of your processing" is. Can you let me know?

Regards,

Rich heilman

0 Kudos
153

Hi,

I'd like to add something else..

The excel file i would be getting would not always have the same number of fields..it depends on the type of table selected from thef selection screen..and so i generate the internal table at runtime and then populate that excel sheet data into it..

so i cant use the split command as shown above..

i.e. split wa_xflat at ',' into field1,

field2...etc.

0 Kudos
153

I see, so you never know how many field you are dealing with?

Regards,

Rich Heilman

0 Kudos
153

Hi Rich,

My requirement is something like this..

The excel file supplied for uploading depends on the table selected on the selection screen. So i generate an internal table at runtime based on the table selected and then upload that table.

Now the excel sheet structure is not exactly the same as teh table structure..ie few fields of the excel sheet appear in the first and second columns..but in the table these form the second and third fields from the last..

So what i do is..after i split the string based on comma's ..i take the sy-tfill value..now to access the last fields in the table..i increment the sy-tfill and then populate it.

So for this it is important for me to know the proper sy-tfill value.

Thanks

0 Kudos
153

If you use:

DESCRIBE FIELD <F> TYPE TYP COMPONENTS N. 

'N' will have the number of fields in the structure. Then, if there are not enough lines in the internal table, you can append one.

Rob