Reputation: 431
I have 1-d vector of values. I want to convert them into a string with values separated by comma. Is there an easy way in Julia to do this? Something like collapse in r
{julia}
julia> x = [24,122,63,24,83,56,54,175,11,11,24,51,85,92,74,500,80,127,126,59,111,155,132,202,64,164,1301]
#I want output like this as a string
#24,122,63,24,83,56,54,175,11,11,24,51,85,92,74,500,80,127,126,59,111,155,132,202,64,164,1301,27
#I have tried something like this
[julia> [print(i,",") for i in x]
24,122,63,24,83,56,54,175,11,11,24,51,85,92,74,500,80,127,126,59,111,155,132,202,64,164,1301,27-element Array{Void,1}:
nothing
nothing
nothing
nothing
nothing
nothing
nothing
nothing
nothing
nothing
nothing
nothing
nothing
nothing
nothing
nothing
nothing
nothing
nothing
nothing
nothing
nothing
nothing
nothing
nothing
nothing
nothing
Upvotes: 7
Views: 2660
Reputation: 49318
Print most of the values with an ordinary loop, then print the last item (to eliminate the trailing comma):
julia> for i in @view x[1:end-1]
print(i, ',')
end; print(x[end])
24,122,63,24,83,56,54,175,11,11,24,51,85,92,74,500,80,127,126,59,111,155,132,202,64,164,1301
You can also join each item in the iterable with a comma:
julia> print(join(x, ','))
24,122,63,24,83,56,54,175,11,11,24,51,85,92,74,500,80,127,126,59,111,155,132,202,64,164,1301
Upvotes: 9
Reputation: 505
Also this, which is simpler: print(string(x)[2:(end - 1)])
.
Upvotes: 0