AutoHotkey Community

It is currently May 27th, 2012, 1:28 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: June 28th, 2005, 6:09 pm 
Offline

Joined: September 7th, 2004, 9:20 pm
Posts: 275
Location: France
The test function :
Code:
OneFunct(Param)

{
   msgbox %Param%
   Ret = The text is %Param%
   return Ret
}


The first text code is :
Code:
#v::
   TheText = This is the text
   Try := OneFunct(TheText)
   msgbox %Try%
   If Try contains is the text
      msgbox OK
   else
      msgbox not OK
   return

The msgboxes are :
1) This is the text (msgbox of the function)
2) The text is This is the text (msgbox %Try%)
3) OK (msgbox OK)
All is OK,
- I put "this is the text" in a variable (TheText)
- I affect the result of the function to another variable
- I made a test on this variable content.

Next test :
Code:
#w::
   TheText = This is the text
   If OneFunct(TheText) contains is the text
      msgbox OK
   else
      msgbox not OK
   return

The msgboxes are :
1) This is the text (msgbox of the function)
2) OK (msgbox OK)
All is OK,
- I put "this is the text" in a variable (TheText)
- I made a test using a direct function call

Next test :
Code:
#x::
   If OneFunct(This is the text) contains is the text
      msgbox OK
   else
      msgbox not OK
   return

The msgboxes are :
1) Nothing is displayed : blank msg (msgbox of the function)
2) OK (msgbox OK)
The result is OK but I am surprised because nothing is displayed in the first msgbox, so the result might be false, but it is OK. I don't know if it's a bug or if it is normal, but the result is OK...
- I made the test using a direct function call in the if statement and I didn't used a temp variable

Next test :
Code:
#y::
   If InStr(OneFunct(This is the text), is the text)
      msgbox OK
   else
      msgbox not OK
   return

The msgboxes are :
1) Nothing is displayed : blank msg (msgbox of the function)
2) OK (msgbox OK)
The result is the same as before.

Last test :
Code:
#z::
   IfInString, OneFunct(This is the text), is the text
      msgbox OK
   else
      msgbox not OK
   return

I have the following error message :
Quote:
Error at line xx.
Line Text: OneFunct(This is the text)
Error: This variable or function name contains an illegal character.


I don't know why the code is (almost) OK with :
Code:
If OneFunct(This is the text) contains is the text
and with :
Code:
If InStr(OneFunct(This is the text), is the text)
but not with :
Code:
IfInString, OneFunct(This is the text), is the text


To be honest, the code :
Code:
#z::
   TheText = This is the text
   IfInString, OneFunct(TheText), is the text
      msgbox OK
   else
      msgbox not OK
   return
give the error message :
Quote:
Error at line xx.
Line Text: OneFunct(TheText)
Error: This variable or function name contains an illegal character.


IfInString bug ?
Function bug (nothing dispalyed in the message box when I don't do the function call using a variable as parameter) ?
Things to think about them at least, I suppose...

What do you think about that, Chris ?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 29th, 2005, 12:48 am 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
You can only call functions from the parameters of commands that explicitly allow them. For example, IfInString doesn't support them. You can work around this by specifying "% " (percent sign and space) as the first characters in a non-expression parameter (this can be used in expression parameters too, since it's easy to forget which parameters support functions).

Also, you might already know this but strings passed to a function should be enclosed in quotes. For example: OneFunct("This is the text")

After applying this info, please post again if there is any remaining suspicious behavior. Also, I apologize in advance if I completely missed the point of your post. It's rather long so I didn't scrutinize every detail.

Thanks.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 29th, 2005, 6:58 pm 
Offline

Joined: September 7th, 2004, 9:20 pm
Posts: 275
Location: France
For precsion I made the tests under XP.
Chris wrote:
Also, you might already know this but strings passed to a function should be enclosed in quotes. For example: OneFunct("This is the text")

I didn't remind that. I tried :
Code:
IfInString, OneFunct("This is the text"), is the text
and the result was :
Quote:
Error at line xx.
Line Text: OneFunct("This is the text")
Error: This variable or function name contains an illegal character.

I better RTFM and seen :
Quote:
Finally, functions may be called in the parameters of any command; but parameters that do not support expressions must use the "% " prefix as in this example: MsgBox % "The answer is: " . Add(3, 2)
So I tried :
Code:
IfInString, % OneFunct("This is the text"), is the text
and the result was :
Quote:
Error at line xx.
Line Text: % OneFunct("This is the text")
Error: This parameter contains a variable name missing its ending percent sign.
probably it isn't the right way to do the things. It is of course possible I didn't understood well.

I made other trys all negatives. I wanted to use a direct call to a function in place of the variable to have a more concise writing but I must absolutely pass by an intermediate variable to have a good result.

Code:
IfInString, The text is This is the text, is the text
is not OK
Code:
TheText := OneFunct("This is the text")
IfInString, TheText, is the text
is OK. The use of a variable is an obligation.

So may be it is not bug that
Code:
IfInString, OneFunct("This is the text"), is the text
don't do the things at all.

Perhaps it can be a wish ?

Thanks for your time, Chris.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 29th, 2005, 10:50 pm 
Quote:
So I tried:
Code:
IfInString, % OneFunct("This is the text"), is the text
Just a guess, but "% expression" can probably only be used as a command parameter that's expected to be a value, not a variable name. In the case of IfInString, the first parameter is expected to be a variable name, i.e. you can't even do
Code:
IfInString, This is some text, some

Jacques.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: June 30th, 2005, 12:50 am 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
Quote:
Just a guess, but "% expression" can probably only be used as a command parameter that's expected to be a value, not a variable name. In the case of IfInString, the first parameter is expected to be a variable name
You're absolutely right. I forgot about this when writing the documentation, so it is inaccurate.

I will correct the documentation to indicate that you can't use the "% expression" method for a parameter that is an input or output variable. Thank you both.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 30th, 2005, 1:09 am 
Offline

Joined: September 7th, 2004, 9:20 pm
Posts: 275
Location: France
Thank you two. But will it be possible (at very low priority level as there are other way to have the same result, to do :
Code:
IfInString, OneFunct("This is the text"), is the text
so to replace a variable name by a function call, as the result in the twice cases is ... a variable content (and not only in IfInString !!!!!!!!). Probably too much work !!!
Thanks for your explanations.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 30th, 2005, 2:18 am 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
You can try the InStr() function, which might do what you want.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 4th, 2005, 5:42 pm 
Offline

Joined: September 7th, 2004, 9:20 pm
Posts: 275
Location: France
Chris wrote:
You can try the InStr() function, which might do what you want.

Thanks but I understood .
Nemroth wrote:
I don't know why the code is (almost) OK with :
Code:
If OneFunct(This is the text) contains is the text
and with :
Code:
If InStr(OneFunct(This is the text), is the text)
but not with :
Code:
IfInString, OneFunct(This is the text), is the text


At first I thought it may be a bug but I wasn't sure of that.

Now I understand it isn't but I think it would be easier if it was possible to replace, in every command where varaibles are allowed or necessary, to replace them by a call to a function wiitch returns a variable, so to be able to write :
Code:
IfInString, OneFunct(This is the text), is the text
in place of :
Code:
TheText := OneFunct(This is the text)
IfInString, TheText, is the text


So ok it isn't any more a bug, it becomes a wish.

I understand it must be an important rewriting of the the code and I think it can't be a high priority, as we can do by an other way, but may be it wuld be a good thing. Thanks for your time.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 5th, 2005, 12:28 am 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
Thanks for the follow-up.

I'll keep these ideas in mind for a future version even though they might be difficult to do without breaking existing scripts.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 5th, 2005, 2:58 am 
Offline

Joined: September 7th, 2004, 9:20 pm
Posts: 275
Location: France
Chris wrote:
I'll keep these ideas in mind for a future version even though they might be difficult to do without breaking existing scripts.

Of course don't do that if it break scripts !!! But if it is possible to do that without breaking scripts, it can be a more concise way to write an AHK script.

Thanks for your attention.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 9th, 2005, 6:18 pm 
Offline

Joined: November 16th, 2004, 6:38 am
Posts: 153
Location: New York
Nemroth wrote:
I think it would be easier if it was possible to replace, in every command where varaibles are allowed or necessary, to replace them by a call to a function which returns a variable
Strictly speaking, as currently implemented, functions never return a variable, only a value. I think what you are actually requesting is the following: that commands that currently require an input variable as a parameter (i.e. a parameter the contents of which are used but not modified by the command) be updated to instead accept an expression (such as a function call or an explicit "quoted string") in place of the input variable. This seems like a pretty tall order, though I don't think it breaks existing syntax and therefore would be backward compatible (because a variable name, when treated as an expression, evaluates to the variable's contents, which is all that is required of an input variable -- again, this does not apply to output variables the contents of which are modified by the command).

Note: The only caveat to the backward compatibility claim is the possibility that some valid variable names can be constructed to look like expressions -- the rules for constructing valid variable names have always, it seems to me, been kind of "loose", probably the result of the AutoIt2 heritage where variables were in fact environment variables. For example, A+B (which looks like an expression) is a valid environment variable name and might (I haven't tested this) also be a valid AutoIt2/AutoHotkey variable name. [I apologize if the naming rules have been tightened up at some point while I wasn't looking! :-)]

Finally, a purely personal comment about this (and similar) requests: in the final analysis, the only benefit of such a feature would be the occasional elimination of the need for an additional line of code (to place an expression result into a temporary variable), at the expense of readability and code simplicity. Note that performance is unlikely to be improved since, internally, AHK still has to evaluate the expression and place the result "somewhere" before continuing with the command, so the extra line of code just makes this step explicit, i.e. you do a tiny amount of extra typing to keep Chris' life simpler! :-) Note that you can decide on a distinctive temporary variable name once and for all (e.g. @T) and keep re-using it in your code every time you need to pre-evaluate an expression, so instead of your
Code:
IfInString, OneFunct(This is the text), is the text
you would use
Code:
@T := OneFunct(This is the text)
IfInString, @T, is the text
and you'd re-use @T every time a similar need arises. I suggest this only because I suspect that sometimes the biggest annoyance in these situations is having to come up with temporary variable names, but since you obviously have no need to use the value again (or you wouldn't be asking for the expression to be able to be incorporated into the command), don't sweat the name!

Later,

Jacques.


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

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


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