Converting SubString Array to Array of Floats without loops


#1

Hi All,

Is there a way to convert an Array{SubString{String},1} to an array of Float64 without using for loops ?

Please let me know if there is any workaround to avoid looping over the container below to covert it to Float.

thank you

#Here is part of my container
println(coordx[1:10])
SubString{String}[" 4"," 6.92584918232797"," 7.14504488729123"," 6.7353635040523"," 10"," \n    7.62028834172961"," 10"," 4"," 6.73887608008517"," 10"]

println(typeof(coordx))
Array{SubString{String},1}

Currently this is what I am doing:

coordx_tmp=zeros(Float64,length(coordx))
for i=1:size(coordx)[1]
    coordx_tmp[i]=parse(Float64,strip(coordx[i]))
end

coordx=[]; coordx=coordx_tmp

#2
coordx = parse.(Float64,coordx)

https://docs.julialang.org/en/stable/manual/arrays/#Broadcasting


#3

thanks for the answer.

Your solutions returns the following:

LoadError: MethodError: no method matching size(::Type{Float64})
Closest candidates are:
  size{N}(::Any, !Matched::Integer, !Matched::Integer, !Matched::Integer...) at abstractarray.jl:48
  size(!Matched::BitArray{1}) at bitarray.jl:39
  size(!Matched::BitArray{1}, !Matched::Any) at bitarray.jl:43
  ...
while loading  in broadcast_shape(::Type{T}, ::Array{SubString{String},1}) at broadcast.jl:31
 in broadcast_t(::Function, ::Type{Any}, ::Type{T}, ::Vararg{Any,N}) at broadcast.jl:213
 in broadcast(::Function, ::Type{T}, ::Array{SubString{String},1}) at broadcast.jl:230
 in include_string(::String, ::String) at loading.jl:441
 in include_string(::String, ::String) at sys.dylib:?
 in include_string(::Module, ::String, ::String) at eval.jl:34
 in (::Atom.##59#62{String,String})() at eval.jl:73
 in withpath(::Atom.##59#62{String,String}, ::String) at utils.jl:30
 in withpath(::Function, ::String) at eval.jl:38
 in macro expansion at eval.jl:71 [inlined]
 in (::Atom.##58#61{Dict{String,Any}})() at task.jl:60

To get it fixed I had to do:

coordx = parse.(coordx)
which now is :
Vector{Real}

do you know why this is happening ? thank you.


#4

This is the wrong function to use. It parses with julia syntax and is almost never what you want.

You cann upgrade to 0.6 or use a comprehension.


#5

To clarify @yuyichao’s answer, you are getting an error because you are using Julia 0.5. The dot-call (broadcast) functionality is greatly improved in Julia 0.6. You should almost certainly upgrade if you can.

Note also that coordx = parse.(Float64,coordx) is not type-stable because it changes the type of coordx. In general, it is a good habit to not change the type of a variable in your code, because it will hurt performance (if you do it in performance-critical code). It probably doesn’t matter for you here, but it is a good habit to use a new variable name if you change the type.


#6

thank you ! It is very clear now, I will follow up on it.


#7

It’s also a lot easier to debug your code by the tried-and-true cut-and-paste-in-REPL method if you avoid reusing variable names. Don’t worry about creating too many variables in function bodies because compilers are very good at figuring out when something will never be used again and not actually keeping it around.


#8

Hello,

I have the same problem, I use julia 0.6.1 on Ubuntu 16.04. This is the error message

MethodError: no method matching parse(::Float64, ::SubString{String})
Closest candidates are:
  parse(::Type{IPv4}, ::AbstractString) at socket.jl:167
  parse(::Type{IPv6}, ::AbstractString) at socket.jl:218
  parse(::Type{DateTime}, ::AbstractString, ::DateFormat{Symbol("yyyy-mm-dd\\THH:MM:SS.s"),Tuple{Base.Dates.DatePart{'y'},Base.Dates.Delim{Char,1},Base.Dates.DatePart{'m'},Base.Dates.Delim{Char,1},Base.Dates.DatePart{'d'},Base.Dates.Delim{Char,1},Base.Dates.DatePart{'H'},Base.Dates.Delim{Char,1},Base.Dates.DatePart{'M'},Base.Dates.Delim{Char,1},Base.Dates.DatePart{'S'},Base.Dates.Delim{Char,1},Base.Dates.DatePart{'s'}}}) at dates/parse.jl:202
  ...

Stacktrace:
 [1] macro expansion at ./In[30]:6 [inlined]
 [2] anonymous at ./<missing>:?

My example

a = "0.24752"
b = parse(1.,a)

I just started to learn using julia this morning. Do I miss something?


#9

The first argument of parse should be a type, not an instance of a type. Try parse(Float64, a) to parse the string a as a Float64.


#10

Ahhh, you’re right.

The first example I saw on some blogs about using parse uses a number of type int there and it works. It somehow makes me think I can just pass a 1. then julia will figure out it’s a Float64.

It works now.

Thanks