# How to optymaly to save strings in JLD ? What wrong?

**URL:** <https://discourse.julialang.org/t/how-to-optymaly-to-save-strings-in-jld-what-wrong/6051>\
**Category:** General Usage\
**Created:** [September 24, 2017, 7:24am UTC](https://discourse.julialang.org/t/how-to-optymaly-to-save-strings-in-jld-what-wrong/6051 "2017-09-24T07:24:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)\
**Post date:** [September 24, 2017, 7:24am UTC](https://discourse.julialang.org/t/how-to-optymaly-to-save-strings-in-jld-what-wrong/6051/1 "2017-09-24T07:24:28Z")

</div>

i have long vecotr od srings, dane[:,5]  
12245198 -element Array{Any,1}:  
“aa”  
“ac”  
“…”  
“ab”  
“aa”

saved by  
writecsv(“file.txt”,string.(dane[:,5]))

is only ±33 000 KB

but saved by  
`file = jldopen(“file.jld”,“w”)  
close(file)  
file = jldopen(“file.jld”,“r+”)

@time write(file,“user”,string.(dane[:,5]))  
close(file)`

created file over 40 000 000 KB !!! What wrong ? How to optymaly this vector to save in JLD ? …as string.  
Paul

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [September 24, 2017, 8:23am UTC](https://discourse.julialang.org/t/how-to-optymaly-to-save-strings-in-jld-what-wrong/6051/2 "2017-09-24T08:23:22Z")

</div>

Try JLD2? Also please quote code with ` ` ` and blocks of code with `````.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 24, 2017, 8:57am UTC](https://discourse.julialang.org/t/how-to-optymaly-to-save-strings-in-jld-what-wrong/6051/3 "2017-09-24T08:57:16Z")

</div>

Also, if each element of that array

1. contains only ASCII characters,
2. has uniform length, or variable length that can be bounded by a small number,

then `Matrix{UInt8}` is also a viable choice.

If the strings repeat a lot, you could collect them and index into it. See for example  
[https://github.com/JuliaComputing/PooledArrays.jl](https://github.com/JuliaComputing/PooledArrays.jl)

---

<div class="post-metadata">

**Author:** ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)\
**Post date:** [September 24, 2017, 9:56am UTC](https://discourse.julialang.org/t/how-to-optymaly-to-save-strings-in-jld-what-wrong/6051/4 "2017-09-24T09:56:51Z")

</div>

Yes, JLD2 make to lage file also!  
Paul

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 24, 2017, 10:57am UTC](https://discourse.julialang.org/t/how-to-optymaly-to-save-strings-in-jld-what-wrong/6051/5 "2017-09-24T10:57:10Z")

</div>

What happens if you convert the array to actually be of type `Vector{String}` instead of `Vector{Any}`?

---

<div class="post-metadata">

**Author:** ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)\
**Post date:** [September 24, 2017, 5:55pm UTC](https://discourse.julialang.org/t/how-to-optymaly-to-save-strings-in-jld-what-wrong/6051/6 "2017-09-24T17:55:27Z")

</div>

Thanks, convert(Array{String},a) with JLD working better!

BUT, if i Have vector “a” {Any} like below. In this case is npossible to make CONVERT(…

If i am using string.(a) the saved data are huge:/  
What to do ?  
Paul

julia\> a  
2-element Array{Any,1}:  
123456  
“ABCDEF”

julia\> string.(a)  
2-element Array{String,1}:  
“123456”  
“ABCDEF”

julia\> convert(Array{String},a)  
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type String  
This may have arisen from a call to the constructor String(…),  
since type constructors fall back to convert methods.  
in copy!(::Base.LinearFast, ::Array{String,1}, ::Base.LinearFast, ::Array{Any,1}) at .\abstractarray.jl:559  
in convert(::Type{Array{String,N}}, ::Array{Any,1}) at .\array.jl:235

---

<div class="post-metadata">

**Author:** ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)\
**Post date:** [September 24, 2017, 6:01pm UTC](https://discourse.julialang.org/t/how-to-optymaly-to-save-strings-in-jld-what-wrong/6051/7 "2017-09-24T18:01:26Z")

</div>

I have !  
convert(Array{String},string.(a))  
🙂

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 24, 2017, 6:50pm UTC](https://discourse.julialang.org/t/how-to-optymaly-to-save-strings-in-jld-what-wrong/6051/8 "2017-09-24T18:50:38Z")

</div>

`string.(a)` doesn’t modify `a`, it returns a new array.
