I wrote this when I wanted to cache the results of matread. Your problem 
sounds like it could be similar.

let matread_cache = (String => Any)[]
    global caching_matread
    function caching_matread(filename)
        if !haskey(matread_cache, filename)
            matread_cache[filename] = matread(filename)
        end
        return matread_cache[filename]
    end
end


Den torsdagen den 9:e oktober 2014 kl. 10:08:23 UTC+2 skrev 
cormu...@mac.com:
>
> A beginner's question...
>
> I'm writing a function that wants to load a set of data from a file, 
> depending on an argument passed to the function (so a different argument 
> requires a different set of data to be loaded). I'd like each set of data 
> to be stored somehow in separate variables so that when the function is 
> called again with the same argument, it doesn't have to load that 
> particular data again (because it takes 5 seconds to load).
>
> I'm not sure whether this requires the use of global variables? I looked 
> through the documents (
> http://julia.readthedocs.org/en/latest/search/?q=global) but didn't gain 
> enlightenment. :)
>
> I think I can test for the existence of a previously-defined variable 
> using:
>
>     if !isdefined(symbol(string(dataset)))
>        dataset = include("$(dataset).jl")
>     end
>
> but I'm not convinced this works correctly, because this creates a 
> variable inside the function...
>
>

Reply via email to