 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Payam
Joined: 07 Apr 2004 Posts: 58
|
Posted: Mon Aug 02, 2004 9:41 am Post subject: Embedded variables |
|
|
Ok, this might be an already talked about limitation or I'm missing the correct way to do this.
itemtitle1 & itemtitle2 are variables.
Now, if in a loop I do this:
num=0 ;initialize
Loop,2
{
num++
title=%itemtitle%num%% ; <=== here is the problem
GoSub,gettitlecost
MsgBox, Cost of itemtitle%num% = %cost%
}
I get an AHK error:
---------------------------
determine_cost.ahk
---------------------------
Error at line 228.
Line Text: %itemtitle%num%%
Error: This parmeter contains an empty variable reference (%%).
The program will exit.
---------------------------
OK
---------------------------
Whats the workaround? Thanks as always. |
|
| Back to top |
|
 |
BoBo Guest
|
Posted: Mon Aug 02, 2004 11:30 am Post subject: |
|
|
title=%itemtitle%num%%
title=%itemtitle%%num%
Maybe you should have a look at this.  |
|
| Back to top |
|
 |
Chris Site Admin
Joined: 02 Mar 2004 Posts: 10467
|
Posted: Mon Aug 02, 2004 4:39 pm Post subject: |
|
|
| In case it's of use: To create a literal percent sign, precede it with the escape character: `% |
|
| Back to top |
|
 |
Ethan Guest
|
Posted: Tue Aug 03, 2004 7:40 am Post subject: |
|
|
Unless I'm mistaken what you're trying to do is use an array of items?
I made this same mistake when first trying to use an array. The problem comes from the fact that AHK doesn't parse such nested dereference characters since they're nondirectional (no open and close, it's just one character).
The solution seems to be to use a function that accepts an OutputVar (variable), such as StringTrimLeft, but actually does nothing. In other words for you it would be:
| Code: | | StringTrimLeft,title,itemtitle%num%,0 |
This puts the num element of the array in the title variable (trimming 0 characters from the left), which I think is your intention.
Chris, might I suggest some more elegent syntax to accomplish this, since apparently I'm not the only one to have trouble with it. Not sure quite how you'd want to do it though, and it's not really a big deal since the workaround is easy once you know what to do. One possibility would be paired dereference characters, but that would be a pain. Maybe the best would be to allow for blocks such as %{itemtitle%num%}%, at least in terms of being consistent with other syntax. |
|
| Back to top |
|
 |
Ethan Guest
|
Posted: Tue Aug 03, 2004 7:43 am Post subject: |
|
|
| Oh, and BTW, you don't need a separate num variable for the loop, since AHK contains the built in A_Index which contains the current loop iteration (indexing from 1). |
|
| Back to top |
|
 |
Chris Site Admin
Joined: 02 Mar 2004 Posts: 10467
|
Posted: Tue Aug 03, 2004 10:54 am Post subject: |
|
|
| Quote: | | Chris, might I suggest some more elegent syntax to accomplish this, since apparently I'm not the only one to have trouble with it. ... One possibility would be paired dereference characters, but that would be a pain. |
Yeah I've put a lot of thought into how to do some kind of "double dereference" exactly as you describe. Unfortunately, most of the cures seem worse than the disease (due to the introduction of confusing syntax). Still, it's on the to-do list so hopefully someday.
For reference, using arrays is described here. |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|