I know this does _not_ answer your question, but are you really sure you want to do this? Can't you just push your variables to an array, and access them as x[1], x[2], ... ?
On Tuesday, November 3, 2015 at 2:40:34 PM UTC+1, [email protected] wrote: > > I can't work out the syntax for creating symbols and assigning values to > the variables in a loop. Here's a simple example: > > Starting with this basic idea: > > julia> for n in 1:10 > println("x_$(n)") > end > > I'd like to do this: > > julia> for n in 1:10 > symbol("x_$(n)") = n > end > ERROR: syntax: "(string #<julia: "x_"> n)" is not a valid function > argument name > > to create variables called x_1, x_2, and assign numeric values to them... > (Numbers in this example, but will be image data eventually.) > > Also, for better formatting for variable names: > > julia> "x_$(@sprintf "%03d" 2)" > "x_002" > > but this gives a similar error when used with symbol(): > > julia> for n in 1:10 > symbol("x_$(@sprintf "%03d" 2)") = n > end > > ERROR: syntax: "(string #<julia: "x_"> (let (block (= #21#out (call (. > #0=#<julia: Base.Printf> (inert IOBuffer)))) (= #22###x#6979 2) (local > #27#neg #26#pt #25#len #20#exp #23#do_out #24#args) (if (call (. #0# (inert > isfinite)) #22###x#6979) (block (= (tuple > #23#do_out #24#args) (call (. #0# (inert decode_dec)) #21#out > #22###x#6979 #<julia: "0"> 2 -1 #<julia: Char(0x00000064)>)) (if #23#do_out > (block (= (tuple #25#len #26#pt #27#neg) #24#args) (&& #27#neg (call (. #0# > (inert write)) #21#out #<julia: > Char(0x0000002d)>)) (&& (comparison (call (. #0# (inert -)) (call (. > #0# (inert -)) 2 #27#neg) #26#pt) (. #0# (inert >)) 0) (call (. #0# (inert > write)) #21#out #<julia: Char(0x00000030)>)) (call (. #0# (inert write)) > #21#out (call (. #0# (inert pointer)) (. #0# (inert DIGITS))) > #26#pt)))) (call (. #0# (inert write)) #21#out (block (line 145 > printf.jl) (if (call (. #0# (inert isnan)) #22###x#6979) #<julia: "NaN"> > (if (comparison #22###x#6979 (. #0# (inert <)) 0) #<julia: "-Inf"> #<julia: > "Inf">))))) (. #0# (inert nothing)) (call (. #0# (inert takebuf_string)) > #21#out))))" is not a valid function argument name > > Perhaps `symbol` isn't what I want here? > >
