The addition operation in PowerShell _powershell

Source: Internet
Author: User
Tags numeric

We have a certain perceptual understanding of the operators in the PowerShell. Let's learn the "+" operator first. PowerShell defines the specific behavior of the "+" operator to handle numeric types, string types, array types, and hash table types. In the numerical calculation, the "+" and the usual numerical calculation rules are basically the same. Note, however, that the storage space used is different because the computer represents different numeric types, so these types can represent only one range of data. In the process of numerical operation, if the result of the calculation exceeds the value range of the numerical type, PowerShell will automatically convert the type and save the result with a larger data type. For example:

PS c:\> 9 +
PS c:\> (9 + 11). GetType (). FullName
System.Int32
PS c:\> [int]::maxvalue + 1
2147483648
PS c:\> ([Int]::maxvalue + 1]. GetType (). FullName
system.double
PS c:\> [int]::minvalue-1
-2147483649
PS c:\> ([int]::minvalue-1). GetType (). FullName
System.Double

In the example, [Int]::maxvalue represents the maximum value of type int, and if the "+ 1" operation is followed, the value is exceeded to a range of type int, so PowerShell automatically converts the data type to a double type. Similarly, [int]::minvalue represents the minimum value for the type int.

When the "+" operator is used on the string type operand, PowerShell will concatenate the string and return a new string, which is consistent in the C # language and in the T-SQL syntax of the database. For example:

PS c:\> "Hello" + "" + "world!"
Hello world!

When the "+" operator is used for an array type operand, a set concatenation operation is performed, and a new array is returned, in accordance with the behavior in the C # language. For example:

PS c:\> $array 1 = 1,2,3
ps c:\> $array 2 = 4,5
ps c:\> $array 3 = $array 1 + $array 2
PS c:\> "$array 3 "
1 2 3 4 5

The example first defines the array array1, which contains 1, 2, and 3 of these three elements. The array array1 is then added to the array array2 and the returned result is stored in the variable ARRAY3. Finally, the elements in the output array go to the console. In PowerShell, when an array is referenced in double quotes, each element of the array is concatenated with the strings stored in the variable $ofs and output. (The default value for "$ofs" is a space character).

When using the "+" operator for a hash table type operand, PowerShell adds a hash table key value pair to the right of "+" to the left Hashtable. If the added key value already exists in the left hash table, PowerShell will give an error message. For example:

PS c:\> $dev = @{Tom = 1; Jerry = 2}
PS c:\> $sale = @{Hero = 1;}
PS c:\> $dev + $sale
Name              Value
----              -----
Tom              1
Jerry             2
Hero              1

The beginning of the example defines the hash table dev, which contains the staff of the Research and Development department Tom and Jerry. Next, we define a hash table sale, the employee only hero one person. Add Dev and sale and get a new hash table containing the three employees. If the right hash table contains the same key-value pairs as the left Hashtable, PowerShell will give an error message:

PS c:\> $manager = @{Tom = 3}
ps c:\> $dev + $manager bad
argument to operator ' + ': Item added. Keywords in the dictionary: "Tom" added the keyword: "Tom".
At line:1 char:7
+ $dev + <<<< $manager

In the previous example, all objects of the same type are added together. You may be thinking, if the "+" is not on both sides of the same type of objects, what will be the result? Here are some common scenarios:

PS c:\> 123 + '
146
PS c:\> 123 + ' 0xa '
PS c:\> 4 + ' test '
cannot convert value ' tes T "to type" System.Int32 ". Error: "Input string is not in a correct format."
At Line:1 Char:4
+ 4 + <<<< ' test '
PS c:\> ' +
0273

PowerShell when the "+" operator is found, the calculated rule will be determined based on the type of left operand. In the first expression 123 + ' 23 ', 123 is an integer, so "+" performs numeric addition operations. However, because the right-hand operand is a string type, PowerShell first converts the operand to a type. So finally, we see the value of the result as a numeric type: 146. In the second example, the string contains the hexadecimal representation of an integer that PowerShell the value to the numeric type and computes it correctly. In the third example, the string ' test ' cannot be converted to an integer type, so PowerShell gives the error message: "The input string is not in the correct format." In the fourth example, the left-hand operand becomes a string containing numbers, so PowerShell performs a concatenation of strings.

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.