Example of defining multiple variables and assigning values in PowerShell _powershell

Source: Internet
Author: User

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:
C=a;a=b;b=c;

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.