# Converting SubString Array to Array of Floats without loops

**URL:** <https://discourse.julialang.org/t/converting-substring-array-to-array-of-floats-without-loops/6702>\
**Category:** New to Julia\
**Created:** [October 26, 2017, 2:34pm UTC](https://discourse.julialang.org/t/converting-substring-array-to-array-of-floats-without-loops/6702 "2017-10-26T14:34:39Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![josimar](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@josimar](https://discourse.julialang.org/u/josimar)\
**Post date:** [October 26, 2017, 2:34pm UTC](https://discourse.julialang.org/t/converting-substring-array-to-array-of-floats-without-loops/6702/1 "2017-10-26T14:34:39Z")

</div>

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

```julia
#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:

```julia
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

```

---

<div class="post-metadata">

**Author:** ![Daneel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daneel/32/175_2.png) [@Daneel](https://discourse.julialang.org/u/Daneel)\
**Post date:** [October 26, 2017, 2:38pm UTC](https://discourse.julialang.org/t/converting-substring-array-to-array-of-floats-without-loops/6702/2 "2017-10-26T14:38:04Z")

</div>

```julia
coordx = parse.(Float64,coordx)

```

[https://docs.julialang.org/en/stable/manual/arrays/#Broadcasting](https://docs.julialang.org/en/stable/manual/arrays/#Broadcasting)

---

<div class="post-metadata">

**Author:** ![josimar](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@josimar](https://discourse.julialang.org/u/josimar)\
**Post date:** [October 26, 2017, 2:53pm UTC](https://discourse.julialang.org/t/converting-substring-array-to-array-of-floats-without-loops/6702/3 "2017-10-26T14:53:27Z")

</div>

thanks for the answer.

Your solutions returns the following:

```julia
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.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 26, 2017, 3:01pm UTC](https://discourse.julialang.org/t/converting-substring-array-to-array-of-floats-without-loops/6702/4 "2017-10-26T15:01:07Z")

</div>

> [@josimar](#):
>
> coordx = parse.(coordx)

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.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 26, 2017, 3:16pm UTC](https://discourse.julialang.org/t/converting-substring-array-to-array-of-floats-without-loops/6702/5 "2017-10-26T15:16:50Z")

</div>

> [@josimar](#):
>
> do you know why this is happening?

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](https://docs.julialang.org/en/stable/manual/performance-tips/#Avoid-changing-the-type-of-a-variable-1) 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.

---

<div class="post-metadata">

**Author:** ![josimar](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@josimar](https://discourse.julialang.org/u/josimar)\
**Post date:** [October 26, 2017, 3:22pm UTC](https://discourse.julialang.org/t/converting-substring-array-to-array-of-floats-without-loops/6702/6 "2017-10-26T15:22:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [October 26, 2017, 4:39pm UTC](https://discourse.julialang.org/t/converting-substring-array-to-array-of-floats-without-loops/6702/7 "2017-10-26T16:39:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![thgnguyen](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@thgnguyen](https://discourse.julialang.org/u/thgnguyen)\
**Post date:** [July 12, 2018, 12:14am UTC](https://discourse.julialang.org/t/converting-substring-array-to-array-of-floats-without-loops/6702/8 "2018-07-12T00:14:33Z")

</div>

Hello,

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

```julia
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

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

```

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

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 12, 2018, 2:18am UTC](https://discourse.julialang.org/t/converting-substring-array-to-array-of-floats-without-loops/6702/9 "2018-07-12T02:18:29Z")

</div>

> [@thgnguyen](#):
>
> parse(1.,a)

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`.

---

<div class="post-metadata">

**Author:** ![thgnguyen](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@thgnguyen](https://discourse.julialang.org/u/thgnguyen)\
**Post date:** [July 13, 2018, 1:18pm UTC](https://discourse.julialang.org/t/converting-substring-array-to-array-of-floats-without-loops/6702/10 "2018-07-13T13:18:27Z")

</div>

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
