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

offset operation on string

Former Member
0 Likes
6,356

experts,

the following is the sample code where i want to assign

a part of a string value to the variable.

when executed, program is getting shortdumped with the

following message.

In the running program "Z_FORSTR", part of a string was about to be accessed with an explicitly specified length.

However, this length (5) exceeded the current length of the string (3).

This kind of access is illegal.

how to solve the error.

REPORT z_forstr.

DATA : str TYPE string, str1 TYPE str.

DATA : BEGIN OF itab OCCURS 0,

line TYPE char5,

END OF itab.

DATA : l TYPE i,

j TYPE i,

m TYPE i.

str = 'ABCDEFGHIJKLMNOPQRSTUVWXZYABDCEFH'.

l = strlen( str ).

j = l DIV 5.

m = l MOD 5.

IF m <> 0.

j = j + 1.

ENDIF.

DO j TIMES.

itab-line = str+0(5).

APPEND itab-line TO itab.

CLEAR itab-line.

str = str+5.

ENDDO.

LOOP AT itab.

WRITE 😕 itab-line.

ENDLOOP.

thanks in advance.

1 ACCEPTED SOLUTION
Read only

BGarcia
Active Contributor
0 Likes
2,219

Hello Rae,

The shortdump occurs because in last step of the loop, str variable was 3 characters and you're asking for 5. Try to check first if variable str has less than 5 characteres in last part of the loop.

You can try like this:


DO j TIMES.
  l = strlen( str ).
  IF l > 5.
    itab-line = str+0(5).
    str = str+5.
  ELSE.
    itab-line = str.
  ENDIF.
  APPEND itab-line TO itab.
  CLEAR itab-line.
ENDDO.

Kind regards.

4 REPLIES 4
Read only

BGarcia
Active Contributor
0 Likes
2,220

Hello Rae,

The shortdump occurs because in last step of the loop, str variable was 3 characters and you're asking for 5. Try to check first if variable str has less than 5 characteres in last part of the loop.

You can try like this:


DO j TIMES.
  l = strlen( str ).
  IF l > 5.
    itab-line = str+0(5).
    str = str+5.
  ELSE.
    itab-line = str.
  ENDIF.
  APPEND itab-line TO itab.
  CLEAR itab-line.
ENDDO.

Kind regards.

Read only

vinod_vemuru2
Active Contributor
0 Likes
2,219

Hi,

I copy pasted ur code and i am not getting any dump. But result is wrong.

Modify the code like this.

DO j TIMES.

itab-line = str+0(5).

APPEND itab-line TO itab.

CLEAR itab-line.

str = str+5.

ENDDO.

itab-line = str.

APPEND itab-line TO itab.

Above two lines will put the remaining string into itab.

Thanks,

Vinod.

Read only

Former Member
0 Likes
2,219

change this code...

IF m eq 0.   <--- use EQ
j = j + 1.
ENDIF.

Read only

Former Member
0 Likes
2,219

thanks bruno problem solved