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

时间:2018-01-02 21:22:22

标签: scripting autoit data-conversion

我的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)

2 个答案:

答案 0 :(得分:0)

这种语法:

($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]与每个字符串进行比较。

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

答案 1 :(得分:0)

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

#include <signal.h>
#include <unistd.h>
...
signal(SIGUSR1, SIG_IGN); /* Ignore own signal */

killpg(getpgrp(), SIGUSR1); /* Send SIGUSR1 to pgrp */