Home   Archive   Permalink



create block names dynamically

how I can create dynamically the names for my variables or blocks? Thanks!

posted by:   vector     6-Aug-2011/22:55:24-7:00



Something like this should work:
    
to-word join "a" "b"
    
What do you mean by a name for a block?

posted by:   Kaj     7-Aug-2011/9:30:45-7:00



Sorry for my english.. what I mean is if is posible to rename a block once have been created.
I want to create a series of blocks programatically and asign to them a name based on a variable. Thanks for answer!

posted by:   vector     7-Aug-2011/13:40:09-7:00



A block doesn't have a name. A block is a value, and a name is a word. You can assign and reassign values to words any way you like.
    
What you're proposing sounds too complex, though. Why use separate variable names? Why not use an object or a hash?
    
[name1: [block 1] name2: [block 2]]

posted by:   Kaj     8-Aug-2011/15:42:28-7:00



I did it!!.. Rebol is great!!... this is what I wanted:
num: 1
set (make set-word! ( append "block" (get 'num))) [ test ]
Thank you! your input guide me in my research...


posted by:   Vector     9-Aug-2011/16:39:07-7:00



Congratulations. :-) This is simpler:
    
num: 1
set to-word join "block" num [test]
    
It can also be executed multiple times. In your code, "block" gets ever longer with appended numbers.

posted by:   Kaj     9-Aug-2011/19:21:24-7:00



You could also do this:
    
myblock: [1 2 3]
newlabel: "test"
do rejoin [newlabel ": copy " mold myblock]
test
    
You can concatenate any text, and then "do" the concatenated text. Super simple metaprogramming :)

posted by:   Nick     10-Aug-2011/9:20:51-7:00



count: 1
foreach day system/locale/days [
     do rejoin ["var" count ": " mold day]
     count: count + 1
]
for i 1 (length? system/locale/days) 1 [
     do rejoin ["print var" i]
]
print var3
print var4

posted by:   Nick     10-Aug-2011/9:33:10-7:00



Don't get thrown by the "mold" function. The following lines both do the same thing:
    
do rejoin [newlabel ": copy " mold myblock]
do rejoin [newlabel {: copy "} myblock {"}]

posted by:   Nick     10-Aug-2011/9:41:42-7:00



Very good answers :)
I just wanted to warn about the number of global words is limited in REBOL.
So be careful using those methods, do not create thousands of word. If you really need that, put those words in an object context using BIND.
    
     >> repeat i 50000 [set to-set-word ajoin ["i" i] i]
     ** Internal Error: No more global variable space
    


posted by:   Endo     10-Aug-2011/12:59:24-7:00