Sign in to follow this  
Followers 0
JonatanRaven

Variables and Loops

4 posts in this topic

I'm pretty new at scripting so I'm just trying to wrap my head around some functions.

I'm trying to figure out how to accomplish a simple script that when executed does something a number of times using two global variables which I can easily change should I want to. The problem arises when I want the first loop to be slightly different.

$current = 0
$target = 5

If $current = 0 Then
    Call ("First")
    $current = $current + 1
ElseIf $current < $target Then
    Call ("Loop")
    $current = $current + 1
Else
    Call ("Finish")
EndIf
    
Func First ()
    MsgBox(4096,"","First Loop")
    Sleep (15000)
EndFunc

Func Loop ()
    MsgBox(4096,"","Loop")
    Sleep (15000)
EndFunc

Func Finish ()
    Sleep (2000)
    Beep(500, 1000)
    Exit
EndFunc

I started playing around with global variables with the following earlier variant but I couldn't figure out how to make an exception for the first loop.

$current = 0
$target = 5

Do
  Call ("Loop")
  $current = $current + 1
Until $current = $target

Func Loop ()
    MsgBox(4096,"","Loop")
    Sleep (5000)
    
EndFunc

Sleep (2000)
Beep(500, 1000)

Exit

I've been looking in the help pages and trying to find threads about this but I just can't get it to work the way I expect it to.

Any pointers?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Something like this:

$iNumberOfLoops = 5
For $iIndex = 1 to $iNumberOfLoops
  If $iIndex = 1 Then
    ; do something for the first time
  Else
    ; do something else for all other loops
  EndIf
Next

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Ah, thank you. Sometimes you get stuck in the tracks trying to do something.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Ok, new question, related to my first question. I've made a small GUI with an input field and a checkbox. Depending on what the input in the field is and whether or not the checkbox is checked, I want the script to execute slightly different. It works as intended as I've written it but I'm curious as to whether there is any way to trim this code down or organize it in any other way?

Func _btn_ok()
; ### No amount selected or 0
If GUICtrlRead ($Input1) < 1 Then
  $sText = "Cannot execute 0"
  _ExtMsgBox (16,0, $sTitle, $sText,0,@DesktopWidth -270, @DesktopHeight -200)
Else
  Select
   Case GUICtrlRead ($Checkbox) = $GUI_CHECKED
    Select
; ### Checkbox checked, 1 loop
    Case GUICtrlRead ($Input1) =1
     $sText = "Do you want to execute " & GUICtrlRead ($Input1) & " time?" & @CRLF & "Checkbox checked!"
     _ExtMsgBox (32,0, $sTitle, $sText,0,@DesktopWidth -270, @DesktopHeight -200)
; ### Checkbox checked, more than 1 loop
    Case Else
     $sText = "Do you want to execute " & GUICtrlRead ($Input1) & " times?" & @CRLF & "Checkbox checked!"
     _ExtMsgBox (32,0, $sTitle, $sText,0,@DesktopWidth -270, @DesktopHeight -200)
    EndSelect
   Case Else
    Select
; ### Normal loop, only once
    Case GUICtrlRead ($Input1) =1
     $sText = "Do you want to execute " & GUICtrlRead ($Input1) & " time?"
     _ExtMsgBox (32,0, $sTitle, $sText,0,@DesktopWidth -270, @DesktopHeight -200)
; ### Normal loop, more than once
    Case Else
     $sText = "Do you want to execute " & GUICtrlRead ($Input1) & " times?"
     _ExtMsgBox (32,0, $sTitle, $sText,0,@DesktopWidth -270, @DesktopHeight -200)
    EndSelect
   EndSelect
EndIf
EndFunc

The script only really changes depending on the checkbox other than the number of repetitions. The only reason for this setup is that the message box displayed before starting the script changes.

Edit: I changed the structure a bit and here's what I'm trying now and it seems to work.

Func _btn_ok()
$sText1 = "Cannot execute 0"
$sText2 = "Do you want to execute " & GUICtrlRead ($Input1) & " time?"
$sText3 = "Do you want to execute " & GUICtrlRead ($Input1) & " times?"
$sText4 = @CRLF & "Checkbox checked!"
If GUICtrlRead ($Input1) < 1 Then
  _ExtMsgBox (16,0, $sTitle, $sText1,0,@DesktopWidth -270, @DesktopHeight -200)
Else
  Select
  Case GUICtrlRead ($Checkbox) = $GUI_CHECKED
   Select
   Case GUICtrlRead ($Input1) =1
    _ExtMsgBox (32,0, $sTitle, $sText2  & $sText4,0,@DesktopWidth -270, @DesktopHeight -200)
   Case Else
    _ExtMsgBox (32,0, $sTitle, $sText3  & $sText4,0,@DesktopWidth -270, @DesktopHeight -200)
   EndSelect
  Case Else
   Select
   Case GUICtrlRead ($Input1) =1
    _ExtMsgBox (32,0, $sTitle, $sText2,0,@DesktopWidth -270, @DesktopHeight -200)
   Case Else
    _ExtMsgBox (32,0, $sTitle, $sText3,0,@DesktopWidth -270, @DesktopHeight -200)
   EndSelect
  EndSelect
EndIf
EndFunc

Any thoughts?

Edited by JonatanRaven

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