数据行到字符串到数组问题 [英] Data Rows to String to Array issue

查看:27
本文介绍了数据行到字符串到数组问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个非常奇怪的问题.我有一个用一堆行填充的对象.我可以很好地访问它们,但我需要附加一个."(点)到其中的每个值,所以我最终使用 for each 循环将每个记录转换为字符串并添加一个."调整值后.然而,现在的问题是,我想将这些行中的每一行(并且这些行只有一个列/项目)分配给另一个数组/对象,以便我以后可以访问它们.

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.

问题是,即使我声明一个对象/数组变量并分配字符串转换的数据行,对象变量也会不断转换为字符串,我似乎无法避免它.

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-约束变量,因为类型文字 [string]被放置在变量的左边$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.

您希望创建一个字符串数组,在PowerShell中表示为[string[]]:

You were looking to create a string array, which in PowerShell is represented as [string[]]:

[string[]] $DBNames = @()


PowerShell 的类型转换是自动(并且非常灵活),因此 [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 必须做的每当您将 += 与数组一起使用分配一个数组 在幕后,复制原始元素并附加新元素;然后新数组会自动分配回变量.

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]](System.Collections.Generic.List`1):

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.

注意:过去常常使用 System.Collections.ArrayList 实例代替;然而:

Note: It used to be common to use System.Collections.ArrayList instances instead; however:

  • 不再推荐使用这种类型(请参阅链接帮助主题中的警告);使用 [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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆