在PowerShell中构建哈希数组

时间:2017-01-30 23:33:44

标签: arrays powershell csv

我正在读取一个由空白行分隔的CSV文件。我希望在数组中捕获的空白行之间的每个部分。数组的位置如下所示。

array[section0][row0]
array[section0][row1]
array[section0][row2]
array[section1][row0]
array[section1][row1]
array[section1][row2]

CSV文件的格式类似于以下内容。

this,is,section,one,line,one
this,is,section,one,line,two
,,,,,,,,,,,,,,,,,,,,,
this,is,section,two,line,one
this,is,section,two,line,two
this,is,section,two,line,three
section,two,with,extra,commas,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,
this,is,section,three,line,one
this,is,section,three,line,two
this,is,section,three,line,three
section,three,with,extra,commas,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,
this,is,section,four,line,one
this,is,section,four,line,two
this,is,section,four,line,three
section,four,with,extra,commas,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,

我正在尝试将数组放入$section[sectionnumber][sectionrownumber]。但这是失败的。

我收到的错误是“index is of bounds”“无法索引到null数组”

我确信它与数组初始化有关。我无法让它发挥作用。

$file   = "filename"
$path   = Split-Path $file
$import = Get-Content $file

#find blank rows
$r = 0
$blank = "yes"
$firstblank = "yes"
$sectionnumber = 0
#initialize section array
$section = ,@()
foreach ($row in $import) {
    if ($row -ne ",,,,,,,,,,,,,,,,,,,,,") {
        #not a blank row
        if ($firstblank -eq "yes") {
            $blank = "no"
            $firstblank = "no"
        } elseif($blank -eq "yes") {
            $blank = "no"
            $r++
        } else {}
        #initialize array for multidemension
        $section[$r][$sectionnumber] = $row
        $sectionnumber++
    } else {
        #this is a blank row
        if ($blank = "no") {
            $blank = "yes"
            $sectionnumber = 0
        } else {
            $blank = "yes"
            $sectionnumber = 0
        }
    }
}
Write-Host $section

1 个答案:

答案 0 :(得分:0)

如果允许,PowerShell可以为您完成大部分繁重的工作。这意味着你可能想做的事。

$import = Get-Content 'C:\path\to\your.txt' -Raw

$section = [ordered]@{}
$i = 0

# remove trailing consecutive commas from the end of each line, then
# split the lines at consecutive line breaks
$import -replace '(?m),+$' -replace "`r?`n", "`n" -split "`n`n+" | Where-Object {
    # filter out blank lines
    $_.Trim()
} | ForEach-Object {
    # for each text block create a new section with a nested hashtable,
    # then split each block into individual rows
    $j = 0
    $section["section$i"] = [ordered]@{}
    $_ -split "`n" | ForEach-Object {
        # split each row at commas and assign to a new record in the
        # nested hashtable
        $section["section$i"]["row$j"] = $_ -split ','
        $j++
    }
    $i++
}

请注意,对于有序哈希表和Get-Content -Raw,您需要PowerShell v3或更高版本。如果您仅限于PowerShell v2或更早版本,请删除[ordered]类型广播并将参数-Raw替换为| Out-String

编辑:如果您只想要一个简单的行列表,其中每行前面都有节和行标题,您可以像这样简化上述内容:

$import -replace '(?m),+$' -replace "`r?`n", "`n" -split "`n`n+" | Where-Object {
    $_.Trim()
} | ForEach-Object {
    $j = 0
    $_ -split "`n" | ForEach-Object {
        "section $i - row $j - $_"
        $j++
    }
    $i++
}