How to create an empty array named by variable and use it only by referring to it thru the variable name?

StackOverflow https://stackoverflow.com/questions/13186090

質問

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top