# How to create vector of specific structure?

**URL:** <https://discourse.julialang.org/t/how-to-create-vector-of-specific-structure/67606>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 2, 2021, 11:25pm UTC](https://discourse.julialang.org/t/how-to-create-vector-of-specific-structure/67606 "2021-09-02T23:25:04Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 2, 2021, 11:25pm UTC](https://discourse.julialang.org/t/how-to-create-vector-of-specific-structure/67606/1 "2021-09-02T23:25:04Z")

</div>

I have this structure:

```julia
julia> Base.@kwdef mutable struct Vsines
           instancename::String="Def"
           value::Float64=0.0
       end

```

and I want to create a vector of “Vsines” instances (name of this vector is “s”), in which I dont know initially the size of this vector, however, it grows in iterative manner when (event = true). So, I wrote something like below, but I have an error. Could you please correct my way?

```julia
julia> i=0; event = true;
julia> if event == true
          i +=1;
          s[i]=Vsines()
       end
ERROR: UndefVarError: s not defined
Stacktrace:
 [1] top-level scope
   @ REPL[3]:3

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 2, 2021, 11:41pm UTC](https://discourse.julialang.org/t/how-to-create-vector-of-specific-structure/67606/2 "2021-09-02T23:41:13Z")

</div>

```julia
s=Vsines[] #initialize
push!(s,Vsines()) # push new element

```

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 2, 2021, 11:48pm UTC](https://discourse.julialang.org/t/how-to-create-vector-of-specific-structure/67606/3 "2021-09-02T23:48:55Z")

</div>

Can we assign specific value meanwhile we using push? such as:

```julia
Base.@kwdef mutable struct Vsines
           instancename::String="Def"
           value::Float64=0.0
       end
s=Vsines[]
push!(s.value,Vsines(,0.0))

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [September 2, 2021, 11:59pm UTC](https://discourse.julialang.org/t/how-to-create-vector-of-specific-structure/67606/4 "2021-09-02T23:59:48Z")

</div>

> [@Amro](#):
>
> `push!(s.value,Vsines(,0.0))`

`s` is a vector of `Vsines`, it doesn’t have a field called `value`. Just push into `s`

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 3, 2021, 12:01am UTC](https://discourse.julialang.org/t/how-to-create-vector-of-specific-structure/67606/5 "2021-09-03T00:01:03Z")

</div>

```julia
push!(s,Vsines("name",0.0))

```

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 3, 2021, 12:05am UTC](https://discourse.julialang.org/t/how-to-create-vector-of-specific-structure/67606/6 "2021-09-03T00:05:28Z")

</div>

Thank you very much!

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 3, 2021, 12:05am UTC](https://discourse.julialang.org/t/how-to-create-vector-of-specific-structure/67606/7 "2021-09-03T00:05:45Z")

</div>

Thank you very much!!

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 3, 2021, 1:52pm UTC](https://discourse.julialang.org/t/how-to-create-vector-of-specific-structure/67606/8 "2021-09-03T13:52:19Z")

</div>

Could you please let me know how to solve this problem?

```julia
ulia> Base.@kwdef mutable struct Vsines
           instancename::Array{String,3} = ["NN","NN","NN"]
           value::Array{Int128,3} = [0,0,0]
       end
Vsines

julia> s=Vsines[]
Vsines[]

julia> push!(s,Vsines())
ERROR: MethodError: no method matching Array{String, 3}(::Vector{String})
Closest candidates are:
  Array{T, N}(::AbstractArray{S, N}) where {T, N, S} at array.jl:540      
  Array{T, N}(::BitArray{N}) where {T, N} at bitarray.jl:494
  Array{T, N}(::Core.Compiler.BitArray{N}) where {T, N} at bitarray.jl:494
  ...
Stacktrace:
 [1] convert(#unused#::Type{Array{String, 3}}, a::Vector{String})
   @ Base .\array.jl:532
 [2] Vsines(instancename::Vector{String}, value::Vector{Int64})  
   @ Main .\REPL[2]:2
 [3] Vsines(; instancename::Vector{String}, value::Vector{Int64})
   @ Main .\util.jl:450
 [4] Vsines()
   @ Main .\util.jl:450
 [5] top-level scope
   @ REPL[4]:1

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 3, 2021, 1:55pm UTC](https://discourse.julialang.org/t/how-to-create-vector-of-specific-structure/67606/9 "2021-09-03T13:55:30Z")

</div>

> [@Amro](#):
>
> `Array{String,3} = ["NN","NN","NN"]`

The 3 is the dimension of the array, not the number of elements. What you there is a Vector, or, equivalently, an array of dimension 1:

```julia
julia> Base.@kwdef mutable struct Vsines
                  instancename::Vector{String} = ["NN","NN","NN"]
                  value::Vector{Int128} = [0,0,0]
              end
Vsines

julia> s=Vsines[]
Vsines[]

julia> push!(s,Vsines())
1-element Vector{Vsines}:
 Vsines(["NN", "NN", "NN"], Int128[0, 0, 0])

```

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 3, 2021, 1:59pm UTC](https://discourse.julialang.org/t/how-to-create-vector-of-specific-structure/67606/10 "2021-09-03T13:59:58Z")

</div>

> [@lmiq](#):
>
> ```julia
> Base.@kwdef mutable struct Vsines
> instancename::Vector{String} = ["NN","NN","NN"]
> value::Vector{Int128} = [0,0,0]
> end
> 
> ```

Thank you very much for your experience and explanation!
