全部!@ {}和[Hashtable] :: Clear()之間有什麼區別?
我正在編寫代碼將標準INI文件結構轉換爲基於哈希表和數組的代碼。例如,INI代碼段如下所示:
[Book]
title="Moby Dick"
author="Herman Melville"
genre=fiction
genre=fantasy
[Book]
title="James and the Giant Peach"
author="Roald Dahl"
genre="fiction"
會是什麼樣子:
@{
"Book" = [
@{
"title" = ["Moby Dick"];
"author" = ["Herman Melville"];
"genre" = ["fiction","fantasy"]
},
@{
"title" = ["James and the Giant Peach"];
"author" = ["Roald Dahl"];
"genre" = ["fiction"]
}
]
}
我想解釋有多個相同名字的部分和部分有多個相同名字的特性INI文件,因此這個數據結構。我知道我可以用一棵樹來做同樣的事情,但我並不關心它的速度,而且在我編寫它時更容易實現。
這是通過更新他們的值的區間內關聯的所有屬性哈希表做(讓我們稱之爲$items
),然後更新,與前述的那些哈希表的每個部分相關聯的主要哈希表(我們稱之爲一個$results
)。散列表是全局性的,並且在迭代到新節時清除屬性散列表。
雖然我編寫了這樣的代碼,並且運行良好,但我卻爲了Powershell清除哈希表的方式而苦苦掙扎了幾分鐘。如果我使用$items.Clear()
,清除$items
,那麼與$results
中的所有元素關聯的散列表也會被清除。但是,如果我通過陳述[email protected]{}
「清除」它,則不會發生這種情況。
我假設發生這種情況是因爲前一種情況下的哈希表都是對全局哈希表的引用,並且是後者中的獨立對象。這是怎麼回事?
代碼在下面;對不起,如果任何這是令人困惑(並且它不是典型的Powershell語法)。與$items = $items.Clear()
替換$items = @{}
複製:
function parse ($file) {
$result = @{}
if (!(Test-Path $file)) {
write-host "File does not exist: $file"
} elseif (!($data = cat $file)) {
write-host "File has no data: $file"
} else {
$last_value = ""
$last_data = ""
$items = @{}
foreach ($line_raw in $data | ?{$_.Trim().Length -gt 0}) {
$line = $line_raw.TrimStart().TrimEnd()
$first_char = $line[0]
switch ($first_char) {
';' {
continue
}
'[' {
$key = $line -replace "\[|\]",""
if ($last_key) {
if (!($result.ContainsKey($last_key)))
{ $result.Add($last_key,@($items)) }
else
{ $result[$last_key] += $items }
if ($last_key -ne $key)
{ $items = @{} }
}
$last_key = $key
}
default {
$item , $data = $(
$sep = $line.indexOf('=')
$line.substring(0,$sep) , $line.substring($sep+1)
)
if (!$items.ContainsKey($item))
{ $items.Add($item,@($data)) }
else
{ $items[$item] += $data }
}
}
}
$result
}
}
我似乎無法複製該行爲。你可以發佈代碼,瞭解你如何設置不同的哈希表? – DavidN