Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Send a Text String that contains an Increasing Variable.



  • Please log in to reply
8 replies to this topic
MassiveDynamic
  • Members
  • 11 posts
  • Last active: Aug 02 2013 04:05 AM
  • Joined: 22 Jun 2013

Problem:

Cannot figure out how to Send a text string that contains X, where X is an expression containing n+1, with n being a number from 0 to infinity. Also loop said function for a predetermined amount of times.

 

Further clarification of query:

Let n= 0-infinity

Let x= n+1

Let y= "Count "

Loop # {Send %y%%x% {Enter}, Notepad}

 

So it results in:

Count 1

Count 2

Count 3

Count 4...

etc

 

My attempt that yielded undesired results:

 

Winactivate Untitled - Notepad

sleep 3000

x:=1++; I believe this is how you express 1+n? Not sure what this type of variable is called. My script vocab is very limited.

Loop 100{

send Count %x% {enter}

}

 

Thanks for any assistance.



Alpha Bravo
  • Members
  • 1687 posts
  • Last active: Nov 07 2015 03:06 PM
  • Joined: 01 Sep 2011
✓  Best Answer

use the built-in variable A_index

Winactivate Untitled - Notepad
WinWaitActive Untitled - Notepad
Loop 100
	send Count %A_Index% {enter}
return


MassiveDynamic
  • Members
  • 11 posts
  • Last active: Aug 02 2013 04:05 AM
  • Joined: 22 Jun 2013

Ahh. Thank you for the prompt reply. The %A_Index% is giving me the desired result =). One small question though, as I am still unsure of how Return works. I know that the description says it causes the script to return from a "side quest" (as i understand it). I dont understand the use of Return in the example you have posted.

Is return needed after a Loop? or is return inside the Loop?



kon
  • Members
  • 1652 posts
  • Last active:
  • Joined: 04 Mar 2013

The return is outside the loop. The code posted by Alpha Bravo does not require brackets around the loop because the loop is only one line, but it is identical in functionality to this:

Winactivate Untitled - Notepad
WinWaitActive Untitled - Notepad
Loop 100
{
    send Count %A_Index% {enter}
}
return

Also here is an example which may be useful to illustrate the function of a "return":

n := 5
x := n + 1
y := "Count "
return        ;Upon launching, the script will execute the lines of code up to this point and then will wait for the hotkeys to be pressed (or some other action like a timer)
MsgBox, This line will never be executed.


^1::    ;Ctrl+1 to start first method
Loop, 10
{
    Send, Count %A_Index% {Enter}
    Sleep, 500
}
MsgBox, End of Ctrl+1 hotkey subroutine.
return


^2::    ;Ctrl+2 to start the second method
Loop, 10
{
    Send, %y% %x% {Enter}
    x ++        ;or you could use "x += 1", or "x := x + 1"
    Sleep, 500
}
MsgBox, End of Ctrl+2 hotkey subroutine.
return

http://l.autohotkey...._Persistent.htm may also be of interest.



MassiveDynamic
  • Members
  • 11 posts
  • Last active: Aug 02 2013 04:05 AM
  • Joined: 22 Jun 2013

Thanks for the tip, Kon. The test script is pretty nice. I was gonna ask for a way to start the counting from a predetermined number instead of just 1.

Your  ^2 does the job, but I'm not sure why.

 

1. What does x++ mean?

2. Did you mean that x++ can be replaced by either x += 1 or x:= x+1

 

n:=5; <<<why is this an expression? as opposed to just "="

x:= n+1

 

Uncertain on how these work:

x++ (needs space?) and does this need to be below %x%? Im guessing the order of operation goes like this:

x++ = { %x% = 5+1+0, %x% = 5+1+1, %x% = 5+1+2...} read that since ++ is after Var, Var first results in its original value (5+1), and then the +1 is applied on the second execution of n+1

 

+= ;I didn't quite understand the description about this operator ?_?

 

x := x+1

x := x+1 = (n+1)+1 = 7 ?_?



kon
  • Members
  • 1652 posts
  • Last active:
  • Joined: 04 Mar 2013

All three examples add 1 to x. (The number stored in the x variable is increased by 1. So if the value of x is 5, then the new value of x after any of the following lines are executed will be 6.)

  • x ++  
  • x += 1
  • x := x + 1

Storing values in variables: To store a string or number in a variable, there are two methods: traditional and expression. The traditional method uses the equal sign operator (=) to assign unquoted literal strings or variables enclosed in percent signs. For example:
MyNumber = 123
MyString = This is a literal string.
CopyOfVar = %Var% ; With the = operator, percent signs are required to retrieve a variable's contents.

By contrast, the expression method uses the colon-equal operator (:=) to store numbers, quoted strings, and other types of expressions. The following examples are functionally identical to the previous ones:
MyNumber := 123
MyString := "This is a literal string."
CopyOfVar := Var ; Unlike its counterpart in the previous section, percent signs are not used with the := operator.
The latter method is preferred by many due to its greater clarity, and because it supports an expression syntax nearly identical to that in many other languages.

Quote Source



MassiveDynamic
  • Members
  • 11 posts
  • Last active: Aug 02 2013 04:05 AM
  • Joined: 22 Jun 2013

After further tweaking with your script, I think I've found the "process" that the variables go through to get the result:

 

a. x := n+1 was treated the same as x :=6 or x = 6. So x goes into Loop as 6.

b. Send writes 6 where %x% is

c. 6 goes into x++ and exits as 7.

d. 7 returns to the top of the loop; Send writes 7....so on and so forth.

 

So my question now is: why did you separate 5 and 1 with n:=5 and x:=n+5 as opposed to just doing x := 5?

I think that part is what confused me a little.

 

Anyways, thanks for the assistance.



kon
  • Members
  • 1652 posts
  • Last active:
  • Joined: 04 Mar 2013

So my question now is: why did you separate 5 and 1 with n:=5 and x:=n+5 as opposed to just doing x := 5?


Yes I could have just said x := 5, but I was trying to show an example relating to your original question, and to show the various ways of manipulating a variable.

Let n= 0-infinity
Let x= n+1
Let y= "Count "
...



MassiveDynamic
  • Members
  • 11 posts
  • Last active: Aug 02 2013 04:05 AM
  • Joined: 22 Jun 2013

Ahh, ok, I was going nuts tweaking the variables thinking that the result from Loop returns to the top of the script to cycle through the variables again. Then i figured it out... haha. Lesson learned, the hard way =)