Reputation: 5
For example
a = @(
"test1",
"test2"
)
b = @(
"test3",
"test4"
)
one is
foreach ($c in $a) {
ehco $c
}
What should I do if I have more than one?
Upvotes: 0
Views: 48
Reputation: 8889
$a = @("test1","test2"); $b = @("test3","test4")
$a,$b | % {$_}
test1
test2
test3
test4
Upvotes: 0
Reputation: 12959
You can add the arrays and then loop through each item.
$a = @(
"test1",
"test2"
)
$b = @(
"test3",
"test4"
)
foreach($item in ($a + $b))
{
write-host $item
}
test1
test2
test3
test4
Upvotes: 0
Reputation: 23633
Or create an (ordered) hashtable:
$hashtable = [Ordered]@{ a = @("test1", "test2"); b = @("test3", "test4") }
foreach ($Key in $HashTable.Keys) {
foreach ($Item in $HashTable[$Key]) {
echo $Item
}
}
Yields:
test1
test2
test3
test4
Upvotes: 1
Reputation: 141
You could do it like this:
$a = @(
"test1",
"test2"
)
$b = @(
"test3",
"test4"
)
foreach ($c in ($a,$b)) {
echo $c
}
Upvotes: 0
Reputation: 1277
You cannot do a foreach on more than one object. You can do a for loop, but it will break if the arrays are not of the same size Here's an example with a check on the arrays size:
if ($a.Count -eq $b.Count)
{
for ($i=0; $i -lt $a.count; $i++)
{
$a[$i]
$b[$i]
}
}
Upvotes: 0