Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

change type of every element in array in julia

i want an array that contains a range of numbers but store them as strings.
this is a sample output i need:

['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

I tried this, but it produces a string with an array as its value.

julia> string([0:9...])
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"

so how can i generate such thing?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

and this is how it can be done in python in case of better understanding:

>>> list('0123456789')
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

>Solution :

First of all, note that Julia has separate types for Char (single characters) and String types. In Julia, ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] would be a character array, and can be created as:

julia> '0':'9'
'0':1:'9'

You can verify that this contains the above array by doing:

julia> '0':'9' |> collect |> print
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

But if you do want the result to be Strings and not Chars, you can use broadcasting to generate that:

julia> v = 1001:1010 # your input range here
1001:1010

julia> string.(v)
10-element Vector{String}:
 "1001"
 "1002"
 "1003"
 "1004"
 "1005"
 "1006"
 "1007"
 "1008"
 "1009"
 "1010"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading