How to integrate parse with split?

Hello,
How can I correct the command a, b, c = parse(Int,split(readline()))? I want to receive three inputs and simultaneously convert it to an integer and assign it to each of the variables.

Thank you.

Admittedly unsure what you’re inputting, but broadcast?

julia> a, b, c = parse.(Int,split(readline()))
2 3 1
3-element Vector{Int64}:
 2
 3
 1

julia> c, a, b
(1, 2, 3)
2 Likes

Hi,
Thank you so much.
Why did you use . in parse? I had seen the general form of parse as follows:

variable = parse(Type, readline())

That is dot syntax for broadcasting a function or dotted sequence of functions over arrays of their inputs. The example is equivalent to the higher-order function call broadcast(parse, Int, split(readline())), though dot calls (check Meta.@lower) actually lower to a different sequence of higher-order function calls.

Broadcasting is the go-to way to do elementwise operations, but it’s not simply that. map is the straightforward higher-order function for elementwise operations over sequences of inputs, and it has parallels in many functional languages and languages influenced by them. Broadcasting additionally involves expanding the output along the different but compatible dimensions of the input arrays, and it has parallels in MATLAB and NumPy. Unlike those languages however, Julia doesn’t have separate broadcasting versions of functions, we just put our functions into higher-order functions. Dot syntax helps make broadcasting easier to write.

julia> [10 100 1000] * [1,2,3] # matrix multiplication, NOT elementwise
1-element Vector{Int64}:
 3210

julia> 10 * 1 # scalar multiplication; elementwise methods below
10

julia> [10 100 1000] .* [1,2,3] # 1x3-matrix .* 3-vector = 3x3 matrix
3×3 Matrix{Int64}:
 10  100  1000
 20  200  2000
 30  300  3000

julia> map(*, [10 100 1000], [1,2,3]) # iterates as sequences
3-element Vector{Int64}:
   10
  200
 3000
2 Likes