How do I request user input from a running script in Julia? In MATLAB, I would do:
result = input(prompt)
Thanks
How do I request user input from a running script in Julia? In MATLAB, I would do:
result = input(prompt)
Thanks
The easiest thing to do is readline(stdin)
. Is that what you're looking for?
As @StefanKarpinski points out, it's going to be adressed in the future, this is what I do for the time being:
julia> @doc """
input(prompt::String="")::String
Read a string from STDIN. The trailing newline is stripped.
The prompt string, if given, is printed to standard output without a
trailing newline before reading input.
""" ->
function input(prompt::String="")::String
print(prompt)
return chomp(readline())
end
input (generic function with 2 methods)
julia> x = parse(Int, input());
42
julia> typeof(ans)
Int64
julia> name = input("What is your name? ");
What is your name? Ismael
julia> typeof(name)
String
help?> input
search: input
input(prompt::String="")::String
Read a string from STDIN. The trailing newline is stripped.
The prompt string, if given, is printed to standard output without a trailing newline before reading input.
julia>
First I ran Pkg.add("Dates") then
using Dates
println()
print("enter year "); year = int(readline(STDIN))
print("enter month "); month = int(readline(STDIN))
print("enter day "); day = int(readline(STDIN))
date = Date(year, month, day)
println(date)