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: 

Reg. Do Varying f from f2 next f3 range f5

former_member191468
Participant
0 Kudos
150

Hi All,

I have coded the below code and executed it is giving dump. I could not under stand what is the excat use of Range in the statement.

Please kindly let me know if any one have used this statement.

I would like excat use of the statement.

DATA : begin of g_s_vbak_vbap,

field1 type c value 'A',

field2 type c value 'B',

field3 type c value 'C',

field4 type c value 'D',

field5 type c value 'F',

end of g_s_vbak_vbap,

char1 type c.

start-of-selection.

do varying char1 from g_s_vbak_vbap-field1 next g_s_vbak_vbap-field3

range g_s_vbak_vbap.

write char1.

enddo.

Regards

Anil Kumar K

7 REPLIES 7

Former Member
0 Kudos
87

You have to EXIT the DO after all fields have been processed.

From the help on 'DO':

Example

DATA: BEGIN OF WORD,

ONE VALUE 'E',

TWO VALUE 'x',

THREE VALUE 'a',

FOUR VALUE 'm',

FIVE VALUE 'p',

SIX VALUE 'l',

SEVEN VALUE 'e',

EIGHT VALUE '!',

END OF WORD,

LETTER1, LETTER2.

DO VARYING LETTER1 FROM WORD-ONE NEXT WORD-THREE

VARYING LETTER2 FROM WORD-TWO NEXT WORD-FOUR.

WRITE: LETTER1, LETTER2.

IF LETTER2 = '!'.

EXIT.

ENDIF.

ENDDO.

The resulting output is the character string

"E x a m p l e !".

Note

When using this addition, ensure that the DO loop terminates at the "right" time, in order to avoid assigning meaningless values that happen to be in memory after this sequence of fields. This could result in a runtime error.

Rob

Message was edited by:

Rob Burbank

0 Kudos
87

Like Rob said, you have to exit from the do loop.. RANGE addition ensures that the system accesses data within that range.. but you've to explicitly provide the exit from the loop.. even with the RANGE addition..

report  zpdo                                    .
data : begin of g_s_vbak_vbap,
field1 type c value 'A',
field2 type c value 'B',
field3 type c value 'C',
field4 type c value 'D',
field5 type c value 'F',
end of g_s_vbak_vbap,
char1 type c.

start-of-selection.

  do varying char1 from g_s_vbak_vbap-field1 next g_s_vbak_vbap-field3
  range g_s_vbak_vbap.
    write: / char1.
    if char1 = 'F'.
      exit.
    endif.
  enddo.

~Suresh

0 Kudos
87

Well, I don't really like the help example. It's dependent on the values in the structure, which you normally don't know. A more flexible solution is:


DATA : BEGIN OF g_s_vbak_vbap,
field1 TYPE c VALUE 'A',
field2 TYPE c VALUE 'B',
field3 TYPE c VALUE 'C',
field4 TYPE c VALUE 'D',
field5 TYPE c VALUE 'F',
END OF g_s_vbak_vbap,
char1 TYPE c.

START-OF-SELECTION.

  DO VARYING char1 FROM g_s_vbak_vbap-field1 NEXT g_s_vbak_vbap-field3
    RANGE g_s_vbak_vbap.
    WRITE char1.
    IF sy-index = 3.
      EXIT.
    ENDIF.
  ENDDO.

Rob

Former Member
0 Kudos
87

Hi Anil Kumar K,

Maybe you should check if the value of char1 is already equal to 'F'. This is probably the reason of short dump. Accdg to ABAP Keyword documentation:

... VARYING f FROM f1 NEXT f2 RANGE f3

This <u>addition has a similar effect to ...VARYING f FROM f1 NEXT f2. It also ensures that the system only accesses data within the field f3.</u>

Message was edited by:

p328202

0 Kudos
87

Hi Rah,

Then what is the excat use of range keyword. Please kindly let me know.

Regards

Anil Kumar K

0 Kudos
87

Hi Anil Kumar K,

Accdg to ABAP keyword documentation:

To avoid overwriting memory contents, a RANGE for valid accesses is implicitly or explicitly implemented for these statements

If the RANGE f3 addition is specified, a syntax or runtime error is triggered, should f1 or f2 not be included in f3. For f3, only structures and elementary fields of the types C, N, or X are permitted.

If the RANGE addition is not specified, it is implicitly defined with FROM f1 NEXT f2 as follows:

1.) If the syntax check recognizes that both f1 and f2 are components of the same structure, the valid RANGE area is defined from the smallest structure containing f1 and f2.

2.) There is a syntax error if the syntax check recognizes that f1 and f2 are not part of the same structure.

3.) A valid range must be defined explicitly using RANGE if the syntax check does no recognize that f1 and f2 are not together.

If a deep structure is defined as a RANGE addition, the system checks for every loop pass that there are no field references, object references, tables, or strings within the accessed range.

Message was edited by:

p328202

0 Kudos
87

The addition RANGE ensures that the data accessed during the loop execution is within that range.. in the above example, if we repalce the exit condition with say,

if char 1 = 'Z'.

exit.

endif.

it will give a short-dump with the DO_WHILE_VARY_NOT_IN_RANGE

runtime error at the 4th loop pass.. ie the system enforces the RANGE during runtime.

hope this is clear..

~Suresh