The so-called assignment statement, small series in the earliest learning C language to know that is "a=1;" Over the years, although the language is changing, but the assignment statement basically has not changed. But today, while looking at a string of PowerShell code, this idea was overturned. Small series see a multivariate variable assignment of the statement.
Small series to see the statement is like this:
Copy Code code as follows:
$a [0], $a [1]= $a [0], $a [1];
Let's guess what it means.
In fact, a good guess, the first look is like the $a[0 and $a[1] These two array elements upside down. And the effect is actually the same. Is this the two-variable interchange value statement? We know that in the traditional programming language, we need to exchange the value of two variables, at least three statements, and also need an intermediate variable. For example, to exchange the value of a and B, the code should write this:
Copy Code code as follows:
The c here is the new addition of a variable. In this way, PowerShell is really a hell of a powerful ah!
Small knitting also whimsical to do some attempts, found that the original grammar is not only reversed two values so simple. Like what:
Copy Code code as follows:
Ps> $a =1,2,3
Ps> $a [0], $a [1], $a [2]= $a [1], $a [2], $a [0];
Ps>-join $a
231
See no, turn the value of the array element around.
The small series also did such a test:
Copy Code code as follows:
Ps> $a, $b =1,2
Ps> $a
1
Ps> $b
2
The equivalent is to assign values in order, before and after the equals sign. Very well understood! And look at this:
Copy Code code as follows:
PS c:\users\hong> $a, $b =1,2,3
PS c:\users\hong> $a
1
PS c:\users\hong> $b
2
3
Haha, this is interesting. The front is two variables followed by three values. What will the PowerShell do with them? According to the results, the $a was assigned a value of 1, $b was treated as an array, and assigned values of 2 and 3.
So the small series called this syntax "multivariate simultaneous assignment", which is to assign a number of variables at the same time, of course, there are some details of the assignment, has been shown in the example above.
Finally, someone must ask, if the number of variables more than the number of values, what will be the result? Oh, look at:
Copy Code code as follows:
PS c:\users\hong> $a, $b, $c, $d =1,2,3
PS c:\users\hong> $d
PS c:\users\hong>