Jump to content
Sign in to follow this  
filet

Do Until String Comparison

Recommended Posts

filet

Im trying to write a do until loop statement comparing one string to another string plus a number.

For example:

Do .......

Until $string2 = ($string1 + 25)

I can't quite get the syntax.

I want the loop to stop when $string2 is equal to $string1 plus 25.

Share this post


Link to post
Share on other sites
czardas

; Example 1 - Adding numbers using the += operator
Dim $iNumber = 0 ; Declare a number equal to zero
Do ; Start of loop
    $iNumber += 1 ; Add increments to the number
Until $iNumber = 25
MsgBox(0, "Number", $iNumber)
 
; Example 2 - Concatenating strings using the & operator
Dim $sString = "" ; Declare an empty string
Dim $sVar = "String" ; Declare a string with 6 characters
$iNumber = 0 ; The number suffix to be concatenated at the end of the string
 
While $sString <> "String25" ; Loop while the values are different from each other
    $sString = $sVar & $iNumber ; Concatenate the string
    $iNumber += 1 ; Add increments to the number
WEnd
MsgBox(0, "String", $sString)

Be careful when working with loops. You may write something that produces an infinite loop. If that happens you will need to stop the script from running manually from the task bar or from inside SciTE editor. I would avoid automating the keyboard or mouse inside a loop until you know more about what you are doing. I made that mistake in the beginning and crashed my PC on several occasions.

Edited by czardas

Share this post


Link to post
Share on other sites
filet

I don't want a number loop. I just want to know the correct way to write = $string + 25

I want the loop to stop running when $string2 is a number that is 25 higher than $string1

Example $string 1 is 100

$string2 is 124 - loop still running

$string2 is 125 - loop stops running

Share this post


Link to post
Share on other sites
czardas

Don't confuse numbers and strings. You can't add 25 to a string, unless the string is already a number. Once you add a number to a string of digits it is automatically converted to a number, which is not the same as a string. The examples were intended to illustrate methods of working with loops. Not necessarilly a solution to your particular problem. Without knowing how the number becomes 125 it is impossible write the correct code.

$string = 0
$what = 0
Do
    $what += 25
Until $what = $string + 25
Edited by czardas

Share this post


Link to post
Share on other sites
somdcomputerguy

I want the loop to stop running when $string2 is a number that is 25 higher than $string1

Like this?

Local $num1 = 0, $num2 = 0

Do
    ConsoleWrite($num2 & " ")
    $num2 += 1
Until $num2 > ($num1 + 25)
ConsoleWrite(" Done!" & @CRLF)

But take note of what czardas said in the post above about strings and numbers!

Edited by somdcomputerguy

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites
filet

This is the way I wrote the line

~ until $secondtotal = ($firsttotal + 25)

I get an error back that says unable to parse line. Both $secondtotal and $firsttotal are declared and the rest of the code works fine. I suspect the syntax is wrong, but I'm not sure how to change it. I tried several combinations and I keep getting the same error message.

Obviously I'm very new at this, but the rest of the program works fine.

Share this post


Link to post
Share on other sites
czardas

There's nothing wrong with the syntax. The error is occuring elsewhere in the script. The interpreter gets stuck on that line because of an error elsewhere in your code. Run this and you'll see the syntax is fine.

$firsttotal = 100
$secondtotal = 0
Do
    $secondtotal += 1
Until $secondtotal = ($firsttotal + 25) ; Nothing wrong with this line
MsgBox(0, "This works for me", $secondtotal)

You can also remove the brackets and it still works. You will need to post more of your code for anyone to help you with this. Also it's a good idea to use the AutoIt code box to post your code. That's the blue square button with 'A' written on it.

Edited by czardas

Share this post


Link to post
Share on other sites
kaotkbliss

I would do

until $secondtotal >= ($firsttotal + 25)

that way, if whatever you add to secondtotal brings it higher then 25 (say 26+) then the loop will still stop


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!

Share this post


Link to post
Share on other sites
filet

Here is the code for the loop portion: I changed the name of the website.

Do

Send("#r")

;WinWaitActive("Run")

Send("http://www.website") ;open Demand Studios Electrical

Send("{enter}")

Sleep (3000)

Send("{ALT}") ;opens source file

Send("{RIGHT 2}")

Send("{DOWN 12}")

Send("{ENTER}") ; opens source file

Sleep (3000) ; two second delay

Send("{CTRLDOWN }") ; select all of source

Send("{A}")

Send("{A}") ; DUPLICATE NOT NEEDED

Send("{C}") ; copy source code

Send("{CTRLUP}")

Send("{ENTER}") ; copy source code

Sleep (4000)

$handle = WinGetHandle ("")

winclose ($handle)

Sleep (4000)

$handle = WinGetHandle ("")

winclose ($handle)

Sleep (4000)

Send("{CTRLDOWN }") ;opens notepad

Send("{ALTDOWN }")

Send("{n}")

Send("{CTRLUP }")

Send("{ALTUP}") ;opens notepad

Sleep (4000)

Send("{CTRLDOWN}") ; pastes source to notepad

Send("{V}")

Send("{CTRLUP}") ; pastes source to notepad

Sleep (5000)

Send("{ALT}")

Send("{DOWN 3}")

Send("{ENTER}")

Sleep (3000)

Send("C:\Users\phil\Desktop\sourcetest.txt") ; saves source to notepad file

Sleep (3000)

Send("{ENTER}")

Send("{LEFT}")

Send("{ENTER}") ; saves source to notepad file

Sleep (3000)

$handle = WinGetHandle ("")

winclose ($handle)

#include<array.au3> ; finds total and puts in array

$re = StringRegExp(FileRead('C:\Users\phil\Desktop\sourcetest.txt'), 'Total.*?\:graduated:\d+)', 3)

;_ArrayDisplay($re)

$secondtotal = $re[0]

;MsgBox (0, "", $firsttotal) ; displays total string in message box

~ until $secondtotal >= ($firsttotal + 25)

Here are the declarations that are at the beginning of the code.

dim $firsttotal

dim $secondtotal

dim $handle

Share this post


Link to post
Share on other sites
BrewManNH

You have this ~ until, you need to lose the "~" from in front of the until. I'm assuming this is your actual code because you've used it with the "~" twice so far, so it appears your syntax error is because of that.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
filet

Thank you. It was so small I didn't even notice it. I didn't type it . I'm not sure how it got there, but that was the problem.

Share this post


Link to post
Share on other sites
BrewManNH

It could have been inserted when you commented the line out in SciTE because Scite uses ;~ to indicate a commented line from the tools menu, removing the comment character ";" and not the tilde would be an easy thing to miss.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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  

×