How do I initialize an empty array / vector of strings, and then push strings into

How do I initialize an empty array / vector of strings, and then push strings into it? When I do t = Vector{String} , I get this error when I then push into it:

push!(t, "a")
ERROR: MethodError: no method matching push!(::Type{Array{String,1}}, ::String)
Closest candidates are:
  push!(::Any, ::Any, ::Any) at abstractarray.jl:2252
  push!(::Any, ::Any, ::Any, ::Any...) at abstractarray.jl:2253
  push!(::Array{Any,1}, ::Any) at array.jl:940

and t = Array{String} results in the same error.

(I want to specify the type that the vector will be filled with in advance because the array will subsequently go into a namedtuple)

Note that the original poster on Slack cannot see your response here on Discourse. Consider transcribing the appropriate answer back to Slack, or pinging the poster here on Discourse so they can follow this thread.
(Original message :slack:) (More Info)

1 Like
julia> t = String[]
String[]

julia> push!(t,"a string")
1-element Array{String,1}:
 "a string"

julia> t
1-element Array{String,1}:
 "a string"

Another way

julia> w = Vector{String}()
String[]

julia> push!(w,"another string")
1-element Array{String,1}:
 "another string"

julia> w
1-element Array{String,1}:
 "another string"
5 Likes

2 posts were split to a new topic: Understanding push!