[julia-users] Re: Creating variables programmatically

2015-11-03 Thread David P. Sanders


El martes, 3 de noviembre de 2015, 10:27:27 (UTC-6), Dawid Crivelli 
escribió:
>
> How about using a dictionary instead of variables, to do something like 
> the following:
>
> images = Dict{AbstractString, Image}() # dictionary associating an 
> Image object to a string
> image["flower"] = imread("flower.png") # made up function name
>
> Accessing the dictionary won't significantly slow the access to the image 
> data stored in memory, and you can access the images by their reference 
> string, which can depend on input data (e.g. a list of files).
>

Using a dictionary is a nice solution, but usually you want the types used 
for the keys and values to be concrete types, not abstract:

images = Dict{UTF8String, Image}()


[julia-users] Re: Creating variables programmatically

2015-11-03 Thread Dawid Crivelli
How about using a dictionary instead of variables, to do something like the 
following:

images = Dict{AbstractString, Image}() # dictionary associating an 
Image object to a string
image["flower"] = imread("flower.png") # made up function name

Accessing the dictionary won't significantly slow the access to the image 
data stored in memory, and you can access the images by their reference 
string, which can depend on input data (e.g. a list of files).


[julia-users] Re: Creating variables programmatically

2015-11-03 Thread cormullion
Hi Patrick. I'm never sure about things like this, but it seemed like a 
good approach to investigate. I want to load 20 or so images from disk and 
access them using predictable names.

On Tuesday, November 3, 2015 at 2:05:45 PM UTC, Patrick Kofod Mogensen 
wrote:
>
> 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], ... ?
>


[julia-users] Re: Creating variables programmatically

2015-11-03 Thread Patrick Kofod Mogensen
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, cormu...@mac.com 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 # 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 # (let (block (= #21#out (call (. 
> #0=# (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 # 2 -1 #)) (if #23#do_out 
> (block (= (tuple #25#len #26#pt #27#neg) #24#args) (&& #27#neg (call (. #0# 
> (inert write)) #21#out #Char(0x002d)>)) (&& (comparison (call (. #0# (inert -)) (call (. 
> #0# (inert -)) 2 #27#neg) #26#pt) (. #0# (inert >)) 0) (call (. #0# (inert 
> write)) #21#out #)) (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) # 
> (if (comparison #22###x#6979 (. #0# (inert <)) 0) # # "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?
>
>