在C:\folder
中,我有一个名为text.txt的文件,其内容如下。
This is a test line 1
This is also a test
This is a test on line 3
This was a test.
This is a test for line 5.
This is a test line 1
This is also a test
This is a test on line 3
This was a test.
This is a test for line 5.
我有一个脚本来获取该文件的内容。
$test_template = "{worthless_text:This is a test} for {line_number:line 5}"
$test = gc "C:\folder\test.txt"
$test_array = @()
$counter = 0
foreach($line in $test){
if($line -match 'line\s+5'){
$new_line = $line | ConvertFrom-String -TemplateContent $test_template
$test_array += $new_line.line_number
$counter++
}
}
$test_array
我正在用模板解析内容。
这是我的问题。
为什么我执行$ test_array.count会显示4
?如果仅显示$test_array
的内容,则仅显示找到匹配的两项。
如果我将$test_array += $new_line.line_number
更改为$test_array.Add($new_line.line_number)
,为什么会出现以下错误?
Exception calling "Add" with "1" argument(s): "Collection was of a fixed size." At line:10 char:9
+ $test_array.Add($new_line.line_number)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NotSupportedException Exception calling "Add" with "1" argument(s): "Collection was of a fixed size." At line:10 char:9
+ $test_array.Add($new_line.line_number)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NotSupportedException
我真的需要理解我的第二个问题,因为我基于试图解析文件的脚本构建了该测试脚本,当我的模板项进入数组时,而不是使其像(' item1','item2','item3')将它们像一个长字符串一样混在一起。('item1 item2 item3')。我试图了解自己在做错什么。