 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
tired Guest
|
Posted: Tue Mar 10, 2009 11:42 pm Post subject: Dynamic Label? |
|
|
Is this possible?
I want to generate a random number for a label so it won't be used again, like:
Random, MYLABEL, 1, 99999
%MYLABEL%:
But you can't put variables into the label in that way. |
|
| Back to top |
|
 |
System Monitor
Joined: 09 Mar 2007 Posts: 509 Location: Unknown
|
Posted: Tue Mar 10, 2009 11:52 pm Post subject: |
|
|
| There may be another way if you tell us what you want to do. |
|
| Back to top |
|
 |
gwarble
Joined: 23 May 2009 Posts: 230 Location: north bay, california
|
Posted: Sat Sep 12, 2009 1:41 am Post subject: |
|
|
on a related note which may help him, i want something to the effect of:
| Code: |
Loop 5
NotifyKill%A_Index%: ;create 5 labels
;do something
Return
|
to replace:
| Code: |
NotifyKill1:
NotifyKill2:
NotifyKill3:
NotifyKill4:
NotifyKill5:
;do something
Return
|
same basic question, except i would know what numbers are being used (to randomize, you could create label1-label10 and save a variable array Random1=7628, Random2=2324, etc if we could get the above working)
any ideas??
thanks, gwarble |
|
| Back to top |
|
 |
Fry
Joined: 01 Nov 2007 Posts: 885
|
Posted: Sun Sep 13, 2009 4:14 pm Post subject: |
|
|
| Bump |
|
| Back to top |
|
 |
purloinedheart
Joined: 04 Apr 2008 Posts: 537 Location: Canada
|
Posted: Mon Sep 14, 2009 12:33 am Post subject: |
|
|
Caveman Attempt
Script 1
| Code: |
Loop 5
Dynamic .= "Label" A_Index ":`n"
FileAppend %Dynamic%, Dynamic.txt
FileAppend Msgbox Caveman Worked!`nMsgbox Write rest of script here, Dynamic.txt
|
Script 2
| Code: |
#include Dynamic.txt
Goto label1
|
 |
|
| Back to top |
|
 |
gwarble
Joined: 23 May 2009 Posts: 230 Location: north bay, california
|
Posted: Mon Sep 14, 2009 4:11 am Post subject: |
|
|
| hehe, nice |
|
| Back to top |
|
 |
gwarble
Joined: 23 May 2009 Posts: 230 Location: north bay, california
|
Posted: Wed Sep 16, 2009 7:27 am Post subject: |
|
|
how about a new AHK command, Label, which acts just like HotKey,
| Code: | Loop 5
Label, DynamicLabel%A_Index%, DynamicLabel
DynamicLabel:
MsgBox, % A_ThisLabel
Return |
yeah, obviously all five dynamically created variables would run the same label "sub", but if the A_ThisLabel variable was updated properly it would solve some reasons for this requested ability, at least for me
i don't know how ahk internally deals with labels which is preventing something like this... but if the list of labels is created once at script start, a way to specify a range of possible dynamic labels may be necessary...
- gwarble |
|
| Back to top |
|
 |
Relayer
Joined: 24 Nov 2008 Posts: 68
|
Posted: Fri Jun 25, 2010 5:16 pm Post subject: |
|
|
I just thought of a good use for dynamic labels. This is one example only; there may be others.
I like this version of a switch:
| Code: | Loop 1 ; switch
{
Goto % IsLabel("Case(" . foo . ")") ? "Case(" . foo . ")" : "Case_Default"
Case(a):
bar := 10
break
Case(b):
bar := 11
break
Case(c):
bar := 12
break
Case_Default:
letter =
break
} |
It would be convenient to place a token (syntax TBD) that would be automatically preprocessed to assign unique labels before the script is actually run. For example, "Case_Default" would become "Case[token]_Default". Token would be replaced with a unique number during preprocessing so that each instance of a switch would generate guaranteed unique labels and free the writer to simply use a common syntax for all instances. |
|
| Back to top |
|
 |
Relayer
Joined: 24 Nov 2008 Posts: 68
|
Posted: Fri Jun 25, 2010 8:49 pm Post subject: |
|
|
OK, I just went ahead and did it...
Here is the preprocessor. It takes a .ahk file passed to it and assigns random numbers to all places containing a token, saves it to a temp file and runs it before killing itself.
| Code: |
#NoEnv
If (%0% > 0)
{
in = %1%
PreProcess(in)
}
ExitApp
PreProcess(file)
{
token := "[rn]"
Random, rnum, 0
FileDelete, %A_ScriptDir%\tmp.ahk
Loop, Read, %A_ScriptDir%\%file%
{
OldLine := A_LoopReadLine
IfInString, OldLine, %token%
StringReplace, NewLine, OldLine, %token%, %rnum%, All
else
NewLine := OldLine
FileAppend, %NewLine%`n, %A_ScriptDir%\tmp.ahk
IfInString, OldLine, Case%token%_Default:
Random, rnum, 0 ;generate new random number for next switch
}
Run, %A_ScriptDir%\tmp.ahk
} |
So, running the preprocessor takes this:
| Code: |
Loop 1 ; switch
{
Goto % IsLabel("Case[rn](" . foo . ")") ? "Case[rn](" . foo . ")" : "Case[rn]_Default"
Case[rn](a):
foo := 10
break
Case[rn](b):
foo := 11
break
Case[rn](c):
foo := 12
break
Case[rn](d):
foo := 13
break
Case[rn](e):
foo := 14
break
Case[rn](f):
foo := 15
break
Case[rn]_Default:
break
}
Case[rn]_Default:
Case[rn]_Default:
Case[rn]_Default:
Case[rn]_Default: |
... and turns it into this
| Code: |
Loop 1 ; switch
{
Goto % IsLabel("Case1097872240(" . foo . ")") ? "Case1097872240(" . foo . ")" : "Case1097872240_Default"
Case1097872240(a):
foo := 10
break
Case1097872240(b):
foo := 11
break
Case1097872240(c):
foo := 12
break
Case1097872240(d):
foo := 13
break
Case1097872240(e):
foo := 14
break
Case1097872240(f):
foo := 15
break
Case1097872240_Default:
break
}
Case1504552248_Default:
Case1570426814_Default:
Case1402824234_Default:
Case1803565540_Default: |
|
|
| 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
|