简体   繁体   English

WHILE 循环中的 sy-index

[英]sy-index within WHILE loop

I wrote a program to count the number of each vowel in a string character.我编写了一个程序来计算字符串字符中每个元音的数量。

Here is the code:这是代码:

    DATA: v_nom(10)  TYPE c VALUE 'christian',
          vowels     TYPE string,
          lenght     TYPE i,
          char       TYPE c,
          occurences TYPE i,
          index      TYPE i.
    
    vowels ='aeiouy'.
    lenght =  strlen( vowels ).
    
    index = sy-index.
    
    WRITE index.
    
    WHILE index < lenght.
    
      char = vowels+index.
    
      FIND ALL OCCURRENCES OF char IN v_nom MATCH COUNT occurences.
    
    
      ADD 1 TO index.
    
      IF occurences = 0.
    
      ELSE.
    
     write:/ char, 'appears',/ occurences, 'times'.
    
      ENDIF.
    ENDWHILE.

It works fine... but at first I try do it by using directly sy-index without assigning it to a variable, an the problem when I run the debug I saw that sy-index starts at the position 1, not 0.它工作正常......但起初我尝试直接使用sy-index而不将其分配给变量,当我运行调试时出现问题,我看到sy-index从 position 1 开始,而不是 0。

Using a variable or using directly the system variable should work the same?使用变量或直接使用系统变量应该一样吗?

Here is the code:这是代码:

DATA: v_nom(10)  TYPE c VALUE 'christian',
      vowels     TYPE string,
      lenght     TYPE i,
      char       TYPE c,
      occurences TYPE i.

vowels ='aeiouy'.
lenght =  strlen( vowels ).


WHILE sy-index < lenght.

  char = vowels+sy-index.

  FIND ALL OCCURRENCES OF char IN v_nom MATCH COUNT occurences.
  ADD 1 TO index.

  IF occurences = 0.

  ELSE.

 write:/ char, 'appears',/ occurences, 'times'.

  ENDIF.
ENDWHILE.

Sy-index starts at pos 1 not 0, by the letter 'e' not with the first one which is 'a'.... Sy-index从 pos 1 而不是 0 开始,由字母 'e' 开始,而不是第一个是 'a'....

Can someone tell me where I make a mistake....谁能告诉我哪里出错了......

Regards.问候。

Name姓名 Type类型 Length长度 Content内容
sy-index sy索引 i一世 - - Loop index.循环索引。 In DO and WHILE loops, contains the number of previous loop passes, including the current pass.在 DO 和 WHILE 循环中,包含先前循环遍数,包括当前遍数。

Within the statement block, the system field sy-index contains the number of previous loop passes, including the current pass.在语句块中,系统字段 sy-index 包含先前循环遍数,包括当前遍数。

Documentation . 文档

Now in your example you assign sy-index before you reach the loop.现在在您的示例中,您在到达循环之前分配sy-index Hence it will have it's initial value, which is 0 for type i .因此它将具有它的初始值,即类型i0
If you use it within the loop, the first iteration will have 1 (number of previous loop passes, including the current pass).如果在循环中使用它,第一次迭代将有1 (先前循环通过的次数,包括当前通过)。
And yes, ABAP is a 1-based language.是的,ABAP 是一种基于 1 的语言。 The first line of an internal table is also lt_sflight[ 1 ] , not lt_sflight[ 0 ] .内部表的第一行也是lt_sflight[ 1 ] ,而不是lt_sflight[ 0 ]

Note that this can be solved way easier with count_any_of :请注意,使用count_any_of可以更轻松地解决此问题:

DATA(occurences) = count_any_of( 
  val = v_nom
  sub = 'aeiouy'
).

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM