Why is a leading comma required when creating an array?
I want to create an array containing arrays of two numbers. Pretty straightforward. However, If I do not provide a leading comma before the first array, it is incorrect. Why is this leading comma required?
PS C:srcpowershell> Get-Content .fr-btest.ps1
$files1 = @(
@(4, 1024)
, @((7), (16))
)
$files1
$files1.GetType()
$files1.Length
$files1.Count
'========'
$files2 = @(
, @(4, 1024)
, @((7), (16))
)
$files2
$files2.GetType()
$files2.Length
$files2.Count
PS C:srcpowershell> .fr-btest.ps1
4
1024
7
16
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object System.Array
3
3
========
4
1024
7
16
True True Object System.Array
2
2
arrays powershell
add a comment |
I want to create an array containing arrays of two numbers. Pretty straightforward. However, If I do not provide a leading comma before the first array, it is incorrect. Why is this leading comma required?
PS C:srcpowershell> Get-Content .fr-btest.ps1
$files1 = @(
@(4, 1024)
, @((7), (16))
)
$files1
$files1.GetType()
$files1.Length
$files1.Count
'========'
$files2 = @(
, @(4, 1024)
, @((7), (16))
)
$files2
$files2.GetType()
$files2.Length
$files2.Count
PS C:srcpowershell> .fr-btest.ps1
4
1024
7
16
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object System.Array
3
3
========
4
1024
7
16
True True Object System.Array
2
2
arrays powershell
add a comment |
I want to create an array containing arrays of two numbers. Pretty straightforward. However, If I do not provide a leading comma before the first array, it is incorrect. Why is this leading comma required?
PS C:srcpowershell> Get-Content .fr-btest.ps1
$files1 = @(
@(4, 1024)
, @((7), (16))
)
$files1
$files1.GetType()
$files1.Length
$files1.Count
'========'
$files2 = @(
, @(4, 1024)
, @((7), (16))
)
$files2
$files2.GetType()
$files2.Length
$files2.Count
PS C:srcpowershell> .fr-btest.ps1
4
1024
7
16
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object System.Array
3
3
========
4
1024
7
16
True True Object System.Array
2
2
arrays powershell
I want to create an array containing arrays of two numbers. Pretty straightforward. However, If I do not provide a leading comma before the first array, it is incorrect. Why is this leading comma required?
PS C:srcpowershell> Get-Content .fr-btest.ps1
$files1 = @(
@(4, 1024)
, @((7), (16))
)
$files1
$files1.GetType()
$files1.Length
$files1.Count
'========'
$files2 = @(
, @(4, 1024)
, @((7), (16))
)
$files2
$files2.GetType()
$files2.Length
$files2.Count
PS C:srcpowershell> .fr-btest.ps1
4
1024
7
16
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object System.Array
3
3
========
4
1024
7
16
True True Object System.Array
2
2
arrays powershell
arrays powershell
asked Mar 13 '17 at 19:40
litlit
5,50233051
5,50233051
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
@() is the array subexpression operator, which works differently than array construction operators you may be used to from other languages. The operator evaluates the nested subexpression and returns the output of that expression as an array. Meaning you can do something like this:
@(
Write-Output 'foo'
Get-Content 'C:somefile.txt'
Test-Connection '192.168.23.42' -Count 1
)
and have an array come out.
For your first example this means that the two statements @(4, 1024) and , @((7), (16)) are evaluated individually, and the collective output of the two statements is then returned as an array.
The first statement (@(4, 1024)) outputs two integers, but the second statement (, @((7), (16))) outputs an array of two integers. That is because the leading comma in that statement is interpreted as the unary array construction operator (or comma operator), so you get an array nested in another array, and only the outer array is unrolled during output.
Essentially, your expression is the same as
$files1 = @(
4
1024
, @(7, 16)
)
or
$files1 = 4, 1024, @(7, 16)
Your second example avoids this pitfall, because both nested arrays are prepended with the unary array construction operator and thus protected from being completely unrolled.
With that said, I would recommend to define arrays in a more clear-cut way, e.g. like this:
$files1 = @(4, 1024),
@(7, 16)
or (using grouping expressions instead of array subexpressions) like this:
$files1 = (4, 1024),
(7, 16)
to avoid surprises like the one you observed. The outer @() isn't necessary for defining an array here. PowerShell automatically detects that via the trailing comma at the end of the first line.
For further information see about_Operators.
1
Good answer, but I suggest not (also) calling@(...)an array construction operator; if you want to call it by another name, call it array guarantor operator; that is, loosely speaking, if something isn't already an array, make it one. To drive the point home that@(...)is never needed to create array literals, I suggest removing the@from the final code snippet:$files1 = (4, 1024), (7, 16).
– mklement0
Dec 22 '18 at 22:27
add a comment |
Powershell uses both a comma and a line break as an array separator. Your first declare:
$files1 = @(
@(4, 1024)
, @((7), (16))
)
Creates the following:
$files1[0] = 4
$files1[1] = 1024
$files1[2] = @(7,16)
Your second declare
$files1 = @(
, @(4, 1024)
, @((7), (16))
)
Creates the following:
$files1[0] = @(4, 1024)
$files1[1] = @(7, 16)
As to the parsing decision, it is dependent on the first non-white space character encountered on a line:
Array Literals In PowerShell
and
Understanding PowerShell Parsing Modes
PowerShell uses both a comma and a line break as an array separator is not quite correct: PowerShell has no array separator;,is the array construction operator. Inside array-subexpression operator@(...)- the content of which is not an array - it is statements that you can separate with line breaks (in lieu of;).
– mklement0
Dec 22 '18 at 22:37
add a comment |
The key to understanding the Array subexpression operator @( ) is the realization that you don't need it to create arrays, instead arrays are created with the Comma operator ,
As a binary operator, the comma creates an array. As a unary operator,
the comma creates an array with one member. Place the comma before the member.
$myArray = 1,2,3
$SingleArray = ,1
$xs = (1,2,3), (4,5,6) # Count: 2
$ys = (1,2,3),
(4,5,6) # Count: 2
Now consider
# A - two expressions, each expression yields one array of size 3
(1,2,3)
(4,5,6)
# B - one expression resulting in an array of two elements
(1,2,3),
(4,5,6)
# C - similar to A except the sizes are 3 and 1
# (the second array contains a single element)
(1,2,3)
,(4,5,6)
And the final step is to realize that
in essence, the
@(...)operation is syntactic sugar for
[array] $(...)
as explained by the PowerShell Team Blog (The link was given by Christopher G. Lewis answer). Although the meaning and limitations of in essence is not entirely clear to me.
@(...)is just a syntactic sugar for[array] $(...)That is plain wrong. Case 1:$a = @($null); $b = [array] $($null); ConvertTo-Json $a; ConvertTo-Json $b. Case 2:$a = @(,[array]0); $b = [array] $(,[array]0); ConvertTo-Json $a; ConvertTo-Json $b.
– PetSerAl
Dec 22 '18 at 22:39
You seem to know more about this, would you mind editing the answer and fixing it? Especially since the claim is made in the PowerShell Team Blog.
– Micha Wiedenmann
Jan 3 at 13:47
Well, except that last claim, answer is correct. You absolutely do not need@(…), when you use,as array construction operator.
– PetSerAl
Jan 4 at 12:30
What is your opinion on the explanation given by the PowerShell Team Blog?
– Micha Wiedenmann
Jan 4 at 12:34
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42772083%2fwhy-is-a-leading-comma-required-when-creating-an-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
@() is the array subexpression operator, which works differently than array construction operators you may be used to from other languages. The operator evaluates the nested subexpression and returns the output of that expression as an array. Meaning you can do something like this:
@(
Write-Output 'foo'
Get-Content 'C:somefile.txt'
Test-Connection '192.168.23.42' -Count 1
)
and have an array come out.
For your first example this means that the two statements @(4, 1024) and , @((7), (16)) are evaluated individually, and the collective output of the two statements is then returned as an array.
The first statement (@(4, 1024)) outputs two integers, but the second statement (, @((7), (16))) outputs an array of two integers. That is because the leading comma in that statement is interpreted as the unary array construction operator (or comma operator), so you get an array nested in another array, and only the outer array is unrolled during output.
Essentially, your expression is the same as
$files1 = @(
4
1024
, @(7, 16)
)
or
$files1 = 4, 1024, @(7, 16)
Your second example avoids this pitfall, because both nested arrays are prepended with the unary array construction operator and thus protected from being completely unrolled.
With that said, I would recommend to define arrays in a more clear-cut way, e.g. like this:
$files1 = @(4, 1024),
@(7, 16)
or (using grouping expressions instead of array subexpressions) like this:
$files1 = (4, 1024),
(7, 16)
to avoid surprises like the one you observed. The outer @() isn't necessary for defining an array here. PowerShell automatically detects that via the trailing comma at the end of the first line.
For further information see about_Operators.
1
Good answer, but I suggest not (also) calling@(...)an array construction operator; if you want to call it by another name, call it array guarantor operator; that is, loosely speaking, if something isn't already an array, make it one. To drive the point home that@(...)is never needed to create array literals, I suggest removing the@from the final code snippet:$files1 = (4, 1024), (7, 16).
– mklement0
Dec 22 '18 at 22:27
add a comment |
@() is the array subexpression operator, which works differently than array construction operators you may be used to from other languages. The operator evaluates the nested subexpression and returns the output of that expression as an array. Meaning you can do something like this:
@(
Write-Output 'foo'
Get-Content 'C:somefile.txt'
Test-Connection '192.168.23.42' -Count 1
)
and have an array come out.
For your first example this means that the two statements @(4, 1024) and , @((7), (16)) are evaluated individually, and the collective output of the two statements is then returned as an array.
The first statement (@(4, 1024)) outputs two integers, but the second statement (, @((7), (16))) outputs an array of two integers. That is because the leading comma in that statement is interpreted as the unary array construction operator (or comma operator), so you get an array nested in another array, and only the outer array is unrolled during output.
Essentially, your expression is the same as
$files1 = @(
4
1024
, @(7, 16)
)
or
$files1 = 4, 1024, @(7, 16)
Your second example avoids this pitfall, because both nested arrays are prepended with the unary array construction operator and thus protected from being completely unrolled.
With that said, I would recommend to define arrays in a more clear-cut way, e.g. like this:
$files1 = @(4, 1024),
@(7, 16)
or (using grouping expressions instead of array subexpressions) like this:
$files1 = (4, 1024),
(7, 16)
to avoid surprises like the one you observed. The outer @() isn't necessary for defining an array here. PowerShell automatically detects that via the trailing comma at the end of the first line.
For further information see about_Operators.
1
Good answer, but I suggest not (also) calling@(...)an array construction operator; if you want to call it by another name, call it array guarantor operator; that is, loosely speaking, if something isn't already an array, make it one. To drive the point home that@(...)is never needed to create array literals, I suggest removing the@from the final code snippet:$files1 = (4, 1024), (7, 16).
– mklement0
Dec 22 '18 at 22:27
add a comment |
@() is the array subexpression operator, which works differently than array construction operators you may be used to from other languages. The operator evaluates the nested subexpression and returns the output of that expression as an array. Meaning you can do something like this:
@(
Write-Output 'foo'
Get-Content 'C:somefile.txt'
Test-Connection '192.168.23.42' -Count 1
)
and have an array come out.
For your first example this means that the two statements @(4, 1024) and , @((7), (16)) are evaluated individually, and the collective output of the two statements is then returned as an array.
The first statement (@(4, 1024)) outputs two integers, but the second statement (, @((7), (16))) outputs an array of two integers. That is because the leading comma in that statement is interpreted as the unary array construction operator (or comma operator), so you get an array nested in another array, and only the outer array is unrolled during output.
Essentially, your expression is the same as
$files1 = @(
4
1024
, @(7, 16)
)
or
$files1 = 4, 1024, @(7, 16)
Your second example avoids this pitfall, because both nested arrays are prepended with the unary array construction operator and thus protected from being completely unrolled.
With that said, I would recommend to define arrays in a more clear-cut way, e.g. like this:
$files1 = @(4, 1024),
@(7, 16)
or (using grouping expressions instead of array subexpressions) like this:
$files1 = (4, 1024),
(7, 16)
to avoid surprises like the one you observed. The outer @() isn't necessary for defining an array here. PowerShell automatically detects that via the trailing comma at the end of the first line.
For further information see about_Operators.
@() is the array subexpression operator, which works differently than array construction operators you may be used to from other languages. The operator evaluates the nested subexpression and returns the output of that expression as an array. Meaning you can do something like this:
@(
Write-Output 'foo'
Get-Content 'C:somefile.txt'
Test-Connection '192.168.23.42' -Count 1
)
and have an array come out.
For your first example this means that the two statements @(4, 1024) and , @((7), (16)) are evaluated individually, and the collective output of the two statements is then returned as an array.
The first statement (@(4, 1024)) outputs two integers, but the second statement (, @((7), (16))) outputs an array of two integers. That is because the leading comma in that statement is interpreted as the unary array construction operator (or comma operator), so you get an array nested in another array, and only the outer array is unrolled during output.
Essentially, your expression is the same as
$files1 = @(
4
1024
, @(7, 16)
)
or
$files1 = 4, 1024, @(7, 16)
Your second example avoids this pitfall, because both nested arrays are prepended with the unary array construction operator and thus protected from being completely unrolled.
With that said, I would recommend to define arrays in a more clear-cut way, e.g. like this:
$files1 = @(4, 1024),
@(7, 16)
or (using grouping expressions instead of array subexpressions) like this:
$files1 = (4, 1024),
(7, 16)
to avoid surprises like the one you observed. The outer @() isn't necessary for defining an array here. PowerShell automatically detects that via the trailing comma at the end of the first line.
For further information see about_Operators.
edited Dec 28 '18 at 1:12
answered Mar 13 '17 at 20:28
Ansgar WiechersAnsgar Wiechers
141k13125185
141k13125185
1
Good answer, but I suggest not (also) calling@(...)an array construction operator; if you want to call it by another name, call it array guarantor operator; that is, loosely speaking, if something isn't already an array, make it one. To drive the point home that@(...)is never needed to create array literals, I suggest removing the@from the final code snippet:$files1 = (4, 1024), (7, 16).
– mklement0
Dec 22 '18 at 22:27
add a comment |
1
Good answer, but I suggest not (also) calling@(...)an array construction operator; if you want to call it by another name, call it array guarantor operator; that is, loosely speaking, if something isn't already an array, make it one. To drive the point home that@(...)is never needed to create array literals, I suggest removing the@from the final code snippet:$files1 = (4, 1024), (7, 16).
– mklement0
Dec 22 '18 at 22:27
1
1
Good answer, but I suggest not (also) calling
@(...) an array construction operator; if you want to call it by another name, call it array guarantor operator; that is, loosely speaking, if something isn't already an array, make it one. To drive the point home that @(...) is never needed to create array literals, I suggest removing the @ from the final code snippet: $files1 = (4, 1024), (7, 16).– mklement0
Dec 22 '18 at 22:27
Good answer, but I suggest not (also) calling
@(...) an array construction operator; if you want to call it by another name, call it array guarantor operator; that is, loosely speaking, if something isn't already an array, make it one. To drive the point home that @(...) is never needed to create array literals, I suggest removing the @ from the final code snippet: $files1 = (4, 1024), (7, 16).– mklement0
Dec 22 '18 at 22:27
add a comment |
Powershell uses both a comma and a line break as an array separator. Your first declare:
$files1 = @(
@(4, 1024)
, @((7), (16))
)
Creates the following:
$files1[0] = 4
$files1[1] = 1024
$files1[2] = @(7,16)
Your second declare
$files1 = @(
, @(4, 1024)
, @((7), (16))
)
Creates the following:
$files1[0] = @(4, 1024)
$files1[1] = @(7, 16)
As to the parsing decision, it is dependent on the first non-white space character encountered on a line:
Array Literals In PowerShell
and
Understanding PowerShell Parsing Modes
PowerShell uses both a comma and a line break as an array separator is not quite correct: PowerShell has no array separator;,is the array construction operator. Inside array-subexpression operator@(...)- the content of which is not an array - it is statements that you can separate with line breaks (in lieu of;).
– mklement0
Dec 22 '18 at 22:37
add a comment |
Powershell uses both a comma and a line break as an array separator. Your first declare:
$files1 = @(
@(4, 1024)
, @((7), (16))
)
Creates the following:
$files1[0] = 4
$files1[1] = 1024
$files1[2] = @(7,16)
Your second declare
$files1 = @(
, @(4, 1024)
, @((7), (16))
)
Creates the following:
$files1[0] = @(4, 1024)
$files1[1] = @(7, 16)
As to the parsing decision, it is dependent on the first non-white space character encountered on a line:
Array Literals In PowerShell
and
Understanding PowerShell Parsing Modes
PowerShell uses both a comma and a line break as an array separator is not quite correct: PowerShell has no array separator;,is the array construction operator. Inside array-subexpression operator@(...)- the content of which is not an array - it is statements that you can separate with line breaks (in lieu of;).
– mklement0
Dec 22 '18 at 22:37
add a comment |
Powershell uses both a comma and a line break as an array separator. Your first declare:
$files1 = @(
@(4, 1024)
, @((7), (16))
)
Creates the following:
$files1[0] = 4
$files1[1] = 1024
$files1[2] = @(7,16)
Your second declare
$files1 = @(
, @(4, 1024)
, @((7), (16))
)
Creates the following:
$files1[0] = @(4, 1024)
$files1[1] = @(7, 16)
As to the parsing decision, it is dependent on the first non-white space character encountered on a line:
Array Literals In PowerShell
and
Understanding PowerShell Parsing Modes
Powershell uses both a comma and a line break as an array separator. Your first declare:
$files1 = @(
@(4, 1024)
, @((7), (16))
)
Creates the following:
$files1[0] = 4
$files1[1] = 1024
$files1[2] = @(7,16)
Your second declare
$files1 = @(
, @(4, 1024)
, @((7), (16))
)
Creates the following:
$files1[0] = @(4, 1024)
$files1[1] = @(7, 16)
As to the parsing decision, it is dependent on the first non-white space character encountered on a line:
Array Literals In PowerShell
and
Understanding PowerShell Parsing Modes
edited Dec 22 '18 at 22:29
mklement0
127k20241269
127k20241269
answered Mar 13 '17 at 20:31
Christopher G. LewisChristopher G. Lewis
4,3502139
4,3502139
PowerShell uses both a comma and a line break as an array separator is not quite correct: PowerShell has no array separator;,is the array construction operator. Inside array-subexpression operator@(...)- the content of which is not an array - it is statements that you can separate with line breaks (in lieu of;).
– mklement0
Dec 22 '18 at 22:37
add a comment |
PowerShell uses both a comma and a line break as an array separator is not quite correct: PowerShell has no array separator;,is the array construction operator. Inside array-subexpression operator@(...)- the content of which is not an array - it is statements that you can separate with line breaks (in lieu of;).
– mklement0
Dec 22 '18 at 22:37
PowerShell uses both a comma and a line break as an array separator is not quite correct: PowerShell has no array separator;
, is the array construction operator. Inside array-subexpression operator @(...) - the content of which is not an array - it is statements that you can separate with line breaks (in lieu of ;).– mklement0
Dec 22 '18 at 22:37
PowerShell uses both a comma and a line break as an array separator is not quite correct: PowerShell has no array separator;
, is the array construction operator. Inside array-subexpression operator @(...) - the content of which is not an array - it is statements that you can separate with line breaks (in lieu of ;).– mklement0
Dec 22 '18 at 22:37
add a comment |
The key to understanding the Array subexpression operator @( ) is the realization that you don't need it to create arrays, instead arrays are created with the Comma operator ,
As a binary operator, the comma creates an array. As a unary operator,
the comma creates an array with one member. Place the comma before the member.
$myArray = 1,2,3
$SingleArray = ,1
$xs = (1,2,3), (4,5,6) # Count: 2
$ys = (1,2,3),
(4,5,6) # Count: 2
Now consider
# A - two expressions, each expression yields one array of size 3
(1,2,3)
(4,5,6)
# B - one expression resulting in an array of two elements
(1,2,3),
(4,5,6)
# C - similar to A except the sizes are 3 and 1
# (the second array contains a single element)
(1,2,3)
,(4,5,6)
And the final step is to realize that
in essence, the
@(...)operation is syntactic sugar for
[array] $(...)
as explained by the PowerShell Team Blog (The link was given by Christopher G. Lewis answer). Although the meaning and limitations of in essence is not entirely clear to me.
@(...)is just a syntactic sugar for[array] $(...)That is plain wrong. Case 1:$a = @($null); $b = [array] $($null); ConvertTo-Json $a; ConvertTo-Json $b. Case 2:$a = @(,[array]0); $b = [array] $(,[array]0); ConvertTo-Json $a; ConvertTo-Json $b.
– PetSerAl
Dec 22 '18 at 22:39
You seem to know more about this, would you mind editing the answer and fixing it? Especially since the claim is made in the PowerShell Team Blog.
– Micha Wiedenmann
Jan 3 at 13:47
Well, except that last claim, answer is correct. You absolutely do not need@(…), when you use,as array construction operator.
– PetSerAl
Jan 4 at 12:30
What is your opinion on the explanation given by the PowerShell Team Blog?
– Micha Wiedenmann
Jan 4 at 12:34
add a comment |
The key to understanding the Array subexpression operator @( ) is the realization that you don't need it to create arrays, instead arrays are created with the Comma operator ,
As a binary operator, the comma creates an array. As a unary operator,
the comma creates an array with one member. Place the comma before the member.
$myArray = 1,2,3
$SingleArray = ,1
$xs = (1,2,3), (4,5,6) # Count: 2
$ys = (1,2,3),
(4,5,6) # Count: 2
Now consider
# A - two expressions, each expression yields one array of size 3
(1,2,3)
(4,5,6)
# B - one expression resulting in an array of two elements
(1,2,3),
(4,5,6)
# C - similar to A except the sizes are 3 and 1
# (the second array contains a single element)
(1,2,3)
,(4,5,6)
And the final step is to realize that
in essence, the
@(...)operation is syntactic sugar for
[array] $(...)
as explained by the PowerShell Team Blog (The link was given by Christopher G. Lewis answer). Although the meaning and limitations of in essence is not entirely clear to me.
@(...)is just a syntactic sugar for[array] $(...)That is plain wrong. Case 1:$a = @($null); $b = [array] $($null); ConvertTo-Json $a; ConvertTo-Json $b. Case 2:$a = @(,[array]0); $b = [array] $(,[array]0); ConvertTo-Json $a; ConvertTo-Json $b.
– PetSerAl
Dec 22 '18 at 22:39
You seem to know more about this, would you mind editing the answer and fixing it? Especially since the claim is made in the PowerShell Team Blog.
– Micha Wiedenmann
Jan 3 at 13:47
Well, except that last claim, answer is correct. You absolutely do not need@(…), when you use,as array construction operator.
– PetSerAl
Jan 4 at 12:30
What is your opinion on the explanation given by the PowerShell Team Blog?
– Micha Wiedenmann
Jan 4 at 12:34
add a comment |
The key to understanding the Array subexpression operator @( ) is the realization that you don't need it to create arrays, instead arrays are created with the Comma operator ,
As a binary operator, the comma creates an array. As a unary operator,
the comma creates an array with one member. Place the comma before the member.
$myArray = 1,2,3
$SingleArray = ,1
$xs = (1,2,3), (4,5,6) # Count: 2
$ys = (1,2,3),
(4,5,6) # Count: 2
Now consider
# A - two expressions, each expression yields one array of size 3
(1,2,3)
(4,5,6)
# B - one expression resulting in an array of two elements
(1,2,3),
(4,5,6)
# C - similar to A except the sizes are 3 and 1
# (the second array contains a single element)
(1,2,3)
,(4,5,6)
And the final step is to realize that
in essence, the
@(...)operation is syntactic sugar for
[array] $(...)
as explained by the PowerShell Team Blog (The link was given by Christopher G. Lewis answer). Although the meaning and limitations of in essence is not entirely clear to me.
The key to understanding the Array subexpression operator @( ) is the realization that you don't need it to create arrays, instead arrays are created with the Comma operator ,
As a binary operator, the comma creates an array. As a unary operator,
the comma creates an array with one member. Place the comma before the member.
$myArray = 1,2,3
$SingleArray = ,1
$xs = (1,2,3), (4,5,6) # Count: 2
$ys = (1,2,3),
(4,5,6) # Count: 2
Now consider
# A - two expressions, each expression yields one array of size 3
(1,2,3)
(4,5,6)
# B - one expression resulting in an array of two elements
(1,2,3),
(4,5,6)
# C - similar to A except the sizes are 3 and 1
# (the second array contains a single element)
(1,2,3)
,(4,5,6)
And the final step is to realize that
in essence, the
@(...)operation is syntactic sugar for
[array] $(...)
as explained by the PowerShell Team Blog (The link was given by Christopher G. Lewis answer). Although the meaning and limitations of in essence is not entirely clear to me.
edited Jan 3 at 13:46
answered Nov 22 '18 at 9:08
Micha WiedenmannMicha Wiedenmann
10.3k1364103
10.3k1364103
@(...)is just a syntactic sugar for[array] $(...)That is plain wrong. Case 1:$a = @($null); $b = [array] $($null); ConvertTo-Json $a; ConvertTo-Json $b. Case 2:$a = @(,[array]0); $b = [array] $(,[array]0); ConvertTo-Json $a; ConvertTo-Json $b.
– PetSerAl
Dec 22 '18 at 22:39
You seem to know more about this, would you mind editing the answer and fixing it? Especially since the claim is made in the PowerShell Team Blog.
– Micha Wiedenmann
Jan 3 at 13:47
Well, except that last claim, answer is correct. You absolutely do not need@(…), when you use,as array construction operator.
– PetSerAl
Jan 4 at 12:30
What is your opinion on the explanation given by the PowerShell Team Blog?
– Micha Wiedenmann
Jan 4 at 12:34
add a comment |
@(...)is just a syntactic sugar for[array] $(...)That is plain wrong. Case 1:$a = @($null); $b = [array] $($null); ConvertTo-Json $a; ConvertTo-Json $b. Case 2:$a = @(,[array]0); $b = [array] $(,[array]0); ConvertTo-Json $a; ConvertTo-Json $b.
– PetSerAl
Dec 22 '18 at 22:39
You seem to know more about this, would you mind editing the answer and fixing it? Especially since the claim is made in the PowerShell Team Blog.
– Micha Wiedenmann
Jan 3 at 13:47
Well, except that last claim, answer is correct. You absolutely do not need@(…), when you use,as array construction operator.
– PetSerAl
Jan 4 at 12:30
What is your opinion on the explanation given by the PowerShell Team Blog?
– Micha Wiedenmann
Jan 4 at 12:34
@(...) is just a syntactic sugar for [array] $(...) That is plain wrong. Case 1: $a = @($null); $b = [array] $($null); ConvertTo-Json $a; ConvertTo-Json $b. Case 2: $a = @(,[array]0); $b = [array] $(,[array]0); ConvertTo-Json $a; ConvertTo-Json $b.– PetSerAl
Dec 22 '18 at 22:39
@(...) is just a syntactic sugar for [array] $(...) That is plain wrong. Case 1: $a = @($null); $b = [array] $($null); ConvertTo-Json $a; ConvertTo-Json $b. Case 2: $a = @(,[array]0); $b = [array] $(,[array]0); ConvertTo-Json $a; ConvertTo-Json $b.– PetSerAl
Dec 22 '18 at 22:39
You seem to know more about this, would you mind editing the answer and fixing it? Especially since the claim is made in the PowerShell Team Blog.
– Micha Wiedenmann
Jan 3 at 13:47
You seem to know more about this, would you mind editing the answer and fixing it? Especially since the claim is made in the PowerShell Team Blog.
– Micha Wiedenmann
Jan 3 at 13:47
Well, except that last claim, answer is correct. You absolutely do not need
@(…), when you use , as array construction operator.– PetSerAl
Jan 4 at 12:30
Well, except that last claim, answer is correct. You absolutely do not need
@(…), when you use , as array construction operator.– PetSerAl
Jan 4 at 12:30
What is your opinion on the explanation given by the PowerShell Team Blog?
– Micha Wiedenmann
Jan 4 at 12:34
What is your opinion on the explanation given by the PowerShell Team Blog?
– Micha Wiedenmann
Jan 4 at 12:34
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42772083%2fwhy-is-a-leading-comma-required-when-creating-an-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown