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: 

Convert a string into an Internal Table

Former Member
0 Kudos

Hi,

What is the easiest way of convert a string into an internal table? Basically I'm calling a function which expects the data in a table format. The data I have is in string format.

The table format is:

data it_srdidata type SDOK_SDATX occurs 0.

SDOK_SDATX Data type is raw, length 1022.

initial thought ,was use a loop, and offset and copy in?

17 REPLIES 17

Former Member
0 Kudos

Hi Sims,

I guess you have to use offset looping at your table,as you dont know the length at which you will split the string to the internal table fields.

0 Kudos

The length of the internal table rows is 1022

Former Member
0 Kudos

Hi Sims,

Do you want to split this 1022 length field to different fields??

0 Kudos

Hi,

Now, the string data is from a function a function

CALL FUNCTION 'ZST_CREATE_RDIOUTPUT'

EXPORTING

ispoolnumber = spool_no

IMPORTING

srdioutput = srdidata.

The next function I call, needs the data srdidata in an internal table, of which each row is defined as type raw with a length of 1022

0 Kudos

use below logic to populate into a internal table. for testing I used 5, you can take 1022 so that 1022 chars will be populated into each record.

data : begin of it_st occurs 0,

str(1022),

end of it_st.

data : st(10000).

data : l type i,

dl_start type i,

dl_end type i.

st = '12345678910'.

l = strlen( st ).

if l >= 1.

clear: dl_start, dl_end.

do.

dl_start = dl_end + 0.

dl_end = dl_end + <b>5.</b>

if dl_end > l.

dl_end = l.

endif.

it_st-str = st+dl_start(dl_end).

append it_st.

if dl_end >= l.

exit.

endif.

enddo.

endif.

loop at it_st.

write 😕 it_st-str.

endloop.

0 Kudos

that did not quite work,

the code is below

REPORT z_fs_covus_print_preview_test .

PARAMETERS spool_no TYPE i.

DATA: srdidata TYPE string,

mime_content LIKE bapiconten OCCURS 0,

mime_type TYPE char128,

mime_subtype TYPE char128,

mimesize TYPE i,

len type i,

loop_counter type i,

remainder type i.

data it_srdidata type SDOK_SDATX occurs 0.

spool_no = 1708.

  • Get the info from the spool and import into srdidata

CALL FUNCTION 'ZST_CREATE_RDIOUTPUT'

EXPORTING

ispoolnumber = spool_no

IMPORTING

srdioutput = srdidata.

data : l type i,

dl_start type i,

dl_end type i.

l = strlen( srdidata ).

if l >= 1.

clear: dl_start, dl_end.

do.

dl_start = dl_end + 0.

dl_end = dl_end + 1022.

if dl_end > l.

dl_end = l.

endif.

it_srdidata = srdidata+dl_start(dl_end).

append it_srdidata.

if dl_end >= l.

exit.

endif.

enddo.

endif.

loop at it_srdidata.

write 😕 it_srdidata.

endloop.

and the message is:

the type of "it_srdidata" cannot be converted to the type of srdidata+dl_start(dl_end).

0 Kudos

SIMS - You need to create and use a work are for it_srdidata.

loop at it_srdidata into wa_srdidata 

Rob

0 Kudos

Hi Sims,

You can't assign a string to raw using the substring like that. You have to convert the text to binary and then add that to your internal table. YOu can use FM SCMS_TEXT_TO_BINARY to convert your string to binary variable. You need to replace "it_srdidata = srdidata+dl_start(dl_end)", with following FM call -

data : begin of gt_text occurs 0,

data(1022),

end of gt_text.

data : begin of gt_bin occurs 0,

data type SDOK_SDATX,

end of gt_bin.

gt_Text = srdidata+dl_start(dl_end)..

append gt_text.

call function 'SCMS_TEXT_TO_BINARY'

  • EXPORTING

  • FIRST_LINE = 0

  • LAST_LINE = 0

  • APPEND_TO_TABLE = ' '

  • MIMETYPE = ' '

  • IMPORTING

  • OUTPUT_LENGTH =

tables

text_tab = gt_text

binary_tab = gt_bin

EXCEPTIONS

FAILED = 1

OTHERS = 2

.

if sy-subrc = 0.

read table gt_bin index 1.

append gt_bin to it_srdidata .

endif.

0 Kudos

Hi Sims,

One minor change to the coding i mentioned above,

Declare gt_bin as below without header line...

data : gt_bin type SDOK_SDATX occurs 0,

gs_bin type SDOK_SDATX.

gt_Text = 'Line1'.

append gt_text.

call function 'SCMS_TEXT_TO_BINARY'

  • EXPORTING

  • FIRST_LINE = 0

  • LAST_LINE = 0

  • APPEND_TO_TABLE = ' '

  • MIMETYPE = ' '

  • IMPORTING

  • OUTPUT_LENGTH =

tables

text_tab = gt_text

binary_tab = gt_bin

EXCEPTIONS

FAILED = 1

OTHERS = 2

.

if sy-subrc = 0.

read table gt_bin into gs_bin index 1.

append gs_bin to it_srdidata.

endif.

Otherwise it might give unicode error. Pls let me know if this doesn't work.

Regards

Hari

0 Kudos

Hi,

it does not need to be binnary. But I still get an error, not sure what it is, substituted a smaller string and still get the same error - offset and length incorrect.

The code is shown below, simplified so it can be run but still gives an error

DATA : BEGIN OF gt_text OCCURS 0,

data(1022),

END OF gt_text.

data srdidata type string value

'Ornages and Lemon on the hills of St Clements'.

DATA : l TYPE i,

dl_start TYPE i,

dl_end TYPE i.

l = strlen( srdidata ).

IF l >= 1.

CLEAR: dl_start, dl_end.

DO.

dl_start = dl_end + 0.

dl_end = dl_end + 3.

IF dl_end > l.

dl_end = l.

ENDIF.

IF sy-index = 30.

WRITE ''.

ENDIF.

gt_text = srdidata+dl_start(dl_end).

APPEND gt_text.

IF dl_end >= l.

EXIT.

ENDIF.

ENDDO.

ENDIF.

WRITE ''.

0 Kudos

Hi Sims,

This is what the analysis says,

However, the sum of the offset (24) and length (27) specifications

was greater than the length of the string (45).

This is not allowed.

This happened for some 6th or 7 th loop pass when the offset + legth > length of the string which is not allowed.

Regards,

Ravi

0 Kudos

Hi sims,

see if the following is useful,

SPLIT text AT space INTO TABLE itab.

Regards,

Ravi

0 Kudos

Thanks for that, it help me work out what I was doing wrong.

0 Kudos

Hi Sims,

I am not really sure of your loop logic, I haven't checked it. But if you want assign a string to a raw variables( it_srdidata is raw table), you have to convert the text to raw and then assign. This is where FM SCMS_TEXT_TO_BINARY is useful. Hope you fixed the Loop problem with Ravi's analysis and fixed the assigment issue with this function call.

Hari

0 Kudos

Hi,

you don't have to call the function to convert to binary, it does it implicitly.

Thanks.

0 Kudos

Hi Sims,

It does if you are working in the latest version of SAP with UNICODE Check active. With UNICODE activated, the casting is not implicit but has to be explicit all the time. If UNICODE is not activated then your code will work as it is,otherwise you will get a unicod error. Ofcourse, you can deactivate the unicode check in the program attributes. We did have the same problem in converting text to binary so we have to use this FM to avoid unicode error.

Hari

0 Kudos

use function module

CONVERT_STRING_TO_TABLE for converting string to itab.

Regards

Raja