簡體   English   中英

在PowerShell中構建哈希數組

[英]Build hash array in PowerShell

我正在讀取由空白行分隔的CSV文件。 我要捕獲數組中空白行之間的每個部分。 where數組將如下所示。

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] 但這是失敗的。

我收到錯誤消息,說“索引超出范圍”“無法索引到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

如果您願意,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++
}

請注意,您需要PowerShell v3或更高版本的有序哈希表和Get-Content -Raw 如果您限於PowerShell v2或更早版本,請刪除[ordered]類型強制轉換並將參數-Raw替換為| Out-String | 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++
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM