You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

问询:提取COBOL Copybook PIC格式数据的PowerShell代码是否全覆盖

问题分析:现有PowerShell代码对COBOL PIC格式的覆盖情况

问题陈述

需开发PowerShell代码从COBOL Copybook中提取PIC格式各类数据的数字长度(非字节)与精度。现提供一段已实现的代码,需确认:该代码是否能处理所有PIC数据格式?若存在未被覆盖的格式,请予以告知。

代码已处理的PIC格式

  • PIC X(n):提取字符长度n
  • PIC 9(n):提取数字长度n及整数长度
  • PIC 9(n)V99:提取总长度、整数长度n及小数精度
  • PIC -9(n).99:提取总长度、整数长度n及精度
  • PIC 9(n).99:提取总长度、整数长度n及精度
  • PIC -9(n).9(m):提取总长度、整数长度n及小数精度m
  • PIC 9(n).9(m):提取总长度、整数长度n及小数精度m
  • PIC 9(n) OCCURS m TIMES:提取总长度(n*m)及整数长度n
  • 简单类型PIC A、PIC 9:长度设为1
  • PIC .9(n):提取精度n及总长度
  • PIC -.9(n):提取精度n及总长度

现有PowerShell代码

$copybookDataType = "PIC 9(09)V99."
$totalLength = ""
$integerLength = "N/A"
$precision = "N/A"

# 1. Handling format: PIC X(n).
if ($copybookDataType -match "^PIC\s+X\((\d+)\).$") {
    $totalLength = $Matches[1]
}
# 2. Handling format: PIC 9(n).
elseif ($copybookDataType -match "^PIC\s+(\d+)\((\d+)\).$") {
    $totalLength = $Matches[2]
    $integerLength = $Matches[1]
}
# 3. Handling format: PIC 9(09)V99.
elseif ($copybookDataType -match "^PIC\s+9\((\d+)\)V(\d+)\.$") {
    $integerLength = $Matches[1]
    $precision = ($Matches[2] -split "9").Count
    $totalLength = $integerLength + $precision + 1
}
# 4. Handling format: PIC -9(n).
elseif ($copybookDataType -match "^PIC\s+-9\((\d+)\).$") {
    $integerLength = $Matches[1]
    $totalLength = $integerLength + 1
}
# 5. Handling format: PIC 9(n)V9(m).
elseif ($copybookDataType -match "^PIC\s+(\d+)\(\d+V(\d+)\).$") {
    $integerLength = $Matches[1]
    $precision = $Matches[2]
    $totalLength = $integerLength + $precision + 1
}
# 6. Handling format: PIC -9(n).99.
elseif ($copybookDataType -match "^PIC\s+-9\((\d+)\)\.(\d+).$") {
    $integerLength = $Matches[1]
    $precision = ($Matches[2] -split "9").Count
    if ($copybookDataType -match "\.$") {
        $precision -= 1
    }
    $totalLength = $integerLength + $precision + 3
}
# 7. Handling format: PIC 9(n).99.
elseif ($copybookDataType -match "^PIC\s+9\((\d+)\)\.(\d+).$") {
    $integerLength = $Matches[1]
    $precision = ($Matches[2] -split "9").Count
    if ($copybookDataType -match "\.$") {
        $precision -= 1
    }
    $totalLength = $integerLength + $precision + 2
}
# 8. Handling format: PIC -9(n).9(m).
elseif ($copybookDataType -match "^PIC\s+-9\((\d+)\)\.9\((\d+)\).$") {
    $integerLength = $Matches[1]
    $precision = $Matches[2]
    if ($copybookDataType -match "\.$") {
        $precision -= 1
    }
    $totalLength = $integerLength + $precision + 4
}
# 9. Handling format: PIC 9(n).9(m).
elseif ($copybookDataType -match "^PIC\s+9\((\d+)\)\.9\((\d+)\).$") {
    $integerLength = $Matches[1]
    $precision = $Matches[2]
    if ($copybookDataType -match "\.$") {
        $precision -= 1
    }
    $totalLength = $integerLength + $precision + 3
}
# 10. Handling format: PIC 9(n) OCCURS m TIMES.
elseif ($copybookDataType -match "^PIC\s+(\d+)\(\d+\)\s+OCCURS\s+(\d+)\s+TIMES.$") {
    $dataTypeLength = $Matches[1]
    $occursTimes = $Matches[2]
    $totalLength = $dataTypeLength * $occursTimes
    $integerLength = $dataTypeLength
}
# 11. Handling simple data types: PIC A, PIC 9.
elseif ($copybookDataType -match "^PIC\s+(\w+).$") {
    $totalLength = 1
}
# 12. Handling format: PIC .9(n).
elseif ($copybookDataType -match "^PIC\s+\.9\((\d+)\).$") {
    $precision = $Matches[1]
    $totalLength = $precision + 1
}
# 13. Handling format: PIC -.9(n).
elseif ($copybookDataType -match "^PIC\s+-\.9\((\d+)\).$") {
    $precision = $Matches[1]
    $totalLength = $precision + 2
}

Write-Host "Total Length: $totalLength"
Write-Host "Integer Length: $integerLength"
Write-Host "Precision: $precision"

结论:代码无法处理所有PIC格式

现有代码仅覆盖了部分基础PIC格式,以下是未被覆盖的常见格式及场景:

1. 标准符号表示(S前缀/后缀)

COBOL中常用S表示符号位(替代代码中的-),这类格式完全未被处理:

  • PIC S9(n)
  • PIC S9(n)V9(m)
  • PIC 9(n)S(符号位在末尾)
  • PIC 9(n)V9(m)S

2. 无括号的重复格式

直接写重复字符而非用括号指定长度的格式:

  • PIC 999(等价于PIC 9(3))
  • PIC XXX(等价于PIC X(3))
  • PIC 99V99(等价于PIC 9(2)V9(2))

3. 混合类型格式

同时包含字母、数字的组合PIC格式:

  • PIC A9(n)
  • PIC X(n)9(m)
  • PIC 9(n)A(m)

4. 其他编辑符号格式

除.外的编辑分隔符或填充符号:

  • PIC 9(n),9(m)(逗号作为千位分隔符)
  • PIC 9(n)/9(m)(斜杠作为日期分隔符)
  • PIC ZZZ9(Z表示前导零填充为空格)
  • PIC ***9(星号表示前导零填充为星号)
  • PIC 9(n)-(负号在末尾)

5. 带存储属性的格式

PIC后跟随存储类型的格式:

  • PIC 9(n) COMP(二进制存储)
  • PIC 9(n) COMP-3(压缩十进制)
  • PIC X(n) DISPLAY(默认显示存储)

6. 可变次数OCCURS格式

包含DEPENDING ON的可变重复次数格式:

  • PIC 9(n) OCCURS 5 TO 10 TIMES DEPENDING ON COUNT-FIELD

7. 其他特殊格式

  • PIC 9(n) SIGN LEADING SEPARATE(符号位单独占一位)
  • PIC 9(n) SIGN TRAILING SEPARATE
  • PIC .999(等价于PIC .9(3))

额外:现有代码的错误问题

除覆盖不全外,代码本身存在正则表达式错误:

  • 处理PIC 9(n)的正则^PIC\s+(\d+)\((\d+)\).$会错误匹配PIC 5(10)这类无效格式,正确应为^PIC\s+9\((\d+)\).$
  • 处理PIC 9(n)V9(m)的正则^PIC\s+(\d+)\(\d+V(\d+)\).$完全不符合格式,正确应为^PIC\s+9\((\d+)\)V9\((\d+)\).$
  • 处理OCCURS的正则^PIC\s+(\d+)\(\d+\)\s+OCCURS\s+(\d+)\s+TIMES.$同样错误匹配非9开头的格式,应修正为^PIC\s+9\((\d+)\)\s+OCCURS\s+(\d+)\s+TIMES.$

内容的提问来源于stack exchange,提问作者your.godlike.gamer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 23:44:51