How to create an empty array named by variable and use it only by referring to it thru the variable name?
-
26-07-2021 - |
質問
How to create an empty array named by variable and use it only by referring to it thru the variable name?
e.g. array named empty
$empty = @()
$empty -is [array]
True
versus:
$arrayname='empty'
${$arrayname}=@()
$empty -is [array]
False
AND
$empty+='hello'
versus
${$arrayname}+='hello'
解決
maybe you want this:
$arrayname='empty'
$empty -is [array]
False
Invoke-Expression "`$$arrayname = @()"
$empty -is [array]
True
他のヒント
I would rather use a PSObject
$empty = New-Object PSObject -property @{
ArrayName = "String"
ArrayValue = @()
}
$empty.pstypenames.Insert(0,"SuperArray")
// Here you add the generic function to manipulate your array
$empty | Add-Member ScriptProperty Add {$this.ArrayValue} { $this.ArrayValue = $args}
The data structure may be optimized to better fit your requirement.
所属していません StackOverflow