rcmaehl

Switch...Case Returning True Out of Range

4 posts in this topic

#1 ·  Posted

I'm having an issue with Switch...Case matching outside the declared range

Using the following code:

#include <Array.au3>

Local $sTxt
Local $aArray
Local $aNumbers[10] = [":zero: ", ":one: ", ":two: ", ":three: ", ":four: ", ":five: ", ":six: ", ":seven: ", ":eight: ", ":nine: "]

While 1
    $sTxt = InputBox("Enter Text", "Enter Text to Convert to Blocks", "")
    If @error Then Exit
    Dim $aArray[StringLen($sTxt)]
    For $iLoop = 0 To StringLen($sTxt) - 1 Step 1
        $aArray[$iLoop] = AscW(StringRight(StringLeft($sTxt, $iLoop + 1), 1))
    Next
    For $iLoop = 0 To UBound($aArray) - 1
        Switch ChrW($aArray[$iLoop])
            Case Chr(0) To Chr(31), '"', "%" To ")", "+" To "/", ":" To ">", "@", "[" To "`"
                ConsoleWrite("1" & @CRLF)
                $aArray[$iLoop] = Chr($aArray[$iLoop])
            Case " "
                ConsoleWrite("2" & @CRLF)
                $aArray[$iLoop] = "   "
            Case "!"
                ConsoleWrite("3" & @CRLF)
                $aArray[$iLoop] = Random(0,1,1) ? ":exclamation: " : ":grey_exclamation: "
            Case "#"
                ConsoleWrite("4" & @CRLF)
                $aArray[$iLoop] = ":hash: "
            Case "$"
                ConsoleWrite("5" & @CRLF)
                $aArray[$iLoop] = Random(0,1,1) ? ":heavy_dollar_sign: " : ":moneybag: "
            Case "*"
                ConsoleWrite("6" & @CRLF)
                $aArray[$iLoop] = Random(0,1,1) ? ":asterisk: " : ":eight_spoked_asterisk: "
            Case "?"
                ConsoleWrite("7" & @CRLF)
                $aArray[$iLoop] = Random(0,1,1) ? ":question: " : ":grey_question: "
            Case "0" To "9"
                ConsoleWrite("8" & @CRLF)
                $aArray[$iLoop] = $aNumbers[Chr($aArray[$iLoop])]
            Case 'A' To 'Z', 'a' to 'z'
                ConsoleWrite("9" & @CRLF)
                If StringLower(ChrW($aArray[$iLoop])) = "a" Then
                    $aArray[$iLoop] = Random(0,1,1) ? ":regional_indicator_a: " : ":a: "
                ElseIf StringLower(ChrW($aArray[$iLoop])) = "b" Then
                    $aArray[$iLoop] = Random(0,1,1) ? ":regional_indicator_b: " : ":b: "
                ElseIf StringLower(ChrW($aArray[$iLoop])) = "i" Then
                    $aArray[$iLoop] = Random(0,1,1) ? ":regional_indicator_i: " : ":information_source: "
                ElseIf StringLower(ChrW($aArray[$iLoop])) = "m" Then
                    $aArray[$iLoop] = Random(0,1,1) ? ":regional_indicator_m: " : ":m: "
                ElseIf StringLower(ChrW($aArray[$iLoop])) = "o" Then
                    $aArray[$iLoop] = Random(0,1,1) ? ":regional_indicator_o: " : ":o2: "
                ElseIf StringLower(ChrW($aArray[$iLoop])) = "p" Then
                    $aArray[$iLoop] = Random(0,1,1) ? ":regional_indicator_p: " : ":parking: "
                Else
                    $aArray[$iLoop] = ":regional_indicator_" & StringLower(Chr($aArray[$iLoop])) & ": "
                EndIf
            Case Chr(128) To Chr(255)
                ConsoleWrite("10" & @CRLF)
                $aArray[$iLoop] = Chr($aArray[$iLoop])
            Case Else
                ConsoleWrite("Else Statement Executed" & @CRLF)
                $aArray[$iLoop] = ChrW($aArray[$iLoop])
        EndSwitch
    Next
    _ArrayToClip($aArray, "")
WEnd

Inputting Plaintext works as expected until you throw in extended ASCII such as ñ

Expected Debug Console Output: 10
Actual Debug Console Output: 9

Does telling case to match A through Z cause it to attempt to match all Latin Characters? Am I misusing Case statements and it shouldn't work this way? Any help would be great as the help file for Switch...Case doesn't specify what the values for ranges can be however it seems to work as expected until it doesn't. 


My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

MY PROJECTS


Active: IRC UDF, WindowEx UDF
Discontinued: GithubBubbleSort UDF

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Did you expect this:

#include <Array.au3>

Local $sTxt
Local $aArray
Local $aNumbers[10] = [":zero: ", ":one: ", ":two: ", ":three: ", ":four: ", ":five: ", ":six: ", ":seven: ", ":eight: ", ":nine: "]

While 1
    $sTxt = InputBox("Enter Text", "Enter Text to Convert to Blocks", "")
    If @error Then Exit
    ConsoleWrite("----" & $sTxt & "----------" & @CRLF)
    Dim $aArray[StringLen($sTxt)]
    For $iLoop = 0 To StringLen($sTxt) - 1 Step 1
        $aArray[$iLoop] = AscW(StringRight(StringLeft($sTxt, $iLoop + 1), 1))
    Next
    For $iLoop = 0 To UBound($aArray) - 1
        ConsoleWrite("->" & ChrW($aArray[$iLoop]) & @CRLF)
        Switch Asc(ChrW($aArray[$iLoop]))
            Case 0 To 31, Asc('"'), Asc("%") To Asc(")"), Asc("+") To Asc("/"), Asc(":") To Asc(">"), Asc("@"), Asc("[") To Asc("`")
                ConsoleWrite("1" & @CRLF)
                $aArray[$iLoop] = Chr($aArray[$iLoop])
            Case Asc(" ")
                ConsoleWrite("2" & @CRLF)
                $aArray[$iLoop] = "   "
            Case Asc("!")
                ConsoleWrite("3" & @CRLF)
                $aArray[$iLoop] = Random(0, 1, 1) ? ":exclamation: " : ":grey_exclamation: "
            Case Asc("#")
                ConsoleWrite("4" & @CRLF)
                $aArray[$iLoop] = ":hash: "
            Case Asc("$")
                ConsoleWrite("5" & @CRLF)
                $aArray[$iLoop] = Random(0, 1, 1) ? ":heavy_dollar_sign: " : ":moneybag: "
            Case Asc("*")
                ConsoleWrite("6" & @CRLF)
                $aArray[$iLoop] = Random(0, 1, 1) ? ":asterisk: " : ":eight_spoked_asterisk: "
            Case Asc("?")
                ConsoleWrite("7" & @CRLF)
                $aArray[$iLoop] = Random(0, 1, 1) ? ":question: " : ":grey_question: "
            Case Asc("0") To Asc("9")
                ConsoleWrite("8" & @CRLF)
                $aArray[$iLoop] = $aNumbers[Chr($aArray[$iLoop])]
            Case Asc('A') To Asc('Z'), Asc('a') To Asc('z')
                ConsoleWrite("9" & @CRLF)
                If StringLower(ChrW($aArray[$iLoop])) = "a" Then
                    $aArray[$iLoop] = Random(0, 1, 1) ? ":regional_indicator_a: " : ":a: "
                ElseIf StringLower(ChrW($aArray[$iLoop])) = "b" Then
                    $aArray[$iLoop] = Random(0, 1, 1) ? ":regional_indicator_b: " : ":b: "
                ElseIf StringLower(ChrW($aArray[$iLoop])) = "i" Then
                    $aArray[$iLoop] = Random(0, 1, 1) ? ":regional_indicator_i: " : ":information_source: "
                ElseIf StringLower(ChrW($aArray[$iLoop])) = "m" Then
                    $aArray[$iLoop] = Random(0, 1, 1) ? ":regional_indicator_m: " : ":m: "
                ElseIf StringLower(ChrW($aArray[$iLoop])) = "o" Then
                    $aArray[$iLoop] = Random(0, 1, 1) ? ":regional_indicator_o: " : ":o2: "
                ElseIf StringLower(ChrW($aArray[$iLoop])) = "p" Then
                    $aArray[$iLoop] = Random(0, 1, 1) ? ":regional_indicator_p: " : ":parking: "
                Else
                    $aArray[$iLoop] = ":regional_indicator_" & StringLower(Chr($aArray[$iLoop])) & ": "
                EndIf
            Case 128 To 255
                ConsoleWrite("10" & @CRLF)
                $aArray[$iLoop] = Chr($aArray[$iLoop])
            Case Else
                ConsoleWrite("Else Statement Executed" & @CRLF)
                $aArray[$iLoop] = ChrW($aArray[$iLoop])
        EndSwitch
    Next
    _ArrayToClip($aArray, "")
WEnd

The ranges work only with numbers I guess.

Jos

Edited by Jos
1 person likes this

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#3 ·  Posted

1 hour ago, Jos said:

The ranges work only with numbers I guess.

Jos

Which is weird, because excluding Extended ASCII it works as expected, but yeah, I'll just convert it all to number ranges as I need it.


My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

MY PROJECTS


Active: IRC UDF, WindowEx UDF
Discontinued: GithubBubbleSort UDF

Share this post


Link to post
Share on other sites

#4 ·  Posted

I believe this is a bug in how Switch handles value ranges. The help says "Switch statements are case-insensitive" but things aren't that simple as it seems to perform a diacritic-insensitive comparison only for value ranges. Here's a [much simpler] repro:

Local $a = "à"
Switch $a
    Case "a"
        MsgBox(0, "", "à = a")
    Case "a" To "a"
        MsgBox(0, "", "à ∊ [a, a]")
    Case "a" To "b"
        MsgBox(0, "", "à ∊ [a, b]")
EndSwitch

Nothing (expectedly) prevents values or value ranges to be strings and this is quite common practice indeed.

Note another issue with the second Case: it should trigger since the third Case in code triggers.

That needs work.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now