AutoHotkey Community

It is currently May 27th, 2012, 12:12 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: April 29th, 2011, 5:51 am 
Offline

Joined: March 24th, 2004, 2:34 pm
Posts: 299
I'm embarassed to admit this, but I have a problem when trying to understand how to code parameters in an AHK command I never used before.

For example, today, I had to use the StringReplace command for the first time. This command searches an input text string for a specific substring and replaces each occurrence with a replacement string. The result is stored in an output string. For example, suppose you want to search the variable "Input_String" and replace every occurrence of the substring "cat" with "mouse". There are basically four parameters to this command.

1) Input_Str - a variable containing the input character string - perhaps "big cat small cat abc cat"
2) Output_Str - a variable containing the output character string that will contain the result of the operation - e.g. "big mouse small mouse abc mouse"
3) Target_Str - a variable containing the search string - in this case "cat"
4) Replace_Str - a variable contining the replacement string - in this case "mouse"

My first attempt to code this command was:

StringReplace, Input_Str, Output_Str, Target_Str, Replace_Str, All

The last parameter can be "All" if you want to replace all occurrences of the target string or "1" if you only want to replace the first occurrence.

Unfortunately, coding the command this way does not work. It has to be coded as:

StringReplace, Input_Str, Output_Str, %Target_Str%, %Replace_Str%, All

I find this very difficult to grasp. It seems like every time I use a new command, I have to figure out which parameters can be coded using the name of a variable and which must contain the name of the variable enclosed in percent signs.

I understand why this is so. It has to do with the use of expressions and the fact that when you use a command some parameters can be forced to appear as expressions.

But, it just takes me a long time to grasp the use of any new AHK command and I'd like to ask for some help in doing this.

When you look at an AHK command for the first time, how do you know which parameters can appear as the names of variables and which parameters need to appear surrounded by percent signs?

I'd like to find an easy way to know how to proceed with an AHK command for the first time. I seem to keep getting stymied with this problem time after time and I'd really like to know how to avoid spending a long time figuring out how to use a new AHK command before I can use it.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 29th, 2011, 6:08 am 
Offline

Joined: May 12th, 2005, 8:20 am
Posts: 331
Location: Münster, Germany
Hi, JDN,
I understand your problem very well, me myself stumble into this "percent-sign-or-not"-dilemma again and again.
As a little help try to think like this:
Differentiate the variables into two groups:
The first group are these variables, the function works with and stores results in => the first and (especially) the second of "StringReplace".
The second group are variables, the function works with their content => third and fourth variable of "StringReplace".
Could this help you?
Regards,
Klaus


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 29th, 2011, 6:18 am 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5482
Location: the tunnel(?=light)
JDN wrote:
When you look at an AHK command for the first time, how do you know which parameters can appear as the names of variables and which parameters need to appear surrounded by percent signs? ...I'd like to find an easy way to know how to proceed with an AHK command for the first time.


Here's a general rule of thumb for AHK commands: If a command's parameter will accept only a number, then it will support an expression automatically. Otherwise you must force an expression or dereference variables with percent signs as needed (or as allowed).

Now don't let the Click command fool you on the above rule. Click is actually a single-parameter command, so since you can use commas and text in that single parameter it thus does not support expressions automatically.

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 29th, 2011, 7:51 am 
Offline

Joined: October 2nd, 2009, 12:43 pm
Posts: 283
http://www.autohotkey.com/docs/commands ... eplace.htm
Quote:
OutputVar: The name of the variable
InputVar: The name of the variable
SearchText: The string to search for.
ReplaceText: SearchText will be replaced with this text

See the difference?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 29th, 2011, 12:56 pm 
Offline

Joined: March 24th, 2004, 2:34 pm
Posts: 299
Thank you all very much for your replies.

I found them to be very helpful.

Most of all, the knowledge that I am not alone and that others have experienced this same problem.

I will save the info in your posts and will use it when I encounter new AHK commands in the future.

Thanks again!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 29th, 2011, 7:09 pm 
Offline

Joined: March 10th, 2011, 7:17 pm
Posts: 374
this problem is not limited to beginners

ive used ahk for 2 years and still run into this


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 29th, 2011, 8:11 pm 
Offline

Joined: March 24th, 2004, 2:34 pm
Posts: 299
Ha Ha!

I've used AHK since it first came out and run into this problem - not only every time I try to use an AHK command for the first time, but also if I haven't used a command for a few months and then try to use it again, I forget almost everything I once learned about it and have to start from scratch again.

Still, I wouldn't trade using AHK for anything.

I program in Delphi, C and other programming languages.

But I consider AHK to be the most valuable of them all.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 29th, 2011, 8:19 pm 
Offline

Joined: December 26th, 2010, 7:40 pm
Posts: 4172
Location: Awesometown, USA
I have the solution!
A command is generally listed as:
Code:
Command, outputvar, param1, [param2, param3]

Whenever it says "Var", that means that you pass the name of the variable. This is because autohotkey is working with that Variable. Possibly modifying it.The other parameters expect literal text. So to give it the literal text inside a variable, you need to "deference" it with %%.

A function is generally listed as:
Code:
ReturnValue := function(Param1, Param2)

A function doesn't need outputvars (usually) because it returns a value. It is in "expression mode", where literal strings are quoted and variables are automatically deferenced.

This is how I have conceptualized AutoHotkey, and I never run into any syntax problems of this sort.

HTH

_________________
Autofire, AutoClick, Toggle, SpamWindow Control Tools
Recommended: AutoHotkey_L


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Google Feedfetcher, Yahoo [Bot] and 15 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group