Sign in to follow this  
Followers 0
hot202

how many switch cases can you have?

14 posts in this topic

hi i have started to make a program that will need to use heaps of Switch cases about 7000 or more. Can you use that many? Whats the most amount of lines you are aloud to use in autoit is there a limit? Will the program run slow when it is searching for the right case?

Switch
                        case "1"
                            ;do stuff
                        case "2"
                            ;do stuff
                        case "3"
                            ;do stuff
                        case "4"
                            ;do stuff
                        case "5"
                            ;do stuff
                        case "6"
                            ;do stuff
                        case "7"
                            ;do stuff so on

Share this post


Link to post
Share on other sites



hi i have started to make a program that will need to use heaps of Switch cases about 7000 or more. Can you use that many? Whats the most amount of lines you are aloud to use in autoit is there a limit? Will the program run slow when it is searching for the right case?

Switch
                        case "1"
                            ;do stuff
                        case "2"
                            ;do stuff
                        case "3"
                            ;do stuff
                        case "4"
                            ;do stuff
                        case "5"
                            ;do stuff
                        case "6"
                            ;do stuff
                        case "7"
                            ;do stuff so on

I don't believe there is a limit, no.

The more cases, the longer it will take the CPU to sift through them, however the largest variable here would be your CPU speed. If you computer is slow, you might run into locking up and other issues of the like.

I'm not sure on the specifics, but I believe each case runs at about 4 bytes each, so unless you run into hundreds of thousands of them, and have low ram, I wouldn't see any issues.

Share this post


Link to post
Share on other sites

hi i have started to make a program that will need to use heaps of Switch cases about 7000 or more.

...

That is a huge amount of Cases. It depends on your ";do stuff" whether Cases are efficient at what you are planning. I would consider using arrays if they are suitable.

Basic example

Global $array[8]
Global $counter = 0

; assign integers to array
For $i = 0 To UBound($array) -1
    $array[$i] = $counter
    $counter += 1
Next

; if switch was testing for 7 then be direct
$var = 7
If $var < UBound($array) Then
    ConsoleWrite($array[$var] & @CRLF)
EndIf

Going by your example code as a measure, the array index of 7 above does a direct show of a value. This example is only as good as your example so it may not be a good solution but I thought I would offer an alternative as an idea.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

i cant really use array cos the cases change they aint 1 2 3 4 5 so on they are more like Case "J198308" Case "J198304" so random kinda

the do stuff will be like this

$1 = what eva

$2 = something

$3 = something else

$4 = so on about 10 things in there

then after the cases i will have something that displays the $1 $2 $3 $4 in a msg box

So is there a limit on the amount of lines you use in SciTe? will it make my exe huge?

Edited by hot202

Share this post


Link to post
Share on other sites

Don't forget that AutoIt is interpreted, not compiled. What I mean is that for instance a C compiler will build a jump table when there are more than few entries and thus large selects can run in constant time whatever case value you give it. Not so with AutoIt. The moral is: arrange your Case entries from the most common Case values down to the rarer.

Also don't forget that you can group entries which need the same code: Case 5, 17, 33 To 46, 78 is perfectly valid.

Finally when you have the situation where value X needs A() then B() processing in turn and value Y needs B() only, use ContinueCase like this:

...
 Case X
 A()
    ContinueCase
 Case Y
 B()
 Case Z ...

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

#6 ·  Posted (edited)

The limit is very large but I do not have the exact specs at the moment. I doubt that you will breach the limit. The size of your exe will depend on the amount of lines of course.

Your last description sounds like an array may still work. No harm in trying. :(

Global $array[2][5] = [ _
        ['J198304', 'what eva', 'something', 'something else', 'so on about 10 things in there'], _
        ['J198305', 'what eva', 'something', 'something else', 'so on about 10 things in there']]

$var = 'J198304'

For $i = 0 To UBound($array) -1
    ; test for value in 1st element of array
    If $var = $array[$i][0] Then
        ; set variables
        $a = $array[$i][1]
        $b = $array[$i][2]
        $c = $array[$i][3]
        $d = $array[$i][4]
        MsgBox(0, $array[$i][0], $a & @CRLF & $b & @CRLF & $c & @CRLF & $d)
    EndIf
Next

An array maybe faster but it will consume memory of all the data which maybe a minus for it. If you go the Case route then you could make a script to create your large amount of Cases if the data can be handled programmically witch may save some manual typing.

Edit:

Corrected grammer

Edited by MHz

Share this post


Link to post
Share on other sites

How about using execute and a bunch of corresponding functions?

Global $result

$case = "J198308"
Execute($case & "()")
MsgBox(0, $case, $result)

Func J198308()
    $1 = "what eva"
    $2 = "something"
    $3 = "something else"
    $4 = "so on about 10 things in there"
    $result = $1 & @CRLF & $2 & @CRLF & $3 & @CRLF & $4
EndFunc

Share this post


Link to post
Share on other sites

Use a double array like a database table, that's the way to go. Sort of like MHz's example.

$array[$y][$x]

$y is the number of entries

$x is the pieces of information you need to store about each entry

Not only is this faster to code but you can populate the array by reading an external file so you don't need to change the code in the future to add something. You even export this 2d array to an excel file with 1 function call if that would be helpful.

Share this post


Link to post
Share on other sites

Thanks guys i will have to see what ones seem to work the best for me.

Share this post


Link to post
Share on other sites

What if i use a ini file? Would that work or is there a limit you can have on them? Are they slower to search?

Share this post


Link to post
Share on other sites

What if i use a ini file? Would that work or is there a limit you can have on them? Are they slower to search?

Slower compared to what? They'll definately be slower than an array created in the script.

According to the helpfile on IniReadSection() and IniReadSectionNames():

Only the first 32767 chars are taken in account in an section due to Win9x compatibility.

There is no notion about the maximum amount of sections.

If you'd show us the actual script I'm sure you'll get allot of examples on how to best handle your problem.

Share this post


Link to post
Share on other sites

You could use an ini file and have the values say comma delimited so you could StringSplit() the return for iniread() but it is limited compared to ShawnW idea of using MS Excel or OO Calc to edit the file if it was say a comma delimited file. I guess it depends on you for your overall purpose of the script maintenance of it there after.

Share this post


Link to post
Share on other sites

if you use excel do people have to have it installed on there computer for it to read from the excel file?

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

Using MS Excel or OO Calc is refered to the application developers POV (i.e you). Expecting the user to edit the data file is extraordinary to comprehend without reason (expecting another 3rd party app) though I support user readable files as default (txt based) but sometimes you need to do what needs to be done.

Edit:

A comma delimited file is a txt based file with commas, i.e human readable as is.

Edited by MHz

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
Sign in to follow this  
Followers 0