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

do <n> varying ...

prabhu_s2
Active Contributor
0 Likes
842

hi

can anyone help me in understanding the execution of

DO 4 TIMES VARYING <W_area>

FROM <W_1> NEXT <W_2>.

with example.....

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
738

Hi,

U have to use

This will give u the correct answer I have done so.

Eg.

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.

More sample
* Define work area
DATA: BEGIN OF VACATION,
              UAR LIKE P0005-UAR01, "Leave type
              UAN LIKE P0005-UAN01, "Leave entitlement
              UBE LIKE P0005-UBE01, "Start date
              UEN LIKE P0005-UEN01, "End date
              UAB LIKE P0005-UAB01, "Leave accounted
           END OF VACATION.

GET PERNR.
     RP-PROVIDE-FROM-LAST P0005 SPACE PN/BEGDA PN/ENDDA.
     DO 6 TIMES VARYING VACATION
             FROM P0005-UAR01 "Starting point
                 NEXT P0005-UAR02. "Increment
             If p0005-xyz then ... endif. 
      ENDDO.

data: begin of alpha,      
       c1 value 'A',       
      c2 value 'B',         
    c3 value 'C',           
  c4 value 'D',          
   c5 value 'E',            
 c6 value 'F',           
 end of alpha.    
  Data wa(1) type c value 'X'. 
Do 3 times varying wa from alpha-c1 
next alpha-c3. 
Write wa no-gap. 
Enddo.

<b>What is the output of the above code?

ACF

ACC

ABC

ACA

ACE</b>

3 REPLIES 3
Read only

Former Member
0 Likes
739

Hi,

U have to use

This will give u the correct answer I have done so.

Eg.

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.

More sample
* Define work area
DATA: BEGIN OF VACATION,
              UAR LIKE P0005-UAR01, "Leave type
              UAN LIKE P0005-UAN01, "Leave entitlement
              UBE LIKE P0005-UBE01, "Start date
              UEN LIKE P0005-UEN01, "End date
              UAB LIKE P0005-UAB01, "Leave accounted
           END OF VACATION.

GET PERNR.
     RP-PROVIDE-FROM-LAST P0005 SPACE PN/BEGDA PN/ENDDA.
     DO 6 TIMES VARYING VACATION
             FROM P0005-UAR01 "Starting point
                 NEXT P0005-UAR02. "Increment
             If p0005-xyz then ... endif. 
      ENDDO.

data: begin of alpha,      
       c1 value 'A',       
      c2 value 'B',         
    c3 value 'C',           
  c4 value 'D',          
   c5 value 'E',            
 c6 value 'F',           
 end of alpha.    
  Data wa(1) type c value 'X'. 
Do 3 times varying wa from alpha-c1 
next alpha-c3. 
Write wa no-gap. 
Enddo.

<b>What is the output of the above code?

ACF

ACC

ABC

ACA

ACE</b>

Read only

Former Member
0 Likes
738

Hello Prabhu,

... VARYING f FROM f1 NEXT f2

Effect

This addition is useful if you have a series of fields of the same type and the same distance from each other.

f is a variable which you define in a DATA statement. On each loop pass, f contains a new value. The field f1 after "FROM" specifies the first value of the variable f, while the field f2 after "NEXT" specifies the value to be assigned to the variable f in the second pass. For each subsequent pass, the variable f contains the next value in the sequence determined by the distance between the fields f, f1 and f2 in memory.

The fields f1 and f2 should be type-compatible and convertible to f.

If the value of f changes during the loop pass, the new value is then placed in the appropriate field fn assigned to f (transfer type: pass by value and result). If the loop pass terminates because of a dialog message, the new value is not passed back if f changes.

The addition ... VARYING f FROM f1 NEXT f2 can be used several times in a DO statement.

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.

Vasanth

Read only

prabhu_s2
Active Contributor
0 Likes
738

wonderful explanations and example.......thanks guys