|
NAMEforeach - foreach loop in ChSYNTAXforeach (token; expr1; expr2; expr3) statementforeach (token; expr1; expr2) statement foreach (token; expr1) statement DESCRIPTIONThe foreach loop is used to handle iterations based on the condition of string type or pointer to char. The expressions expr1, expr2, and expr3 shall have string type or pointer to char. The identifier token also shall have string type or pointer to char. In each iteration, the variable token takes a token from the original expression expr1 separated by the delimiter expr3. The loop body is executed repeatedly until token is a NULL pointer or the same as expr2. This is achieved by comparing the controlling expression (token==NULL || expr2!=NULL && !strcmp(token,expr2)) to 0. The omitted expr2 and expr3 are replaced by NULL and " ;", respectively.EXAMPLESThe following codechar *token,*str="ab:12 cd ef",*cond="cd",*delimit=" :"; foreach(token; str; cond; delimit) printf("token= %s\n", token); printf("after foreach token = %s\n", token); printf("after foreach cond = %s\n", cond); printf("after foreach delimi= %s\n", delimit); gives the output of token= ab token= 12 after foreach token = cd after foreach cond = cd after foreach delimi= : In this example, the delimiters for token are characters of blank space and colon as shown in the value for the variable delimit in the program. The code below will create three directories dir1, dir2, and dir3 in the current directory. string_t token, str="dir1 dir2 dir3"; foreach(token; str) { mkdir $token } FILES
SEE ALSO$CHHOME/docs/chguide.pdfAUTHORHarry H. Cheng
Visit the GSP FreeBSD Man Page Interface. |