Writing on an existing Julia data file with the same key

Viewed 69

Assume that we have a .jld file which has two keys, "hi" and "bye" as

import JLD

file = JLD.jldopen("test.jld","a+")
     file["hi"] = randn(1)
     file["bye"] = randn(1)
JLD.close(file)

Now what should I do if I want to change the value saved on test.jld with key "hi" and without affecting the value for the key "bye"?

It tried the following code

file = JLD.jldopen("test.jld","a+")
     file["hi"] = randn(1)
JLD.close(file)

but it shows the error Error creating dataset //hi.

1 Answers

Once you have created the JLD file, you should use load and save to change values, ie

julia> using JLD

julia> filed = JLD.load("test.jld")
Dict{String,Any} with 2 entries:
  "bye" => [-0.275391]
  "hi"  => [-0.869752]

julia> filed["hi"] = randn(1)
1-element Array{Float64,1}:
 -0.3132472191308679

julia> JLD.save("test.jld", filed)

julia> filed = JLD.load("test.jld")
Dict{String,Any} with 2 entries:
  "bye" => [-0.275391]
  "hi"  => [-0.313247]
Related