How to parse vector/array of string?

question

#1

How to parse vector/array of string ?
julia> HH_baza[1,4]
“4385.37”

julia> parse(Float64,HH_baza[1,4])
4385.37

julia> parse(Float64,HH_baza[:,4])
ERROR: MethodError: parse has no method matching parse(::Type{Float64}, ::Array{UTF8String,1})
Closest candidates are:
parse{T<:AbstractFloat}(::Type{T<:AbstractFloat}, ::AbstractString)

julia> typeof(HH_baza[1,4])
UTF8String

julia> eltype(HH_baza[1,4])
Char

julia>
Paul


#2

Use an array comprehension.
In 0.6 (and maybe 0.5) you can use . (broadcast)


#3

broadcast doesn’t work here in 0.5

julia> parse.(Float64, ["122.3", "19990.2"])
ERROR: MethodError: no method matching size(::Type{Float64})

As @dpsanders suggested, array comprehensions should do the trick

julia> [parse(Float64, x) for x in ["122.3","433.2"]]
2-element Array{Float64,1}:
 122.3
 433.2

#4

On 0.5, there is a sneaky trick that I learnt today: wrap the first argument in square brackets:

julia> x = ["3.1", "-17.2"]
2-element Array{String,1}:
 "3.1"
 "-17.2"

julia> parse.([Float64], x)
2-element Array{Float64,1}:
   3.1
 -17.2

On 0.6, it’s just

julia> parse.(Float64, x)