英文:
Build List of Lists from Delimited String in Powershell
问题
非常简单地说,我想要将一个长度为X的字符串解析为一个对象。
字符串的格式如下:0||1||2||3;a||b||c||d;10||11||12||13;
我的目标是将该内容转换为以下结构,以用作单独的变量和值:
var1 | var2 | var3 | var4 |
---|---|---|---|
0 | 1 | 2 | 3 |
a | b | c | d |
10 | 11 | 12 | 13 |
我的最终目标是能够通过调用 foreach 循环来遍历它们(例如foreach row in table: row.var1 + row.var2 + ...等
)。行数不受限制,但变量的数量始终为4(如果数量多或少,我会处理错误)。
我显然可以处理将行转换为行的初始部分:
$rows = $Content -split ";"
这将返回以下表格:
var |
---|
```0 |
```a |
```10 |
然后,我遍历行并尝试创建单元格(但这不是我需要的方式):
$table = @()
foreach ($row in $rows) {
$cells = $row -split "\|\|"
$table += ,$cells
}
是否有人可以提供关于循环可能看起来像什么以引导我朝正确方向迈出第一步的指导或建议?
我看到有人告诉我构建类似于以下内容的东西:
$NewRow = @{ 'var1' = "0" ; 'var2' = "1" ; 'var3' = "2"; 'var4' = "3"}
$table.Add($NewRow) | Out-Null
然而,我无法想出一个适当的循环,可以一致地填充这些值,或者是否这是正确的方法。
澄清一下:我知道这种数据格式看起来很奇怪,但值中可能包含大量不同的分隔符,因此在我了解数据通常情况下的情况下,|| 是迄今为止最安全的选项。
英文:
Very Simply put I am looking to take a string that is X length and parse it into an object.
The string will look like this: 0||1||2||3;a||b||c||d;10||11||12||13;
My goal is to pipe that content into the following structure to be used as individual variables and values:
var1 | var2 | var3 | var4 |
---|---|---|---|
0 | 1 | 2 | 3 |
a | b | c | d |
10 | 11 | 12 | 13 |
My eventual goal is to be able to loop through these by calling a foreach loop (ie foreach row in table: row.var1 + row.var2 +...etc
). The number of rows is not restricted, but the number of variables will always be 4 (I'll error handle if there are more or fewer).
I can obviously handle the initial part of converting the lines to rows:
$rows = $Content -split ";"
That returns this table:
var |
---|
```0 |
```a |
```10 |
Then I parse through the rows and attempt to make cells (but this doesnt work the way I need it to):
table=@()
foreach ($row in $rows) {
$cells = $row -split "\|\|"
$table += ,$cells
}
Can anyone provide guidance or recommendations and what a loop might look like to kick me in the right direction?
I am seeing guides telling me to build things like this:
$NewRow=@{ 'var1' = "0" ; 'var2' = "1" ; 'var3' = "2"; 'var4' = "3"}
$table.Add($NewRow) | Out-Null
However, I am failing to think of a proper loop that would work to consistently fill those values or if that is the right approach.
To clarify: I know this data format looks odd, but the possible data in the values can contain a ton of different delimiters, so || was by far the safest option given how I know how the data looks on a regular basis.
答案1
得分: 1
ForEach-Object 中的每一行,通过收集字典/哈希表中的所有属性值构建一个单一对象,然后将其强制转换为 `[PSCustomObject]`:
```powershell
'0||1||2||3;a||b||c||d;10||11||12||13;'.Split(';') |Where-Object {$_} |ForEach-Object {
# 创建用于保存属性值的字典
$properties = [ordered]@{}
# 跟踪属性编号
$varCount = 1
# 将每个单独的值添加到字典中的相应属性条目中
$_ -split '\|\|' |ForEach-Object {
$properties["var${varCount}"] = $_
$varCount++
}
# 强制转换并输出结果对象
[PSCustomObject]$properties
}
<details>
<summary>英文:</summary>
For each row, construct a single object by collecting all the property values in a dictionary/hashtable, then cast to `[PSCustomObject]`:
'0||1||2||3;a||b||c||d;10||11||12||13;'.Split(';') |Where-Object {$_} |ForEach-Object {
create dictionary to hold property values
$properties = [ordered]@{}
keep track of property number
$varCount = 1
add each individual value to its own property entry in the dictionary
$_ -split '||' |ForEach-Object {
$properties["var${varCount}"] = $_
$varCount++
}
cast and output resulting object
[PSCustomObject]$properties
}
</details>
# 答案2
**得分**: 0
我能够使用以下公式使我的情况正常工作:
```powershell
$entries = @()
foreach ($line in $inputLines) {
$entryData = $line -split '||'
$obj = [PSCustomObject] @{
var1 = $entryData[0]
var2 = $entryData[1]
var3 = $entryData[2]
var4 = $entryData[3]
}
$entries += $obj
}
其他答案在字段数量未知时非常有用,但在循环外部保留所有内容方面存在问题(可能是因为我的使用方式)。我发现这是最简单的方法。
英文:
I was able to get my situation to work using the following formula:
$entries = @()
foreach ($line in $inputLines) {
$entryData = $line -split '\|\|'
$obj = [PSCustomObject] @{
var1 = $entryData[0]
var2 = $entryData[1]
var3 = $entryData[2]
var4 = $entryData[3]
}
$entries += $obj
}
The other answers are super useful if the number of fields is unknown, but had issues outside the loop of keeping all content (likely from my usage). I found this to be the easiest method.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论