Splitting an array of strings

Hello,
I am trying to split an array of strings so that each row of the string array is split into individual elements.

a = ["MARZ  M AA1 R Z";
"MARZAN  M AA1 R a = Z AH0 N";
"MARZANO  M AA0 R Z AA1 N OW2";
"MARZEC  M AA1 R Z IH0 K";
"MARZETTE  M AA2 R Z EH1 T";
"MARZILLI  M AA0 R Z IY1 L IY2";
"MARZO  M AA1 R Z OW2";
"MARZOLF  M AA1 R Z OW2 L F"]

I’ve tried this:

for i in a
    push!(a,split(i))
end

but I get this error:

ERROR: MethodError: Cannot `convert` an object of type Array{SubString{String},1} to an object of type String
This may have arisen from a call to the constructor String(...),
since type constructors fall back to convert methods.

Many thanks!
Nakul

is a bit hard to understand. Let’s say you have

a = ["ab ab", "cd cd cd"]

what do you want the output to be?

2 Likes

If I understand you correctly you are looking for

[split(el, " ") for el in a]

This returns a Vector{Substring{String}}[8], where each element is one of the elements in a split on spaces.

1 Like

Here’s some python code that parses this type of data. Perhaps you can get some inspiration from that, since you can implement it quite similarly in Julia.

1 Like

A shorter way would be split.(a) - the default split character is already space.

2 Likes

If you want all strings to end up in one long array, you can

julia> String.(reduce(vcat,split.(a)))
58-element Array{String,1}:
 "MARZ"   
 "M"      
 "AA1"    
 "R"      
 "Z"      
 "MARZAN" 
 "M"      
 "AA1"    
 "R"      
 "a"      
 "="      
 "Z"      
 "AH0"    
 "N"      
 ⋮        

where reduce(vcat,... concates all Vectors of Strings and String.() converts from SubString to String

2 Likes