参考操作员缺少属性名称

发布于 2025-01-21 19:06:56 字数 1294 浏览 0 评论 0原文

我有一个地图,最初是C ++代码,文件读取并解析为地图。原始代码是枚举,没有所有项目的值。 $ filecontent:

enum{
    Error_A = 110,    
    Error_B,               
    Error_C,               
    Error_D,             
    Error_E,             
    Error_F,          
    Error_G = 118,  
...
};    

我已经阅读了文件内容,然后将其放在这样的地图中(正常工作):

  function Get-Contents_b{
    [cmdletbinding()]
    Param ([string]$fileContent)

            #Error_AA = 20
  
    # create an ordered hashtable to store the results
    $errorMap = [ordered]@{}
    # process the lines one-by-one
    switch -Regex ($fileContent -split '\r?\n') {
      '^[\s]*([\w]+)[\s=]*([-\d]*)' { # Error...=12345
        $key,$value = ($matches[1,2])|ForEach-Object Trim
        $errorMap[$key] = $value
    }
  }
...

然后我想在地图上迭代,对于枚举值的图表,依赖于单位的枚举值比上一个数字增加了,我想分配上一个值的值加一个。我试图在下面做到这一点,但是使用$ key-1,然后从中获得值,然后在评论中显示错误。

  foreach ($key in $errorMap.$keys)
  {
      $previousKey = $errorMap.[($key-1)]  #missing property name after the reference operator
      Write-Host $errorMap.$previousKey
      if(($errorMap.$key).Value = "")
      {
        $errorMap.$key.Value = $errorMap.$previousKey.Value + 1
      }
  }

有什么想法解决此问题或获取上一个值并为下一个空值分配上一个值的值加一个值?

这是使用PowerShell 5.1和Vscode。

I have a map, that is originally c++ code, file read, and parsed into a map. The original code was an enum, and didn't have values for all items. $fileContent:

enum{
    Error_A = 110,    
    Error_B,               
    Error_C,               
    Error_D,             
    Error_E,             
    Error_F,          
    Error_G = 118,  
...
};    

I have read the file contents and put it in a map like this (works fine):

  function Get-Contents_b{
    [cmdletbinding()]
    Param ([string]$fileContent)

            #Error_AA = 20
  
    # create an ordered hashtable to store the results
    $errorMap = [ordered]@{}
    # process the lines one-by-one
    switch -Regex ($fileContent -split '\r?\n') {
      '^[\s]*([\w]+)[\s=]*([-\d]*)' { # Error...=12345
        $key,$value = ($matches[1,2])|ForEach-Object Trim
        $errorMap[$key] = $value
    }
  }
...

Then I want to iterate over the map, and for the ones with enum values dependent on single digit increase from the previous, I want to assign the value of the previous value plus one. I'm trying to do that below, but getting the $previousKey, using $key-1, and then getting the value from that, is giving the error shown in the comment.

  foreach ($key in $errorMap.$keys)
  {
      $previousKey = $errorMap.[($key-1)]  #missing property name after the reference operator
      Write-Host $errorMap.$previousKey
      if(($errorMap.$key).Value = "")
      {
        $errorMap.$key.Value = $errorMap.$previousKey.Value + 1
      }
  }

Any ideas how to fix this or get the previous value and assign the next empty value the previous value plus one?

This is with powershell 5.1 and VSCode.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森林散布 2025-01-28 19:06:56

您会错误地通过运算符访问索引访问[...] - 您必须使用一个另一个。

但是,您想要的是位置访问密钥(仅适用于订购 hashtable):

foreach ($keyIndex in 0..($errorMap.Count-1))
{
  if ('' -eq $errorMap[$keyIndex]) {
    $previousValue = $errorMap[$keyIndex - 1] 
    Write-Host $previousValue
    $errorMap[$keyIndex] = 1 + $previousValue
  }
}

You're mistakenly mixing member (property) access via the . operator with indexed access via [...] - you must use one or the other.

However, what you want is positional access to your keys (which only works with an ordered hashtable):

foreach ($keyIndex in 0..($errorMap.Count-1))
{
  if ('' -eq $errorMap[$keyIndex]) {
    $previousValue = $errorMap[$keyIndex - 1] 
    Write-Host $previousValue
    $errorMap[$keyIndex] = 1 + $previousValue
  }
}
装迷糊 2025-01-28 19:06:56

为什么不立即在刺台上创建值,而不是后来填充空的值?

function Get-Contents_b{
    [cmdletbinding()]
    Param ([string]$fileContent)
    # create an ordered hashtable to store the results
    $errorMap     = [ordered]@{}
    $currentValue = 0
    # process the lines one-by-one
    switch -Regex ($fileContent -split '\r?\n') {
        '^\s+(\w+)\s*[,=]'{
            $key, $value = ($_ -split '[,=]', 2).Trim()
            if ([string]::IsNullOrWhiteSpace($value)) { $value = $currentValue }
            $errorMap[$key] = [int]$value
            $currentValue   = [int]$value + 1
        }
    }
    # return the map
    $errorMap
}

Get-Contents_b $enum

输出:

Name                           Value
----                           -----
Error_A                        110
Error_B                        111
Error_C                        112
Error_D                        113
Error_E                        114
Error_F                        115
Error_G                        118

Why not create the values in your Hashtable straight away instead of filling the empties afterwards?

function Get-Contents_b{
    [cmdletbinding()]
    Param ([string]$fileContent)
    # create an ordered hashtable to store the results
    $errorMap     = [ordered]@{}
    $currentValue = 0
    # process the lines one-by-one
    switch -Regex ($fileContent -split '\r?\n') {
        '^\s+(\w+)\s*[,=]'{
            $key, $value = ($_ -split '[,=]', 2).Trim()
            if ([string]::IsNullOrWhiteSpace($value)) { $value = $currentValue }
            $errorMap[$key] = [int]$value
            $currentValue   = [int]$value + 1
        }
    }
    # return the map
    $errorMap
}

Get-Contents_b $enum

Output:

Name                           Value
----                           -----
Error_A                        110
Error_B                        111
Error_C                        112
Error_D                        113
Error_E                        114
Error_F                        115
Error_G                        118
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文