0

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 ?

2
  • 1
    A hash table would make a lot of sense for this Commented Jan 19, 2022 at 22:00
  • 3
    That information is not stored in $arrayAll. Think of a variable like a bag or a purse. If you have two bags filled with marbles - one green bag, one blue bag - and you then empty both of them into a big red bag, then you don't have "a red bag of green-bag and blue-bag marbles" - you just have "a red bag of marbles". Commented Jan 19, 2022 at 22:05

1 Answer 1

2

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]
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.