Extract String from Array

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

I’ve run:

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:

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!

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

2 Likes

Worked perfectly! Thanks so much, AndiMD

1 Like

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

3 Likes

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.

2 Likes