I. Condition statements:There are several types
1. If (condition expression) {statement block}
2. Single Perl statement if (conditional expression );
3. If (condition expression) {statement Block 1}
Else {statement Block 2}
4. If (condition expression 1) {statement Block 1}
Elsif (condition expression 2) {statement Block 2}
Elsif (condition expression 3) {statement block 3}
...................
Elsif (condition expression N-1) {statement block N-1}
Else {statement block n}
Ii. Loop statement:There are several types
1. While loop:
While (true value conditional expression) {# loop body}
2. Until loop:
Until (dummy condition expression) {# loop body}
3. Do statement: usually used together with a while loop or until loop. The do statement must at least execute the loop body once;
Do
{
# Loop body;
} While (true value conditional expression );
Or
Do
{
# Loop body;
} Until (dummy Value Condition expression );
These two types of loops have the following features: Execute the loop body each time, and then judge the conditional expression;
Note that do statement blocks cannot use labels. Do-until structures cannot use next, redo, or last statements;
4. For Loop:
Standard for statement:
For (expression 1; expression 2; expression 3) {# loop body}
This loop can be used in two scenarios:
A: When the number of cycles is determined;
B: The number of cycles is uncertain, but the condition for loop end is clearly given;
The for statement has the following simple application form:
For (initial values of cyclic variables; cyclic conditions; modifying values of cyclic variables) {# cyclic body}
5. foreach loop:
The meaning of this type of loop is actually the same as that of the for statement. The for statement can do the same thing that foreach can do. Generally, the for statement and foreach statement are distinguished as follows:
A: For loops are mostly used in environments where the number of loops is determined;
B: foreach loops are mostly used to process arrays and hash variables;
The foreach statement uses the built-in list attribute provided by Perl to repeat arrays or lists. The foreach statement processes arrays, lists, or hash variables as follows:
Foreach scalar (list) {# loop body}
The foreach loop has a built-in list/array element access operator that can access each element in an array, list, or hash variable, scalar is assigned to the next element in the list, and the loop body is executed. Each loop only processes one element in the list. After processing all elements, the list returns a null value, which is false, exit the loop;
Foreach loop can omit the list variable scalar (loop control variable). The format of foreach loop is as follows:
Foreach (list) {# loop body}
If the list variable scalar is omitted, the perl interpreter assigns each element in the list to the default special variable "$ _" one by one _", in this case, the special variable "$ _" is regarded as the list variable of the foreach loop, which has the same effect as the list variable scalar;
The scope of list variables is limited to only those in the foreach loop body. It does not affect the same name variables in vitro. This is different from the loop control variables in the for loop;
Use foreach to process arrays cyclically:
Foreach $ variable (@ array) {# loop body}
When the foreach loop is used to process an array, if the array loop variable $ variable is changed, the value of the corresponding element in the array is changed, which is not the same as when the list is processed using the foreach loop;
Use foreach to process hash variables cyclically:
Hash variables use any type of keywords as the array of subscript indexes, and the indexes of hash variables are not stored in order or read in order, therefore, when processing hash variables using foreach loops, the keys () and values () functions must be used together ():
Foreach $ index (Keys (% hash) {# loop body}
Or
Foreach $ index (sort (Keys (% hash) {# loop body}
Foreach $ value (values (% hase) {# loop body}
Special built-in hash variables provided by Perl for storing the current environment variables of the system: % env