AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Dynamic Label?

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
tired
Guest





PostPosted: Tue Mar 10, 2009 11:42 pm    Post subject: Dynamic Label? Reply with quote

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

PostPosted: Tue Mar 10, 2009 11:52 pm    Post subject: Reply with quote

There may be another way if you tell us what you want to do.
Back to top
View user's profile Send private message Visit poster's website
gwarble



Joined: 23 May 2009
Posts: 230
Location: north bay, california

PostPosted: Sat Sep 12, 2009 1:41 am    Post subject: Reply with quote

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
View user's profile Send private message
Fry



Joined: 01 Nov 2007
Posts: 885

PostPosted: Sun Sep 13, 2009 4:14 pm    Post subject: Reply with quote

Bump
Back to top
View user's profile Send private message
purloinedheart



Joined: 04 Apr 2008
Posts: 537
Location: Canada

PostPosted: Mon Sep 14, 2009 12:33 am    Post subject: Reply with quote

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


Wink
Back to top
View user's profile Send private message
gwarble



Joined: 23 May 2009
Posts: 230
Location: north bay, california

PostPosted: Mon Sep 14, 2009 4:11 am    Post subject: Reply with quote

hehe, nice
Back to top
View user's profile Send private message
gwarble



Joined: 23 May 2009
Posts: 230
Location: north bay, california

PostPosted: Wed Sep 16, 2009 7:27 am    Post subject: Reply with quote

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
View user's profile Send private message
Relayer



Joined: 24 Nov 2008
Posts: 68

PostPosted: Fri Jun 25, 2010 5:16 pm    Post subject: Reply with quote

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
View user's profile Send private message
Relayer



Joined: 24 Nov 2008
Posts: 68

PostPosted: Fri Jun 25, 2010 8:49 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group