Jump to content

Anyway to do this faster?


Recommended Posts

Hi,

I made a autoitscript, that works, but I wonder if this can be written shorter (in the source that is)?

$on1=IniRead ( ".\menu.ini", "BUTTON01", "ON", "default" )
$on2=IniRead ( ".\menu.ini", "BUTTON02", "ON", "default" )
$on3=IniRead ( ".\menu.ini", "BUTTON03", "ON", "default" )
$on4=IniRead ( ".\menu.ini", "BUTTON04", "ON", "default" )
$on5=IniRead ( ".\menu.ini", "BUTTON05", "ON", "default" )
$on6=IniRead ( ".\menu.ini", "BUTTON06", "ON", "default" )
$on7=IniRead ( ".\menu.ini", "BUTTON07", "ON", "default" )
$on8=IniRead ( ".\menu.ini", "BUTTON08", "ON", "default" )
$on9=IniRead ( ".\menu.ini", "BUTTON09", "ON", "default" )
$on10=IniRead ( ".\menu.ini", "BUTTON10", "ON", "default" )
$on11=IniRead ( ".\menu.ini", "BUTTON11", "ON", "default" )
$on12=IniRead ( ".\menu.ini", "BUTTON12", "ON", "default" )
$on13=IniRead ( ".\menu.ini", "BUTTON13", "ON", "default" )
$on14=IniRead ( ".\menu.ini", "BUTTON14", "ON", "default" )
$on15=IniRead ( ".\menu.ini", "BUTTON15", "ON", "default" )
$on16=IniRead ( ".\menu.ini", "BUTTON16", "ON", "default" )
$on17=IniRead ( ".\menu.ini", "BUTTON17", "ON", "default" )
$on18=IniRead ( ".\menu.ini", "BUTTON18", "ON", "default" )
$on19=IniRead ( ".\menu.ini", "BUTTON19", "ON", "default" )
$on20=IniRead ( ".\menu.ini", "BUTTON20", "ON", "default" )
$on21=IniRead ( ".\menu.ini", "BUTTON21", "ON", "default" )
$on22=IniRead ( ".\menu.ini", "BUTTON22", "ON", "default" )
$on23=IniRead ( ".\menu.ini", "BUTTON23", "ON", "default" )
$on24=IniRead ( ".\menu.ini", "BUTTON24", "ON", "default" )
$on25=IniRead ( ".\menu.ini", "BUTTON25", "ON", "default" )
$on26=IniRead ( ".\menu.ini", "BUTTON26", "ON", "default" )
$on27=IniRead ( ".\menu.ini", "BUTTON27", "ON", "default" )
$on28=IniRead ( ".\menu.ini", "BUTTON28", "ON", "default" )
$on29=IniRead ( ".\menu.ini", "BUTTON29", "ON", "default" )
$on30=IniRead ( ".\menu.ini", "BUTTON30", "ON", "default" )
$on31=IniRead ( ".\menu.ini", "BUTTON31", "ON", "default" )
$on32=IniRead ( ".\menu.ini", "BUTTON32", "ON", "default" )
$on33=IniRead ( ".\menu.ini", "BUTTON33", "ON", "default" )
$on34=IniRead ( ".\menu.ini", "BUTTON34", "ON", "default" )
$on35=IniRead ( ".\menu.ini", "BUTTON35", "ON", "default" )
$on36=IniRead ( ".\menu.ini", "BUTTON36", "ON", "default" )
$on37=IniRead ( ".\menu.ini", "BUTTON37", "ON", "default" )
$on38=IniRead ( ".\menu.ini", "BUTTON38", "ON", "default" )
$on39=IniRead ( ".\menu.ini", "BUTTON39", "ON", "default" )
$on40=IniRead ( ".\menu.ini", "BUTTON40", "ON", "default" )
$on41=IniRead ( ".\menu.ini", "BUTTON41", "ON", "default" )
$on42=IniRead ( ".\menu.ini", "BUTTON42", "ON", "default" )
$on43=IniRead ( ".\menu.ini", "BUTTON43", "ON", "default" )
$on44=IniRead ( ".\menu.ini", "BUTTON44", "ON", "default" )
$on45=IniRead ( ".\menu.ini", "BUTTON45", "ON", "default" )

I think it should be something like:

For $on = 1 to 45 Step +1
                    IniRead ( ".\menu.ini", "BUTTON" & $on, "ON", "default" )
                    
                Next

But this loop should be made for $on1, $on2, $on3 until $on45. The problem is I don't know how to make the variable a variable. If you know what I mean.

Please help!

Regards,

Mr. Breaker

Link to comment
Share on other sites

this seems to work

$ra=Random(1,5)
$ran=Round($ra,0)
$duh="duh"
$comb=$duh&$ran
MsgBox(0,"",$comb)
If $comb="duh1" Then
    MsgBox(0,"","duh1")
ElseIf $comb="duh2" Then
    MsgBox(0,"","duh2")
ElseIf $comb="duh3" Then
    MsgBox(0,"","duh3")
ElseIf $comb="duh4" Then
    MsgBox(0,"","duh4")
ElseIf $comb="duh5" Then
    MsgBox(0,"","duh5")
    EndIf

010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Link to comment
Share on other sites

Put it in an array:

$sINI = @ScriptDir & "\menu.ini"
Global $aButtonOn[46] = [45] ; [0] contains count

For $n = 1 To $aButtonOn[0]
    $aButtonOn[$n] = IniRead($sINI, "BUTTON" & $n, "ON", "default")
Next

; Check Button23 as an example
MsgBox(64, "Button23", "ON = " & $aButtonOn[23])

That's exactly what arrays are for.

:(

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

$sIni = ".\menu.ini"
For $i = 1 To 45 ;; No step reguired, 1 is the default
    Assign("on" & $i, IniRead($sIni, "Button" & $i, "ON", "default"), 2)
Next

see Assign() in the help file.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

see Assign() in the help file.

Nay, my son! Shun the ways of the twin evils, Assign() and Eval()! Harken not to the siren song of the temptress George!

:(

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Nay, my son! Shun the ways of the twin evils, Assign() and Eval()! Harken not to the siren song of the temptress George!

:(

I've probably proclaimed the evils of those more than most people around here but there are times when I secretly use it. Of course in this situation an array may be a better solution anyway. Even then you still have to declare the variables.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

You are most welcome. Beware the evils of Assign() though. Okay, so it's not really evil in all situations, but it's not something you want to over-use. As I mentioned above, i use it on occasion and I think the last occasion was about 3 years ago.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • 5 months later...

Just another question...

This is a piece of my source:

Case $msg = $OptionsMenu_button01_browse
                $vr1= StringSplit($execute1, "\")
                $end1= $vr1[0]
                $del1= StringLen($vr1[$end1])
                $new1= StringTrimRight($execute1, $del1)
                $var1= FileOpenDialog("Selecteer te starten item...", $new1, "All (*.*)")
                IniWrite(@ScriptDir & "\menu.ini", "BUTTON01", "EXECUTE", $var1)
                If $var1<> ("") Then  
                $execute1= $var1 
                Endif
                If $var1 = ("") Then                    
                    IniWrite(@ScriptDir & "\menu.ini", "BUTTON01", "EXECUTE", $execute1)
                    Else
                    If @error Then
                    IniWrite(@ScriptDir & "\menu.ini", "BUTTON01", "EXECUTE", $execute1)
                EndIf
                EndIf

This case should should be repeated 45 times

For the moment (as a newbie) scripter, this is what I came up with:

For $i = 1 to 45
            If $i <= 9 Then
                Select
                Case $msg = Eval("OptionsMenu_button0" & $i & "_browse")
                    Assign("vr" & $i, StringSplit(Eval("execute" & $i), "\"))
                    Assign("end" & $i, Eval("vr" & $i [0])
                    Assign("del" & $i, StringLen(Eval("vr" & $i [Eval("end" & $i])
...

Which shows where my problem is (assigning and evaluating variables to array values).

Any hint will be much appreciated!!!

Regards,

Mr. Breaker

Link to comment
Share on other sites

If you need 45 instances of something, keep them in an array:

Global $aVR[45] ; Use this array instead of $vr1 thru $vr45
Global $aEnd[45] ; Use this array instead of $end1 thru $end45
Global $aDel[45] ; Use this array instead of $del1 thru $del45
Global $aExecute[45] ; Use this array instead of $execute1 thru $execute45

; ...

Select
    Case $msg = $OptionsMenu_button01_browse
        For $n = 0 To UBound($aVR) - 1
            $aVR[$n] = StringSplit($aExecute[$n], "\")
            $aEnd[$n] = $aVR[$n][0]
            $aDel[$n] = StringLen($aVR[$n][$aEnd[$n]])
            $new = StringTrimRight($aExecute[$n], $aDel[$n])
            $var = FileOpenDialog("Selecteer te starten item...", $new, "All (*.*)")
            IniWrite(@ScriptDir & "\menu.ini", StringFormat("BUTTON%02s", $n), "EXECUTE", $var)
            
            ; ...

        Next
EndSelect

A couple of points:

1. You don't need an array for variables that will simply be reused (i.e. $var above).

2. Note the StringFormat() to create the "BUTTON01" section name.

3. The example above uses the 0-based arrays, so $n = 0 thru 44. If you don't want a "BUTTON00" then adjust the code to declare the arrays as [46] and use 1 thur 45.

;)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

If you need 45 instances of something, keep them in an array:

Global $aVR[45] ; Use this array instead of $vr1 thru $vr45
Global $aEnd[45] ; Use this array instead of $end1 thru $end45
Global $aDel[45] ; Use this array instead of $del1 thru $del45
Global $aExecute[45] ; Use this array instead of $execute1 thru $execute45

; ...

Select
    Case $msg = $OptionsMenu_button01_browse
        For $n = 0 To UBound($aVR) - 1
            $aVR[$n] = StringSplit($aExecute[$n], "\")
            $aEnd[$n] = $aVR[$n][0]
            $aDel[$n] = StringLen($aVR[$n][$aEnd[$n]])
            $new = StringTrimRight($aExecute[$n], $aDel[$n])
            $var = FileOpenDialog("Selecteer te starten item...", $new, "All (*.*)")
            IniWrite(@ScriptDir & "\menu.ini", StringFormat("BUTTON%02s", $n), "EXECUTE", $var)
            
            ; ...

        Next
EndSelect

A couple of points:

1. You don't need an array for variables that will simply be reused (i.e. $var above).

2. Note the StringFormat() to create the "BUTTON01" section name.

3. The example above uses the 0-based arrays, so $n = 0 thru 44. If you don't want a "BUTTON00" then adjust the code to declare the arrays as [46] and use 1 thur 45.

;)

This looks promising, I will try to master this.

Thanx!!!

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...