Consider there is an m×n Matrix{Any}: where some of its arrays are decimal numbers (Float64) and some of them are strings of this type a.b.c, in which a, b, and c are integer numbers (a,b,c>10). Now, how can we write a code in Julia to 1) find these strings; and 2) remove the second point and change them from string a.b.c to number a.bc?
m = [11.23 "22.33.444"; "333.44.5555" 4.56]
function parse_matrix(m)
map(parse_cell, m)
end
parse_cell(x :: Number) = x;
function parse_cell(x :: AbstractString)
idx = findlast(c -> c == '.', x);
return parse(Float64, x[1 : (idx-1)] * x[(idx+1) : end])
end
parse_matrix(m)
2×2 Matrix{Any}:
11.23 "22.33.444"
"333.44.5555" 4.56
I have a feeling that I made the string indexing clunky / slow. There is probably a more efficient way of deleting the 2nd period from a string (using regex, most likely).
you could use one of these, but I don’t know if they are “better” than yours,
str="222.333.444"
reverse(replace(reverse(str),r"\." => "",count=1))
replace(str,r"\.(\d+)$" => s"\1")