# Assigning result of @load to a variable

**URL:** https://discourse.julialang.org/t/assigning-result-of-load-to-a-variable/118827
**Category:** General Usage
**Created:** [August 30, 2024, 8:07pm UTC](https://discourse.julialang.org/t/assigning-result-of-load-to-a-variable/118827 "2024-08-30T20:07:39Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![julien-arino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/julien-arino/32/211710_2.png) [@julien-arino](https://discourse.julialang.org/u/julien-arino)
#### Post date: [August 30, 2024, 8:07pm UTC](https://discourse.julialang.org/t/assigning-result-of-load-to-a-variable/118827/1 "2024-08-30T20:07:39Z")

</div>

Sorry, I am quite new to julia… From looking around, this may be linked to serialization, but I am not sure. In R, I would do (sorry to the R purists, I am one of these “=” \<=\> “\<-” people):

```julia
a = 2
saveRDS(a, "example.Rds")
b = readRDS("example.Rds")

```

I have tried doing the same type of thing in julia

```julia
using JLD2
a = 2
@save "example.jld2" a

```

but then when I `@load "example.jld2`, it comes with the name `a` attached (even if I go `b = @load...`). I don’t want that, I want to assign it to something else right away without manipulation.

To motivate this, one context where this arises is when I am running simulations where the results weigh quite a bit (hundreds of MB to several GB). In the sim code, I use a generic variable name and just change the name of the save file to include some info about the specific sim. In the post-sim code, I just go through the result files, get what info is needed from the file name and process, using again a generic variable name to which I assign the content of whatever file I am using at that point.

I could of course adapt the code to use specific variable names both in and out, but it’s just much easier to go that way. (And saving as csv is either unrealistic in the case of simple variables or not possible for more structured stuff.)

Is there a julia equivalent of my R example?

---

<div class="post-metadata">

### Author: ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)
#### Post date: [August 30, 2024, 8:27pm UTC](https://discourse.julialang.org/t/assigning-result-of-load-to-a-variable/118827/2 "2024-08-30T20:27:09Z")

</div>

Just from the [Readme](https://github.com/JuliaIO/JLD2.jl):  
Why not use `b = load("example.jld2")`? Or `b = load("example.jld2")["a"]` if you just want the specific entry?

I think `@load` is deprecated anyways.

---

<div class="post-metadata">

### Author: ![julien-arino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/julien-arino/32/211710_2.png) [@julien-arino](https://discourse.julialang.org/u/julien-arino)
#### Post date: [September 1, 2024, 2:52pm UTC](https://discourse.julialang.org/t/assigning-result-of-load-to-a-variable/118827/3 "2024-09-01T14:52:29Z")

</div>

The second version is what I am after. I will say “ish”: it works, but it is weird to have to know/find out the name a variable had when it was saved.

---

<div class="post-metadata">

### Author: ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)
#### Post date: [September 1, 2024, 3:16pm UTC](https://discourse.julialang.org/t/assigning-result-of-load-to-a-variable/118827/4 "2024-09-01T15:16:40Z")

</div>

Well if you know that this file just contains a single key-value pair you could also just do something like:

```julia
first(values(load("example.jld2")))
load("example.jld2") |> values |> first # alternative way of writing the same thing
load("example.jld2") |> first |> first # also works

```

---

<div class="post-metadata">

### Author: ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)
#### Post date: [September 1, 2024, 4:26pm UTC](https://discourse.julialang.org/t/assigning-result-of-load-to-a-variable/118827/5 "2024-09-01T16:26:35Z")

</div>

> [@abraemer](#):
>
> I think `@load` is deprecated anyways.

(Note that also `@save` is listed under Legacy in the [documentation](https://juliaio.github.io/JLD2.jl/dev/).)

> [@julien-arino](#):
>
> it is weird to have to know/find out the name a variable had when it was saved.

If you want to save the value of `a` with an explicit name (different than `"a"`), you can use `jldsave("example.jld2", explicit_name=a)` or `save("example.jld2", "explicit_name", a)` to save, and `new_var = load("example.jld2")["explicit_name"])` to load.

If you don’t care about the name `a` is saved under, use `save_object("example.jld2", a)` and `new_var = load_object("example.jld2")`.

---

<div class="post-metadata">

### Author: ![julien-arino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/julien-arino/32/211710_2.png) [@julien-arino](https://discourse.julialang.org/u/julien-arino)
#### Post date: [September 2, 2024, 11:22pm UTC](https://discourse.julialang.org/t/assigning-result-of-load-to-a-variable/118827/6 "2024-09-02T23:22:50Z")

</div>

Cool, that’s indeed exactly what I was looking for.
