with Yichao's help,
typealias ParseableNumber Union{Float64, Float32, Signed, Unsigned, Bool}
"""
`input{T<:ParseableNumber}(::Type{T}, prompt::String="")::T`
Read an integer or a floating point value from STDIN.
The prompt string, if given, is printed to standard output without a
trailing newline before reading the input.
days = input(Int, "How many days? ")
"""
function input{T<:ParseableNumber}(::Type{T}, prompt::String = "")::T
print(prompt)
str = chomp(readline())
return parse(T, str)
end
On Friday, October 28, 2016 at 12:48:33 AM UTC-4, Jeffrey Sarnoff wrote:
>
> Thank you, that is helpful.
>
> On Friday, October 28, 2016 at 12:22:37 AM UTC-4, Yichao Yu wrote:
>>
>> On Fri, Oct 28, 2016 at 12:01 AM, Jeffrey Sarnoff
>> <[email protected]> wrote:
>> > And although readline() yields a String, if you are asking for, say, a
>> Int
>> > or a Float64 value, you can add a second version of `input`:
>> >
>> > ```
>> > typealias ParseableNumber Union{Float64, Float32, Signed, Unsigned,
>> Bool}
>> >
>> > """
>> > `input{T<:ParseableNumber}(::Type{T}, prompt::String="")::T`
>> >
>> > Read an integer or a floating point value from STDIN.
>> >
>> > The prompt string, if given, is printed to standard output without a
>> > trailing newline before reading the input.
>> >
>> > days = input(Int, "How many days? ")
>> >
>> > """
>> > function input{T<:ParseableNumber}(::Type{T}, prompt::String = "")::T
>> > print(prompt)
>> > str = chomp(readline())
>> > return parse(str)
>>
>> Don't use `parse(::String)` for this. It is for parsing julia code,
>> not for parsing numbers. Use sth like `parse(Int, str)` intead.
>>
>> > end
>> > ```
>> >
>> >
>> > On Thursday, October 27, 2016 at 1:47:27 PM UTC-4, Ismael Venegas
>> Castelló
>> > wrote:
>> >>
>> >> """
>> >> `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
>> >>
>> >>
>> >>
>> >>
>> >> El jueves, 27 de octubre de 2016, 10:16:25 (UTC-5), Aleksandr Mikheev
>> >> escribió:
>> >>>
>> >>> Hello,
>> >>>
>> >>> How could I input a data from the console? For instance, I would like
>> to
>> >>> make such that user is able to input the value of x. Is there any way
>> to do
>> >>> it like in Fortran or something? I can't find anything in
>> documentation.
>> >>>
>> >>> P.S. Also, I believe there is a way to input a string using
>> readline()
>> >>> function. However, if I do something like:
>> >>>
>> >>> a = readline()
>> >>> "asd"
>> >>>
>> >>> then I will get "\"asd\"\r\n".
>> >>>
>> >>> How to avoid these excess symbols?
>> >>>
>> >>> Thank you in advance!
>>
>