GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
foreach(1) FreeBSD General Commands Manual foreach(1)

foreach - foreach loop in Ch

foreach (token; expr1; expr2; expr3) statement

foreach (token; expr1; expr2) statement

foreach (token; expr1) statement

The 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.

The following code

char *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 }

$CHHOME/demos/lang/foreach_1
An example for using foreach

$CHHOME/docs/chguide.pdf

Harry H. Cheng

Januray 2003 foreach

Search for    or go to Top of page |  Section 1 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.