Different ways to create a vector of strings in Julia

5,095 views
Skip to first unread message

Charles Novaes de Santana

unread,
Mar 12, 2015, 7:05:26 AM3/12/15
to julia...@googlegroups.com
Dear all,

I was trying to create a vector of strings in Julia and I didn't understand why the following ways give me different results.

Everything is fine If I try the following two approaches:
names = String[];
push!(names,"word1");

names = Array(String,0);
push!(names,"word1");

However, I supposed this other way should work too, but it didn't:
names = Array{String};
push!(names,"word2");

It gives me the following error: "ERROR: `push!` has no method matching push!(::Type{Array{String,N}}, ::ASCIIString)"

Why is String[] and Array(String,0) different from Array{String}?

Thanks for any help! 

Best,

Charles

-- 
Um axé! :)

--
Charles Novaes de Santana, PhD
http://www.imedea.uib-csic.es/~charles

Tamas Papp

unread,
Mar 12, 2015, 7:24:15 AM3/12/15
to julia...@googlegroups.com
Hi Charles,

Array{String} is a type, not an array:

julia> isa(Array{String}, Type)
true

julia> typeof(Array{String})
DataType

See http://docs.julialang.org/en/release-0.3/manual/types/ .

The function Array(eltype, dimensions...) can be used to instantiate an
object of this type, which is the convention in Julia, so IMO you should
just keep using that.

Best,

Tamas

Mauro

unread,
Mar 12, 2015, 7:26:31 AM3/12/15
to julia...@googlegroups.com
> However, I supposed this other way should work too, but it didn't:
> names = Array{String};
> push!(names,"word2");
>
> It gives me the following error: "ERROR: `push!` has no method matching
> push!(::Type{Array{String,N}}, ::ASCIIString)"
>
> Why is String[] and Array(String,0) different from Array{String}?

`Array{String}` is a type (abstract at that), but you want an instance
of that type. So, what you would want to do is `Array{String,1}()`.
That ought to construct a array of strings with dimension 1 but doesn't.
At least that is what most constructors do, e.g. `Dict{Int,String}()`.
However, there are no array constructors which look like this, you
construct them with the syntax you used in the first two examples:

> names = String[];
> push!(names,"word1");

> names = Array(String,0);
> push!(names,"word1");

Have a look at the Construtors section of the manual

Ivar Nesje

unread,
Mar 12, 2015, 9:34:44 AM3/12/15
to julia...@googlegroups.com
So, what you would want to do is `Array{String,1}()`.
That ought to construct a array of strings with dimension 1 but doesn't.

But in 0.4 you can use Array{String,1}(0) to create a 1d array with 0 elements. Note that you have to provide the size of the array, and 0 is not default (, but maybe it should be?)

Tamas Papp

unread,
Mar 12, 2015, 10:10:14 AM3/12/15
to julia...@googlegroups.com
Array{T}(dims...)

is equivalent to

Array{T,length(dims)}(dims...)

while the latter has a redundant parameter. So instead of providing a
default for dims..., wouldn't

Array{T}(0)

be an idiomatic solution for creating an empty vector of eltype T? Has
one less character than

Array{T,1}(0)

:D

Best,

Tamas

Charles Novaes de Santana

unread,
Mar 12, 2015, 10:17:26 AM3/12/15
to julia...@googlegroups.com
Thank you all for the explanation! We don't stop learning in this list!

Best,

Charles

Ismael VC

unread,
Mar 12, 2015, 10:49:13 AM3/12/15
to julia...@googlegroups.com
@Tammas Array{T}(0) works in 0.4-dev, but I think that's why there is also a Vector{T} typealias for Array{T,1}, so it could be Vector{T}(0) which is slightly better IMHO.

julia> Vector{Int}(0)
0-element Array{Int64,1}

julia> Array{Int,1}(0)
0-element Array{Int64,1}

julia> Array{Int}(0)
0-element Array{Int64,1}
Reply all
Reply to author
Forward
0 new messages