为什么我的脚本将值乘以 30.48?

Why does my script multiply values by 30.48?

我的 AutoIt 脚本让用户将文本粘贴到一个字段中,然后单击一个按钮将习惯单位转换为公制单位。它不仅转换单词,还转换它们前面的数字。

下面的文字是输入:

1 foot 1 inch
2 feet 2 inches
3 feet 3 inches
1 inch
2 inches
3 in
1 foot
2 feet
3 ft

这是预期的输出:

33.02 centimeters
66.04 centimeters
99.06 centimeters
2.54 centimeters
5.08 centimeters
7.62 cm  
30.48 centimeters
60.96 centimeters
91.44 cm

但这就是我得到的:

1006.4496 centimeters
2012.8992 centimeters
3019.3488 centimeters
2.54 centimeters
5.08 centimeters
7.62 cm
30.48 centimeters
60.96 centimeters
91.44 centimeters

前三行似乎乘以 30.48。我不要那个。这是代码:

Case $msg = $CTRL_i
    ;For unit measurements of feet and inches
    $input_str = GUICtrlRead($Textbox)
    $WordArray = StringRegExp($input_str, "[\s\.:;,]*([a-zA-Z0-9-_]*\.?[a-zA-Z0-9-_]+)[\s\.:;,]*", 3)
    ;MsgBox($MB_OK, "Size:", UBound($WordArray))
    For $i = 0 To (UBound($WordArray) - 1) Step 1
        If (($i + 3) <= UBound($WordArray)) Then
            If (StringIsDigit($WordArray[$i]) Or StringIsFloat($WordArray[$i])) And ($WordArray[$i + 1] = "feet" Or "foot" Or "ft") And (StringIsDigit($WordArray[$i + 2]) Or StringIsFloat($WordArray[$i + 2])) And ($WordArray[$i + 3] = "inches" Or "inch" Or "in") Then
                $cm_value = ($WordArray[$i] * 30.48) + ($WordArray[$i + 2] * 2.54)
                $old_string = $WordArray[$i] & " " & $WordArray[$i + 1] & " " & $WordArray[$i + 2] & " " & $WordArray[$i + 3]
                $new_string = $cm_value & " centimeters"
                $input_str = StringReplace($input_str, $old_string, $new_string, 1)
                MsgBox($MB_OK, "Feet and inches", $WordArray[$i])
            EndIf
        EndIf
    Next
    $WordArray = StringRegExp($input_str, "[\s\.:;,]*([a-zA-Z0-9-_]*\.?[a-zA-Z0-9-_]+)[\s\.:;,]*", 3)
    ;MsgBox($MB_OK, "Size:", UBound($WordArray))
    For $i = 0 To (UBound($WordArray) - 1) Step 1
        If (($i + 1) <= UBound($WordArray)) Then
            ;This is for when a measurement of inches is represented as plural.
            If (StringIsDigit($WordArray[$i]) Or StringIsFloat($WordArray[$i])) And $WordArray[$i + 1] = "inches" Then
                $cm_value = $WordArray[$i] * 2.54
                $old_string = $WordArray[$i] & " " & $WordArray[$i + 1]
                $new_string = $cm_value & " centimeters"
                $input_str = StringReplace($input_str, $old_string, $new_string, 1)
                MsgBox($MB_OK, "inches", $WordArray[$i])
                ;This is for when a measurement of inches is represented as singular.
            ElseIf (StringIsDigit($WordArray[$i]) Or StringIsFloat($WordArray[$i])) And $WordArray[$i + 1] = "inch" Then
                $cm_value = $WordArray[$i] * 2.54
                $old_string = $WordArray[$i] & " " & $WordArray[$i + 1]
                $new_string = $cm_value & " centimeters"
                $input_str = StringReplace($input_str, $old_string, $new_string, 1)
                MsgBox($MB_OK, "inch", $WordArray[$i])
                ;This is for when a measurement of inches is represented as an abbreviation.
            ElseIf (StringIsDigit($WordArray[$i]) Or StringIsFloat($WordArray[$i])) And $WordArray[$i + 1] = "in" Then
                $cm_value = $WordArray[$i] * 2.54
                $old_string = $WordArray[$i] & " " & $WordArray[$i + 1]
                $new_string = $cm_value & " cm"
                $input_str = StringReplace($input_str, $old_string, $new_string, 1)
                MsgBox($MB_OK, "in", $WordArray[$i])
            EndIf
        EndIf
    Next
    $WordArrayC = StringRegExp($input_str, "[\s\.:;,]*([a-zA-Z0-9-_]*\.?[a-zA-Z0-9-_]+)[\s\.:;,]*", 3)
    ;MsgBox($MB_OK, "Size:", UBound($WordArray))
    For $i = 0 To (UBound($WordArray) - 1) Step 1
        If (($i + 1) <= UBound($WordArray)) Then
            ;This is for when a measurement of feet is represented as plural or singular.
            If (StringIsDigit($WordArray[$i]) Or StringIsFloat($WordArray[$i])) And ($WordArray[$i + 1] = "feet" Or "foot") Then
                $cm_value = $WordArray[$i] * 30.48
                $old_string = $WordArray[$i] & " " & $WordArray[$i + 1]
                $new_string = $cm_value & " centimeters"
                $input_str = StringReplace($input_str, $old_string, $new_string, 1)
                MsgBox($MB_OK, "feet", $WordArray[$i])
                ;This is for when a measurement of feet is represented as an abbreviation.
            ElseIf (StringIsDigit($WordArray[$i]) Or StringIsFloat($WordArray[$i])) And $WordArray[$i + 1] = "ft" Then
                $cm_value = $WordArray[$i] * 30.48
                $old_string = $WordArray[$i] & " " & $WordArrayC[$i + 1]
                $new_string = $cm_value & " cm"
                $input_str = StringReplace($input_str, $old_string, $new_string, 1)
                MsgBox($MB_OK, "ft", $WordArray[$i])
            EndIf
        EndIf
    Next
    GUICtrlSetData($Textbox, $input_str)

这种语法:

($WordArray[$i+1] = "feet" Or "foot" Or "ft")

实际解释为

(($WordArray[$i+1] = "feet") Or ("foot") Or ("ft"))

其中 "foot" 始终为 True,"ft" 始终为 True,因为两者都不是空字符串。

改用以下语法:

($WordArray[$i+1] = "feet" Or $WordArray[$i+1] = "foot" Or $WordArray[$i+1] = "ft")

后面比较$WordArray[$i+1]每个字符串

更改这种语法的所有实例,您的代码可能会按预期工作。

这有点容易出错,但它显示了一种更简单的方法。

#include <Array.au3>
$str = "1 foot 1 inch" & @CR & _
        "2 feet 2 inches" & @CR & _
        "3 feet 3 inches" & @CR & _
        "1 inch" & @CR & _
        "2 inches" & @CR & _
        "3 in" & @CR & _
        "2 feet" & @CR & _
        "1 foot" & @CR & _
        "3 ft"
$rows_A = StringSplit($str, @CR, 2)
;~ _ArrayDisplay($rows_A, 'Rows')
;~ 33.02 centimeters
;~ 66.04 centimeters
;~ 99.06 centimeters
;~ 2.54 centimeters
;~ 5.08 centimeters
;~ 7.62 centimeters
;~ 30.48 centimeters
;~ 60.96 centimeters
;~ 91.44 centimeters

Local $sum = 0

For $i = 0 To UBound($rows_A) - 1
    If StringRegExp($rows_A[$i], '(\d.*?\d.*?)', 0) Then
        $splitted_row_A = StringRegExp($rows_A[$i], '(\d)\s*(\w+)', 3)
;~      _ArrayDisplay($splitted_row_A)
        $sum = 0
        For $y = 0 To UBound($splitted_row_A) - 2
            If StringInStr($splitted_row_A[$y + 1], 'in') Then
                $sum += $splitted_row_A[$y] * 2.54
            ElseIf StringInStr($splitted_row_A[$y + 1], 'f') Then
                $sum += $splitted_row_A[$y] * 30.48
            EndIf
        Next
        If $sum > 0 Then ConsoleWrite($rows_A[$i] & ' => ' & $sum & @CRLF)
    Else
        If StringInStr($rows_A[$i], 'in') Then ConsoleWrite($rows_A[$i] & ' = ' & $rows_A[$i] * 2.54 & @CRLF)
        If StringInStr($rows_A[$i], 'f') Then ConsoleWrite($rows_A[$i] & ' = ' & $rows_A[$i] * 30.48 & @CRLF)
    EndIf
Next