14
Ich möchte ein Array von Arrays in Powershell erstellen.Wie erstellt man Array von Arrays in Powershell?
$x = @(
@(1,2,3),
@(4,5,6)
)
Funktioniert gut. Manchmal habe ich jedoch nur ein Array in der Array-Liste. In dieser Situation ignoriert Powershell eine der Listen:
$x = @(
@(1,2,3)
)
$x[0][0] # Should return 1
Unable to index into an object of type System.Int32.
At line:1 char:7
+ $a[0][ <<<< 0]
+ CategoryInfo : InvalidOperation: (0:Int32) [], RuntimeException
+ FullyQualifiedErrorId : CannotIndex
Wie erstelle ich ein Array von Arrays, garantierte es bleibt als zweidimensionale Anordnung, selbst wenn das Array in nur ein Array-Element hat?
Die Magie Komma schlägt wieder! Warum erstellt Powershell kein Array ohne Komma? – jumbo
Ein guter Artikel über die Magie: http://blogs.msdn.com/b/powershell/archive/2007/01/23/array-literals-in-powershell.aspx. Der Kommaoperator ist der Array-Konstruktoroperator in PowerShell –
Natürlich würden Sie dann aus Gründen der Konsistenz der Syntax hoffen, dass dieses '@ (, @ (1,2,3), @ (4,5,6))' funktionieren würde ... aber es gibt dir nicht, was du erwartest. Seufzer. –