Input from a file or keyboard arrives as text, even when it looks like a number. Parsing interprets that text as a value of a chosen type. In Julia, put the target type first: `parse(Int, "42")` returns `42`, while `parse(Float64, "42")` returns `42.0`. The same text can therefore produce different values; `parse(Bool, "true")` and `parse(Bool, "1")` both return `true`. section 5.3.2
Turn a sales field into a price
Suppose a line read from a pizza-sales file is `"hawaiian,S,10.5"`. Try `fields = split(line, ',')` after assigning that line to `line`. Then `fields[3]` is text, so multiplying it by `1.25` does not work. Use `price = parse(Float64, fields[3])`; now `price * 1.25` produces `13.125`. Splitting finds the field; parsing makes it usable in arithmetic. section 5.3.2
Listing 5.8 Loading pizza sales data
function load_pizzatable(io)
pizzas = [] #1
readline(io) #2
while !eof(io) #3
pz = split(readline(io), ',')
pr = parse(Float64, price(pz)) #4
sz = portion(pz)
push!(pizzas, (name(pz), sz[1], pr)) #5
end
pizzas
end
The loading function puts these steps together: skip the header, read each line until end-of-file, split it, parse the price as `Float64`, and collect a pizza tuple. section 5.3.2
For numbers, the target type can be more specific: `parse(UInt8, "42")`, `parse(Int16, "42")`, and `parse(Float64, "0.42")` request different numeric types. Integer parsing assumes base 10 unless you specify another base: `parse(Int, "101")` is `101`, but `parse(Int, "101", base=2)` is `5`. To go the other direction, `string(5, base=2)` returns `"101"`. section 11.2.2 section 11.2.2
This approach is useful for small, known input layouts. A line-by-line pizza-sales loader demonstrates it; parsing a CSV file of rocket-engine data is a further input/output application. section 5.3.2 section 17.0












