Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

collapse a vector as comma separated string in julia

Tags:

printing

julia

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
like image 997
PoisonAlien Avatar asked Mar 10 '23 08:03

PoisonAlien


1 Answers

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
like image 63
TigerhawkT3 Avatar answered Mar 19 '23 17:03

TigerhawkT3