|
|
|
|
|
|
|
|
|
|
|
File Iterative Control
|
|
|
In e the way we open and read ASCII files is slightly different then C and C++. There are two types of looping constructs that are provided to help with handling ASCII files.
|
|
|
|
|
|
- for each line in file
- for each file matching
|
|
|
|
|
|
for each line in file
|
|
|
Executes the action block for each line in the text file file-name. Inside the block, it (or optional name) refers to the current line (as string) without the final "\n" (the final new line character, CR).
|
|
|
|
|
|
Syntax :
|
|
|
|
|
|
for each [line] [(name)] in file file-name-exp [do] {action; ...}
|
|
|
|
|
|
Example - for each line in file
|
|
|
|
|
|
Lets consider a ASCII file file.txt containing below lines
|
|
|
|
|
|
This is first line
This is second line
This is third line
|
|
|
|
|
|
Lets write a e program to read this file and print it
|
|
|
|
|
|
1 <'
2 extend sys {
3 run() is also {
4 for each line in file "file.txt" {
5 out (it);
6 };
7 };
8 };
9 '>
You could download file flow_control9.e
here
|
|
|
|
|
|
Simulator Output
|
|
|
|
|
|
This is first line
This is second line
This is third line
|
|
|
|
|
|
|
|
|
for each file matching
|
|
|
For each file (in the file search path) whose name matches file-name-exp execute the action block. Inside the block, it (or optional name) refers to the matching file name.
|
|
|
|
|
|
Syntax
|
|
|
|
|
|
for each file [(name)] matching file-name-exp [do] {action; ...}
|
|
|
|
|
|
|
|
|
|
|
|
Example - for each file matching
|
|
|
|
|
|
1 <'
2 extend sys {
3 run() is also {
4 for each file (file_name) matching "f*.txt" {
5 outf("Found file %s\n",file_name);
6 for each line in file file_name {
7 out (it);
8 };
9 };
10 };
11 };
12 '>
You could download file flow_control10.e
here
|
|
|
|
|
|
Simulator Output
|
|
|
|
|
|
Found file file.txt
This is first line
This is second line
This is third line
|
|
|
|
|
|
Controlling the Program Flow
|
|
|
The actions described in this section are used to alter the flow of the program in places where the flow would otherwise continue differently. The e language provides the following actions for controlling the program flow:
|
|
|
|
|
|
|
|
|
|
|
|
break
|
|
|
Breaks the execution of the nearest enclosing iterative action (for or while). When a break action is encountered within a loop, the execution of actions within the loop is terminated, and the next action to be executed is the first one following the loop.
|
|
|
|
|
|
Syntax
|
|
|
|
|
|
break
|
|
|
|
|
|
continue
|
|
|
Stops the execution of the nearest enclosing iteration of a for or a while loop, and continues with the next iteration of the same loop. When a continue action is encountered within a loop, the current iteration of the loop is aborted, and execution continues with the next iteration of the same loop.
|
|
|
|
|
|
You cannot place continue actions outside the scope of a loop
|
|
|
|
|
|
Syntax
|
|
|
|
|
|
continue
|
|
|
|
|
|
Example - break and continue
|
|
|
|
|
|
1 <'
2 extend sys {
3 run() is also {
4 var i : int = 0;
5 var j : int = 0;
6 for {i=0; i < 15; i = i + 1} do {
7 outf("Current value of i : %d\n",i);
8 outf("Current value of j : %d\n",j);
9 if ( i < 3) {
10 continue;
11 } else {
12 j = j + 1;
13 };
14 if (j > 4) {
15 break;
16 };
17 };
18 };
19 };
20 '>
You could download file flow_control8.e
here
|
|
|
|
|
|
Simulator Output
|
|
|
|
|
|
Current value of i : 0
Current value of j : 0
Current value of i : 1
Current value of j : 0
Current value of i : 2
Current value of j : 0
Current value of i : 3
Current value of j : 0
Current value of i : 4
Current value of j : 1
Current value of i : 5
Current value of j : 2
Current value of i : 6
Current value of j : 3
Current value of i : 7
Current value of j : 4
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|