Reading an array interactively



Hello All

I am trying to find a way to input the components of an array of floating numbers (i.e. a vector for a mechanics class examples) interactively. This is the way I am doing it in a notebook

println("Componentes (3) of vector fx")
fx = zeros(Float64,3)
for i = 1:3
    fx[i] = parse(Float64,readline()) 

But I was wondering if there is a way to input all values at once, say separated by commas.


You could always parse the line as a String, and then use string methods such as split combined with parse to pull individual values out of the strings. So maybe something like

praseline(str::AbstractString) = parse.(Float64, split(str, " "))

line = readline()
fx = parseline(line)

You might be able to find a more direct way using read(stdin), but I don’t know what that would be off the top of my head.


Thanks ExM
I am trying it. Need to play around a little.


If you want them comma-separated, you can do readcsv(IOBuffer(readline())).

You can also do readcsv directly from stdin, but if you want it to work with a Jupyter notebook then you have to use readline().


Why do you want to do that? The user can just call an array constructor with their data.


I want to help the students input the data easily and clearly interactively when asked about the components of the vector. The way I have it now is not so intuitive because the students fill the STDIN box with the three values rather than waiting for the next STDIN box.