The applicable rules are as follows:
, is a unary or binary operator that creates an array of operands (s) and has a higher priority than the + operator.- The
+ operator can be used for numerical addition, string concatenation, and array concatenation. The operand data type on the left determines what type of operation is performed. If possible, the right operand is given as the data type of the left operand; if this is not possible, an error occurs. * - If the priority is equal, the evaluation is performed from left to right.
- Arrays are displayed as strings, listing items separated by spaces.
- Parentheses, as you would expect, have a higher priority than any operator, and cause a closed expression before anything outside the parentheses.
- The default output for an array object is a list of elements, one in each row.
* This explains the seemingly inconsistent results, such as "foo" + 1 evaluating to foo1 , but 1 + "foo" gives you an error.
So, let's take a look at your test cases and analyze what happens after the logic described above.
Case I:
"ab", "cd"
It is very simple: the operator creates an array of two lines, ab and bc .
Case II:
"a" + "b", "c" + "d"
At first glance, this seems counterintuitive; the key must be understood that , evaluated to + . Here, as a result, step by step:
"b", "c" first evaluated (due to priority over + ), creating an array of elements b and c .- Next, the
+ operator is applied to "a" and the array created in step 1. Since the left operand of "a" is a string, the string is concatenated. - To combine a string with an array, the array is created as a string. The array of
b and c becomes the string bc . - Concatenating
a with bc gives ab c . - Finally,
ab c combines with d to get the final result, ab cd
Case III:
"a"+"b" , ("c"+"d")
This gives the same result by coincidence; assessment takes place in different ways:
("c"+"d") is evaluated first because of the brackets. This is a simple concatenation of the vanilla string, which results in the string cd .- Next, the operator
, applied to "b" and the line from step 1, creating an array of b and cd elements. (This evaluates to "a" + "b" because , has a higher priority than + .) - Then the
+ operator is applied to "a" and the array created in step 2. Since the left operand is a string, the string is concatenated. - Array
b and cd displayed as string b cd . - Lines
a and b cd are combined, which leads to the final output of ab cd .
Case IV:
("a" + "b"), "c" + "d"
This is the most interesting test case and probably the most controversial as it gives results that may seem contradictory. Here's how it was evaluated:
- As in the case of step 1 of step III, it is first evaluated
("a" + "b") , creating the string ab . , applied to the line from step 1 and "c" , creating an array of elements ab and c .- The
+ operator is applied to the array from step 2 and the next token, "d" . This time the left operand is an array, so the array is concatenated. - The string operand
"d" is executed as an array of one element. - Two arrays are combined into an array of elements
ab , c and d .
Case V:
("a" + "b"), ("c" + "d")
It should now be easy to see what is happening here:
("a" + "b") evaluates to string ab .("c" + "d") evaluates to the string cd ., applies to the lines from steps 1 and 2, creating an array of ab and cd .
You can see the priority table with the command
Get-Help about_Operator_Precedence
(the names of reference documents can be completed on a tab, so help about_op TAB TAB to save the input)
Adi inbar
source share