AutoHotkey Community

It is currently May 26th, 2012, 10:39 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Dynamic Label?
PostPosted: March 11th, 2009, 12:42 am 
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.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 11th, 2009, 12:52 am 
Offline

Joined: March 9th, 2007, 2:47 am
Posts: 509
Location: Unknown
There may be another way if you tell us what you want to do.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 12th, 2009, 2:41 am 
Offline

Joined: May 23rd, 2009, 4:48 am
Posts: 361
Location: north bay, california
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 13th, 2009, 5:14 pm 
Offline

Joined: November 1st, 2007, 10:03 pm
Posts: 885
Bump


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 14th, 2009, 1:33 am 
Offline

Joined: April 4th, 2008, 8:15 pm
Posts: 538
Location: Canada
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:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 14th, 2009, 5:11 am 
Offline

Joined: May 23rd, 2009, 4:48 am
Posts: 361
Location: north bay, california
hehe, nice


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 16th, 2009, 8:27 am 
Offline

Joined: May 23rd, 2009, 4:48 am
Posts: 361
Location: north bay, california
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 25th, 2010, 6:16 pm 
Offline

Joined: November 24th, 2008, 7:22 pm
Posts: 73
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 25th, 2010, 9:49 pm 
Offline

Joined: November 24th, 2008, 7:22 pm
Posts: 73
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:   


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

All times are UTC [ DST ]


Who is online

Users browsing this forum: JSLover, Miguel, rbrtryn, XstatyK, Yahoo [Bot] and 58 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