# Extract String from Array

**URL:** <https://discourse.julialang.org/t/extract-string-from-array/48720>\
**Category:** General Usage\
**Created:** [October 21, 2020, 12:59am UTC](https://discourse.julialang.org/t/extract-string-from-array/48720 "2020-10-21T00:59:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![torieowers](https://avatars.discourse-cdn.com/v4/letter/t/6a8cbe/32.png) [@torieowers](https://discourse.julialang.org/u/torieowers)\
**Post date:** [October 21, 2020, 12:59am UTC](https://discourse.julialang.org/t/extract-string-from-array/48720/1 "2020-10-21T00:59:25Z")

</div>

Hello,  
I am wanting to get only a list of strings, but instead I am getting an array of arrays.

I’ve run:

```julia
all_weights = vcat(map(get_weight,about_breeds_links))

```

get\_weight is a function to take in a link and return only one element in the page. about\_breeds\_links is an array of links which I am iterating through.

And my output is:

```julia
193-element Array{Array{String,1},1}:
 ["65-80 pounds (male), 55-70 pounds (female)"]
 ["65-90 pounds (male), 50-70 pounds (female)"]
 ["65-75 pounds (male), 55-65 pounds (female)"]
 ["under 28 pounds"]
 ["50 pounds (male), 40 pounds (female)"]
 ["60-70 pounds (male), 40-50 pounds (female)"]
 ["under 20 pounds (13 inches & under), 20-30 pounds (13-15 inches)"]
 ["95-135 pounds (male), 80-100 pounds (female)"]
 ["55-70 pounds (male), 45-60 pounds (female)"]
 ["up to 30 pounds (male), up to 28 pounds (female)"]
 ["16-32 pounds (standard), 11 pounds & under (miniature)"]
 ["7 pounds"]
 ["50-65 pounds (male), 40-55 pounds (female)"]
 ⋮

```

I would like to keep only, for example, ‘65-80 pounds (male), 55-70 pounds (female)’

Any thoughts on this would be much appreciated!

---

<div class="post-metadata">

**Author:** ![AndiMD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andimd/32/6531_2.png) [@AndiMD](https://discourse.julialang.org/u/AndiMD)\
**Post date:** [October 21, 2020, 1:11am UTC](https://discourse.julialang.org/t/extract-string-from-array/48720/2 "2020-10-21T01:11:55Z")

</div>

It appears that `get_weight(link)` returns an array of strings with one element.  
Use `only` (or `first`) to get rid of that.

`all_weights = only.(get_weight.(about_breeds_links))`  
or  
`all_weights = about_breeds_links .|> get_weight .|> only`

---

<div class="post-metadata">

**Author:** ![torieowers](https://avatars.discourse-cdn.com/v4/letter/t/6a8cbe/32.png) [@torieowers](https://discourse.julialang.org/u/torieowers)\
**Post date:** [October 21, 2020, 2:05am UTC](https://discourse.julialang.org/t/extract-string-from-array/48720/3 "2020-10-21T02:05:40Z")

</div>

Worked perfectly! Thanks so much, AndiMD

---

<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 21, 2020, 11:50am UTC](https://discourse.julialang.org/t/extract-string-from-array/48720/4 "2020-10-21T11:50:52Z")

</div>

> [@AndiMD](#):
>
> It appears that `get_weight(link)` returns an array of strings with one element.  
> Use `only` (or `first` ) to get rid of that.

Better yet, change your `get_weight` function to return a single string if that’s what you want.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [October 21, 2020, 1:43pm UTC](https://discourse.julialang.org/t/extract-string-from-array/48720/5 "2020-10-21T13:43:15Z")

</div>

Note `only` will throw an error if your `get_weight` function ever returns a vector with more or less than one element, `first` will throw an error only if the vector comes empty. As @stevengj said, if `only` solves your problem, then you probably want to not return the `String` inside an `Array` instead.
