将for循环的变量插入另一个变量名 [英] Inserting for loop's variable into another variable names
问题描述
我正在尝试简化同事使用的Windows接口脚本,在该脚本中他使用了很多if循环,我认为可以使用for循环进一步缩短这些循环.
I'm trying to simplify a Windows interface script that my colleague did in which he used a lot of if-loops which I think can be further shortened using for-loops.
基本上,我们有几台计算机,每台计算机的范围为4-12,我们为每个管理员提供一个界面来还原其各自的快照.
Basically we have a couple sets of computers that ranged from 4 - 12 per set & we provide each admin an interface to revert their respective snapshots.
我正在用for循环中的$ i值替换每个变量中的数字.
I'm looking to replace the number in each variable with the $i value from the for-loop.
我尝试使用数组&哈希表,但根据我收集的数据,它们更适合存储值&而不是包含其他变量的公式.
I've tried using arrays & hashtables but based on what I've gathered, they're much more suitable in storing values & not formulas that included other variables.
if($ComputerNumber -eq 4) {
$checkBoxComputer1LocX = $leftMargin
$checkBoxComputer1LocY = [int]$labelSubHeaderLocY + [int]$buttonHeight + [int]$padding
$checkBoxComputer2LocX = $leftMargin
$checkBoxComputer2LocY = [int]$labelSubHeaderLocY + [int]$buttonHeight + [int]$padding + (1 * ([int]$checkBoxHeight + [int]$padding))
$checkBoxComputer3LocX = $leftMargin
$checkBoxComputer3LocY = [int]$labelSubHeaderLocY + [int]$buttonHeight + [int]$padding + (2 * ([int]$checkBoxHeight + [int]$padding))
$checkBoxComputer4LocX = $leftMargin
$checkBoxComputer4LocY = [int]$labelSubHeaderLocY + [int]$buttonHeight + [int]$padding + (3 * ([int]$checkBoxHeight + [int]$padding))
}
这是我要实现的目标:
for($i=1; $i -le $ComputerNumber; i++) {
$checkBoxComputer$iLocX = $leftMargin
$checkBOxComputer$iLocY = [int]$labelSubHeaderLocY + [int]$buttonHeight + [int]$padding + (($i - 0) * ([int]$checkBoxHeight + [int]padding))
}
推荐答案
使用变量间接寻址(通过存储在另一个变量中的名称来引用变量)进行赋值 ,使用Set-Variable
(对于检索,您将使用Get-Variable
):
To use variable indirection (referring to a variable via its name stored in another variable) for assignment, use Set-Variable
(for retrieval, you'd use Get-Variable
):
for($i=1; $i -le $ComputerNumber; i++) {
Set-Variable -Name checkBoxComputer${i}LocX -Value $leftMargin
Set-Variable -Name checkBoxComputer${i}LocY -Value ([int]$labelSubHeaderLocY + [int]$buttonHeight + [int]$padding + (($i - 0) * ([int]$checkBoxHeight + [int]padding)))
}
请注意,变量$i
的名称是如何用{...}
(${i}
)括起来的,以便清楚地对其进行描述,从而不会将后续字符视为名称的一部分.
Note how the name of variable $i
is enclosed in {...}
(${i}
) in order to unambiguously delineate it, so that the subsequent characters aren't considered part of the name.
但是,您绝对可以使用 arrays 和自定义对象代替单个变量,这是更可取的:
However, you can definitely use arrays and custom objects instead of individual variables, which is preferable:
[array] $checkBoxes = for($i=1; $i -le $ComputerNumber; i++) {
[pscustomobject] @{
LocX = $leftMargin
LocY= [int]$labelSubHeaderLocY + [int]$buttonHeight + [int]$padding + (($i - 0) * ([int]$checkBoxHeight + [int]padding))
}
}
上面的代码创建了一个(基于0
-索引的)自定义对象数组,每个对象都有一个.LocX
和.LocY
属性,因此,例如,您可以访问第一个复选框的.LocX
值,如下所示:如下:
The above creates a (0
-index-based) array of custom objects that each have a .LocX
and .LocY
property, so that, for instance, you can access the first checkbox's .LocX
value as follows:
$checkBoxes[0].LocX
这篇关于将for循环的变量插入另一个变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!