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

Processing character string

Former Member
0 Likes
2,232

Hi All,

I uploaded a CSV file into an itab via GUI_UPLOAD.

Example:

CSV file filled in cell A1 (as viewed in Excel)
--------------------------------------------------------
|     |       A      |        B         |        C     |
--------------------------------------------------------
| 1   | AAA          |                  |              |
|     | BBB          |                  |              |
|     | CCC          |                  |              |
--------------------------------------------------------
| 2   |              |                  |              |
--------------------------------------------------------

Uploaded itab with one line (after GUI_UPLOAD):

AAA#BBB#CCC

I tried SPLITting the above AT '#' but it is not successful, since '#' in itab is a hexadecimal character. Below are the codes:

LOOP AT itab.
  SPLIT itab-text AT '#'
  INTO i_rec-f1 i_rec-f2 i_rec-f3.
ENDLOOP.

PS: Other than SPLIT, i used SEARCH, FIND, TRANSLATE but to no avail. Can someone help? I need the output in another itab with the result similarly to cell A1.

Thanks

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,198

Hi FSCHU,

When you upload, using the function module, there is a parameter for HAS_FIELD_SEPARATOR .

Pass 'X' to that parameter.

Also, you should define the internal table as the same structure as is displayed in the excel.

OR.

The # you see is nothing but a horozontal tab.

YOu have to split the string using the attribute

CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.

loop at itab.

split itab-data at CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.

into itab_new-field1 itab_new-field2...

append itab_new.

clear itab_new.

endloop.

Regards,

ravi

25 REPLIES 25
Read only

Former Member
0 Likes
2,198

Hello

I had a same problem too..............i came over it but declaring a char field c1(say)of length 1 and moving # to c1 and then used the following code

data : c1.

move '#' to c1.

LOOP AT itab.

SPLIT itab-text AT c1

INTO i_rec-f1 i_rec-f2 i_rec-f3.

ENDLOOP.

let me know if it works.

Vj

Read only

0 Likes
2,198

Thanks Vj,

I tested your case but it doesnt work. The itab-text still does not split itself.

Please help

Read only

Former Member
0 Likes
2,198

do this way...use GUI_UPLOAD....this has a field in import parameter....

HAS_FIELD_SEPARATOR...u can mentioned the field sepaartor in this and when upload it will autmatically spli at the field separator into fields...

Read only

Former Member
0 Likes
2,199

Hi FSCHU,

When you upload, using the function module, there is a parameter for HAS_FIELD_SEPARATOR .

Pass 'X' to that parameter.

Also, you should define the internal table as the same structure as is displayed in the excel.

OR.

The # you see is nothing but a horozontal tab.

YOu have to split the string using the attribute

CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.

loop at itab.

split itab-data at CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.

into itab_new-field1 itab_new-field2...

append itab_new.

clear itab_new.

endloop.

Regards,

ravi

Read only

0 Likes
2,198

Hi Ravi,

I tried also to split at CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.

No success. Here are my codes:

*&---------------------------------------------------------------------*
*& Report  ZTEST_SME928                                                *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&                                                                     *
*&---------------------------------------------------------------------*

REPORT  ZTEST_SME928.
PARAMETER: P_FILE LIKE RLGRAP-FILENAME DEFAULT 'C:Documents and Settingsetong.AXONDesktopdamn.csv'.
DATA: V_FILENAME TYPE STRING.
DATA: BEGIN OF I_INPUT1 OCCURS 0,
        TEXT(1000),
      END OF I_INPUT1.


V_FILENAME = P_FILE.
CALL FUNCTION 'GUI_UPLOAD'
  EXPORTING
    FILENAME                = V_FILENAME
    FILETYPE                = 'ASC'
  TABLES
    DATA_TAB                = I_INPUT1
  EXCEPTIONS
    FILE_OPEN_ERROR         = 1
    FILE_READ_ERROR         = 2
    NO_BATCH                = 3
    GUI_REFUSE_FILETRANSFER = 4
    INVALID_TYPE            = 5
    NO_AUTHORITY            = 6
    UNKNOWN_ERROR           = 7
    BAD_DATA_FORMAT         = 8
    HEADER_NOT_ALLOWED      = 9
    SEPARATOR_NOT_ALLOWED   = 10
    HEADER_TOO_LONG         = 11
    UNKNOWN_DP_ERROR        = 12
    ACCESS_DENIED           = 13
    DP_OUT_OF_MEMORY        = 14
    DISK_FULL               = 15
    DP_TIMEOUT              = 16
    OTHERS                  = 17.

DATA: BEGIN OF I_REC OCCURS 0,
         F1(40),
         F2(40),
         F3(40),
      END OF I_REC.

LOOP AT I_INPUT1.
  SPLIT I_INPUT1-TEXT AT '0A' 
  INTO I_REC-F1
       I_REC-F2
       I_REC-F3.
  BREAK FFXC096.
ENDLOOP.

Read only

0 Likes
2,198

hey

Did u try slitting in CHAR MODE?

Vj

Read only

0 Likes
2,198

Hi Vj,

Thanks, but isn't it by default IN CHARACTER MODE?

Anyway, I tried but not successful either.

Pls give me more inputs

Read only

0 Likes
2,198

hello

Yeah you are right...................checking in case.

Let me see into it and will get back into it.

Vj

Read only

0 Likes
2,198

hey , you had tried in wrong way , if u use direclt SPLIT at '0A' , it will search for 0A and then split

declare one varible of type X and try

data : v_char type X value '0A'.

LOOP AT I_INPUT1.
  SPLIT I_INPUT1-TEXT AT v_char  INTO I_REC-F1
       I_REC-F2
       I_REC-F3.
  BREAK FFXC096.
ENDLOOP.

Read only

0 Likes
2,198

No worries Vj.

Thanks...

Anyone else with more info? Pls share

Read only

0 Likes
2,198

Hi Chandra,

i tried but it gives me a syntax error. It says 'v_char must be a character-type data object (data type C, N, D, T or STRING)

thanks...

Read only

0 Likes
2,198

If you have a csv file and you are not seeing, (,) as the column separator, then your file has not been saved correctly. Check the file again, and make sure that it is saved as a "Comma delimited file" and that comma is the separator. Then this program should work fine.



report zrich_0001.

types: begin of ttab,
       rec(1000) type c,
       end of ttab.

types: begin of tdat,
       fld1(10) type c,
       fld2(10) type c,
       fld3(10) type c,
       end of tdat.

data: itab type table of ttab with header line.
data: idat type table of tdat with header line.

data: file_str type string.

parameters: p_file type localfile.

at selection-screen on value-request for p_file.
  call function 'KD_GET_FILENAME_ON_F4'
       exporting
            static    = 'X'
       changing
            file_name = p_file.

start-of-selection.

  file_str = p_file.

  call function 'GUI_UPLOAD'
       exporting
            filename                = file_str
       tables
            data_tab                = itab
       exceptions
            file_open_error         = 1
            file_read_error         = 2
            no_batch                = 3
            gui_refuse_filetransfer = 4
            invalid_type            = 5
            no_authority            = 6
            unknown_error           = 7
            bad_data_format         = 8
            header_not_allowed      = 9
            separator_not_allowed   = 10
            header_too_long         = 11
            unknown_dp_error        = 12
            access_denied           = 13
            dp_out_of_memory        = 14
            disk_full               = 15
            dp_timeout              = 16
            others                  = 17.


  loop at itab.
    clear idat.
    split itab-rec at ',' into idat-fld1
                               idat-fld2
                               idat-fld3.
    append idat.

  endloop.


  loop at idat.
    write:/ idat-fld1, idat-fld2, idat-fld3.
  endloop.

Regards,

Rich Heilman

Read only

0 Likes
2,198

Hi Rich,

Thanks but I dont have problem splitting the columns.

My problem is the upload file is filled with multiple lines in one cell. The multiple lines appear as different lines in the cell.

CSV file filled in cell A1 (as viewed in Excel)
--------------------------------------------------------
|     |       A      |        B         |        C     |
--------------------------------------------------------
| 1   | AAA          |                  |              |
|     | BBB          |                  |              |
|     | CCC          |                  |              |
--------------------------------------------------------
| 2   |              |                  |              |
--------------------------------------------------------

Im using GUI_UPLOAD and the CSV file is uploaded in my ITAB as follow:

AAA#BBB#CCC

#: hexadecimal formal to indicate new line.

So I have problem splitting AAA#BBB#CCC at #. I tried all methods that they posted in the forum, but no success. Please help.

Thanks

Read only

0 Likes
2,198

TRY THIS

SPLIT ITAB AT <b> CL_ABAP_CHAR_UTILITIES=>NEWLINE</b>

Read only

0 Likes
2,198

Thanks Chandra, so much!!!!

Now it is 12.23am here. You saved my day 😃

Read only

0 Likes
2,198

Just did not read ur question completely, otherwise could have solved a bit before...Glad that it was solved

Read only

Former Member
0 Likes
2,198

try this...


data : v_char type X value '0A'.

LOOP AT itab.
  SPLIT itab-text AT v_char  INTO i_rec-f1 i_rec-f2 i_rec-f3.
ENDLOOP.

Read only

0 Likes
2,198

Hi Chandra,

not working either. Thanks

Read only

Former Member
0 Likes
2,198

Hi,

LOOP AT itab.

REPLACE '#' with SPACE into itab-text.

ENDLOOP.

<b>OR</b>

data: var(1) type c.

var = '#'.

LOOP AT itab.

SPLIT itab-text AT var

INTO i_rec-f1 i_rec-f2 i_rec-f3.

ENDLOOP.

Regards

Sudheer

Read only

Former Member
0 Likes
2,198

HI FSCHU,

look via the debugger (set a break-point before split) at

find out the HEX of #.

than declare a variable

and a slip with this variable.

Regards, Dieter

Read only

0 Likes
2,198

Hi Dieter,

I tried but still not successful. Do I need to SPLIT ... IN BYTE MODE?

Thanks

Read only

0 Likes
2,198

use HAS_FIELD_SEPARATOR in export parameter .

in that specify ' , ' (comma) as field seperator.

Message was edited by:

Kalpanashri Rajendran

Read only

0 Likes
2,198

Hi Kalpa,

My problem now is splitting a column with multiple lines, in which the lines are all in different lines.

Please refer to my initial post for more info.

Thanks lots

Read only

Former Member
0 Likes
2,198

IF U CAN mark the end of the string with a #

like 'AAA#BBB#CCC#'.

then a logic is there to work out .

Execute the code and see .

data :VAL(15) TYPE C VALUE 'AAA#BBB#CCC#'.
DATA :CNT TYPE I.
DATA:  v type c,
       n type i.
DATA : FINAL(15) TYPE C.
DATA : BEGIN OF ITAB OCCURS 0,
       F1(10) TYPE C,
       END OF ITAB.

    CNT = STRLEN( VAL ).

    DO CNT TIMES .
     move val+n(1) to v.
    if v ca '#'.
    CONDENSE FINAL NO-GAPS.
    ITAB-F1 = FINAL.
    APPEND ITAB.
    CLEAR ITAB.
    CLEAR FINAL.
    ELSE.
    move v to final+n(1).
    endif.
    n = n + 1.
    ENDDO.

    LOOP AT ITAB.
    WRITE:/ ITAB-F1.
    ENDLOOP.

hope if this can make some help .

still this needs little adjustments .:)

regards,

vijay.

Read only

0 Likes
2,198

Hi Sniper,

For your example, it will work cos VAL ("AAA#BBB#CCC#") is declared as a variable of type C.

But my problem now is that the VAL is in an uploaded format from a CSV file, and the # refers to hexadecimal format for new line.

thanks