数据行到字符串到数组的问题 [英] Data Rows to String to Array issue
问题描述
我遇到了一个很奇怪的问题.我有一个装有一堆行的对象.我可以很好地访问它们,但是我需要附加一个."(点)到其中的每个值,因此最终我使用for每个循环将每个记录转换为字符串,并添加一个."修整值后.但是,现在的问题是,我想将这些行中的每行(并且这些行只有一个列/项)分配给另一个数组/对象,以便以后可以访问它们.
I have a very weird problem happening. I have an object populated with a bunch of rows. I can access them all well, but I need to append a "." (dot) to every value inside it, so I end up converting each record to a string using a for each loop and adding a "." after trimming the values. The issue now however, is that I would like to assign each of these rows (And the rows have only one column/Item) to another array/object, so I can access them later.
问题在于,即使我声明了一个对象/数组变量并分配了字符串转换后的数据行,该对象变量仍会不断转换为String,而我似乎无法避免.
The issue is that even when I declare an object/array variable and assign the string converted data rows, the object variable keeps getting converted to a String, and I just cant seem to avoid it.
请帮助.这是修改后的代码示例:
Please help. Here is a modified code sample:
[String] $DBNms = @();
ForEach($DBName in $objDBNms){
$tempText += $DBName.Item(0).ToString().Trim() + "."
$DBNms += $tempText
}
Write-Host($DBNms.GetType()) #And this is showing up a string, while I want it to be an array.
如果我打印$ DBNms,它确实显示了一个串联在一起的字符串,虽然我实际上希望它像$ DBNms [0] =第一个项目值,$ DBNms [1] =第二个项目值等等.
And if I print $DBNms, it indeed shows up a string concatenated together into a single string, while I actually want it to be like $DBNms[0] = first Item value, $DBNms[1] = second Item value and so on.
推荐答案
[String] $ DBNms = @();
使 $ DBNms
成为 type-受限制的变量,因为类型文字 [字符串]
被放置在变量的左侧code> $ DBNms ,然后将其类型作为字符串锁定;即,作为单个字符串.
[String] $DBNms = @();
makes $DBNms
a type-constrained variable, due to type literal [string]
being placed to the left of variable $DBNms
, whose type is then locked in as a string; that is, as a single string.
您要创建一个字符串 array ,在PowerShell中将其表示为 [string []]
:
You were looking to create a string array, which in PowerShell is represented as [string[]]
:
[string[]] $DBNames = @()
PowerShell的类型转换是 automatic (非常灵活),因此 [String] $ DBNms = @()
不会报告错误,它会悄悄地转换空的 array ( @()
)到空的 string (根据类型约束的要求):
PowerShell's type conversions are automatic (and very flexible), so that [String] $DBNms = @()
doesn't report an error, it quietly converts the empty array (@()
) to the empty string (as required by the type constraint):
PS> [string] $DBNames = @(); '' -eq $DBNames
True
从数组的多次迭代中收集值的一种更有效的方法是使用 foreach
语句作为表达式 PowerShell为您自动 收集输出:
[string[]] $DBNms = foreach ($DBName in $objDBNms){
# Output this expression's value as-is.
# PowerShell will collect the individual iterations' values for you.
$DBName.Item(0).ToString().Trim() + "."
}
以上内容不仅比您的原始方法更简洁,而且更重要的是效率更高:
The above is not only more concise than your original approach, it is more importantly more efficient:
-
实际上,您不能直接添加(附加到)数组,因为它是一个不可变数据结构.
In reality you cannot directly add (append to) an array, because it is an immutable data structure.
当您将 + =
与数组一起使用时,PowerShell必须做什么分配 new 数组在幕后,并复制原始元素并附加新元素;然后,新数组将自动分配回该变量.
What PowerShell has to do whenever you use +=
with an array is to allocate a new array behind the scenes, with the original elements copied over and the new element(s) appended; the new array is then automatically assigned back to the variable.
注意:使用整个循环作为表达式的替代和下一个最佳解决方案是使用一种有效扩展的列表类型 . [System.Collections.Generic.List [object]]
(
Note: The alternative and next best solution to using the whole loop as an expression is to use an efficiently extensible list type, notably [System.Collections.Generic.List[object]]
(System.Collections.Generic.List`1
):
# Create the list.
# The 'System.' namespace prefix is optional.
[Collections.Generic.List[string]] $DBNms = @()
foreach ($DBName in $objDBNms) {
# Add to the list, using its .Add() method.
# Note: Do NOT try to add with +=
$DBNms.Add($DBName.Item(0).ToString().Trim() + ".")
}
如果循环在每次迭代中不仅仅输出单个值,例如需要附加到多个集合中,则将需要这种方法.
You'll need this approach if your loop does more than outputting a single value per iteration, such as needing to append to multiple collections.
Note: It used to be common to use instances instead; however:System.Collections.ArrayList
-
不再推荐使用这种类型的 (请参阅链接的帮助主题中的警告);使用
[System.Collections.Generic.List [object]]
代替,它还允许您强烈键入集合(替换[object]
以及感兴趣的类型,例如上面代码中的[string]
).
Use of this type is no longer recommended (see the warning in the linked help topic); use
[System.Collections.Generic.List[object]]
instead, which additionally allows you to strongly type the collection (replace[object]
with the type of interest, such as[string]
in the code above).
它的缺点是它的 .Add()
方法具有返回值,您需要明确使其静音(例如, $ null= $ lst.Add(...)
),这样它就不会意外污染代码的输出流(产生意外的额外输出).
It has the drawback of its .Add()
method having a return value, which you need to silence explicitly (e.g., $null = $lst.Add(...)
), so that it doesn't accidentally pollute your code's output stream (produce unexpected extra output).
这篇关于数据行到字符串到数组的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!