Home > other >  Array of Arrays - Print Names of Inner Arrays - Powershell
Array of Arrays - Print Names of Inner Arrays - Powershell

Time:01-22

I have two arrays in one outer array

$array1 = @("a", "b")
$array2 = @("c", "d")
$arrayAll = $array1, $array2

I now want to print the names "array1" , "array2" in the foreach-loop

foreach($array in $arrayAll){
    Write-Host $array
} 

Which returns

a b 
c d
with the result that i want would be
$array1
$array2

While $allArray.Count still returns 2 elements. How do i print just the Array names and not what is in the Arrays ?

CodePudding user response:

Probably not the answer you expect but this is a good place for using a hash table:

$hashAll = [ordered]@{}
$hashAll['array1'] = @("a", "b")
$hashAll['array2'] = @("c", "d")

foreach($key in $hashAll.Keys)
{
    "This is array: $key"
    $hashAll[$key]
}
  •  Tags:  
  • Related