Reputation: 591
So I got this code :
$t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10, $t11 = $list[0].split(" ")
but is too much, and I think I can automate the creation of $t
variables.
How is the correct procedure? with loop but to have the same result.
for ($i=0;$i -le 21; $i++) {
$objResult += New-Object -TypeName PSObject -Property @{"C$i" = ($list[0].split(" "))[$i]}
}
or with
set-variable -name "T$i" -value ($list[0].split(" "))[$i]
Upvotes: 1
Views: 209
Reputation: 2164
If these are simple variables, Set-Variable
works perfectly. like so:
for ($i = 0; $i -lt 3; $i++) {
set-variable -name "test$i" -value $i
}
$test0
$test1
$test2
PS > .\test.ps1
0
1
2
If you are looking to work with somewhat more complicated variables (such as COM objects for instance) then New-Object
is what you need.
Upvotes: 1