2

i wanna write a code in julia, and i need input some data like c code(that explain below). how can i do this in julia?

in c code we wrote:

 printf(" number = \n ");

scanf("%d", &N); 

thanks in advance

Armaa
  • 625
  • 1
  • 5
  • 13
  • 3
    Possible duplicate of [Julia request user input from script](http://stackoverflow.com/questions/17479782/julia-request-user-input-from-script) – Gnimuc May 17 '17 at 06:32
  • 1
    @Gnimuc i don't need input string, i wanna input multiple data,like input integer and array. – Armaa May 17 '17 at 06:35
  • 3
    Stuff read from input is strings. You can extract elements from it with eval and parse, e.g. `eval(parse("[2, 3]"))`. But the more important question is, why would you do it? Julia runs live in the console, so normally you don't have to build artificial user-facing interactivity. You can write the functions and assume the user knows Julia. – Michael K. Borregaard May 17 '17 at 08:21
  • @PatelMinhaj - Please stop appending "thanks in advance" in all these edits. – Brad Larson May 17 '17 at 18:04

1 Answers1

2

You want readline for getting input, and parse(T,s) for parsing a string s into type T. Example

julia> s = readline()
1 # readline blocks until it sees \n, I typed the "1 enter" here
"1\n"

julia> parse(Int, chomp(s))
1

The above example uses Julia 0.5, readline will remove the "\n" by default in 0.6 and later. And another user mentioned, with the REPL you rarely need to do this.

gggg
  • 2,284
  • 1
  • 17
  • 19