# How to read a line character by character?

**URL:** <https://discourse.julialang.org/t/how-to-read-a-line-character-by-character/122734>\
**Category:** New to Julia\
**Tags:** numbers, parsing, io\
**Created:** [November 17, 2024, 9:45am UTC](https://discourse.julialang.org/t/how-to-read-a-line-character-by-character/122734 "2024-11-17T09:45:11Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [November 17, 2024, 9:45am UTC](https://discourse.julialang.org/t/how-to-read-a-line-character-by-character/122734/1 "2024-11-17T09:45:11Z")

</div>

Hello,  
I have a file as follows:

```julia
1 2 3 4 5 6 7 8 9 10

```

I want to calculate the sum of the above numbers. With a command like below I read a whole line and not individual numbers:

```julia
inn = open("input.txt", "r")
inp = readline(inn)

```

How can I separate numbers on a line?

Thank you.

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [November 17, 2024, 11:18am UTC](https://discourse.julialang.org/t/how-to-read-a-line-character-by-character/122734/2 "2024-11-17T11:18:19Z")

</div>

- If you want to read the file `Char`acter by character, you can use `c = read(inn, Char)`.
- If you want to read the line, but split it into characters afterwards, you could just index the `inp` in your code (`inp[2]`), loop over it (`for c in inp`), or `collect` it.
- If you want to get a `Vector{Int}` (which is presumably what you’re after), use `parse.(Int, split(inp))` (with default `' '` for the second argument `dlm` (delimiter) of `split`).

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [November 17, 2024, 11:32am UTC](https://discourse.julialang.org/t/how-to-read-a-line-character-by-character/122734/3 "2024-11-17T11:32:01Z")

</div>

I would probably use `sum` on `eachmatch` directly so I don’t have to allocate a vector of strings with `split` or a vector of ints (`eachline` is also not the most performant because it allocates a string per line I think, but it depends on what you need whether more effort is worth it)

`input.txt`

```julia
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10 11
1 2 3 4 5 6 7 8 9 10 11 12
1 2 3 4 5 6 7 8 9 10 11 12 13

```

```julia
julia> map(eachline("input.txt")) do line
           sum(match -> parse(Int, match.match), eachmatch(r"\d+", line))
       end
4-element Vector{Int64}:
 55
 66
 78
 91

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 17, 2024, 1:07pm UTC](https://discourse.julialang.org/t/how-to-read-a-line-character-by-character/122734/4 "2024-11-17T13:07:39Z")

</div>

Using `eachsplit()` seems to be more performant in this case:

```julia
map(eachline("input.txt")) do line
    sum(x -> parse(Int, x), eachsplit(line))
end

```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [November 17, 2024, 2:38pm UTC](https://discourse.julialang.org/t/how-to-read-a-line-character-by-character/122734/5 "2024-11-17T14:38:42Z")

</div>

Ah right that one was added in 1.8! Nice

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [November 17, 2024, 3:58pm UTC](https://discourse.julialang.org/t/how-to-read-a-line-character-by-character/122734/6 "2024-11-17T15:58:17Z")

</div>

[here a related question](https://discourse.julialang.org/t/performance-of-splitting-string-and-parsing-numbers/92161/7)

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [November 17, 2024, 7:08pm UTC](https://discourse.julialang.org/t/how-to-read-a-line-character-by-character/122734/7 "2024-11-17T19:08:35Z")

</div>

In case the series of values ​​includes multi-digit elements

```julia
function sumchars(data)
    tot = 0
    lcv=0
    for e in data 
        if (e == ' ') 
            tot=tot+lcv
            lcv=0
        else
            lcv = lcv*10+codepoint(e)-0x30
        end
        
    end
    return tot+lcv
end

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [November 17, 2024, 7:30pm UTC](https://discourse.julialang.org/t/how-to-read-a-line-character-by-character/122734/8 "2024-11-17T19:30:24Z")

</div>

> [@rafael.guerra](#):
>
> Using `eachsplit()` seems to be more performant in this case:

Only for the parsing part the line of numbers

```julia

julia> s4
"1 2 3 4 5 6 7 8 9 10 11 12 13"

julia> @btime sum(x -> parse(Int, x), eachsplit(s4))
  739.683 ns (3 allocations: 96 bytes)
91

julia> @btime sumchars(s4)
  38.143 ns (0 allocations: 0 bytes)
91

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 17, 2024, 7:48pm UTC](https://discourse.julialang.org/t/how-to-read-a-line-character-by-character/122734/9 "2024-11-17T19:48:09Z")

</div>

Simple and fast, if no negative integer or integer power such as 2^8 is provided.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [November 17, 2024, 8:29pm UTC](https://discourse.julialang.org/t/how-to-read-a-line-character-by-character/122734/10 "2024-11-17T20:29:17Z")

</div>

> [@rafael.guerra](#):
>
> if no negative integer or integer power such as 2^8 is provided

ok for the first point. But why should powers of 2 cause problems?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 17, 2024, 8:48pm UTC](https://discourse.julialang.org/t/how-to-read-a-line-character-by-character/122734/11 "2024-11-17T20:48:42Z")

</div>

I meant that the string “2^8”, or “2\*10^9”, is a valid input integer that would not be parsed.  
**Edit:** my bad, `parse(Int, "2^8")` errors too ☹

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [November 19, 2024, 6:33am UTC](https://discourse.julialang.org/t/how-to-read-a-line-character-by-character/122734/12 "2024-11-19T06:33:45Z")

</div>

Hi,  
Thank you so much.  
Which part of this code is wrong?

```julia
function summer()
    inn = open("input.txt","r")
    c = 0 
    sum = 0
    while !eof(inn)
        c = parse(Int, read(inn, Char))
        sum += c
    end
    println(sum)
end
summer()

```

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [November 19, 2024, 6:59am UTC](https://discourse.julialang.org/t/how-to-read-a-line-character-by-character/122734/13 "2024-11-19T06:59:48Z")

</div>

Since you read character by character, you would read a line just containing `10`  
as `"1"` and `"0"` so your sum would be `1` not `10`.

You have to follow the hints atop, real the whole line, split it at the spaces and then parse the individual numbers, which are often strings longer than one character.

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [November 19, 2024, 10:46am UTC](https://discourse.julialang.org/t/how-to-read-a-line-character-by-character/122734/14 "2024-11-19T10:46:48Z")

</div>

Apart from the problem of summing digits vs. summing numbers @kellertuer highlights, there’s also the issue that you are trying to convert a space to an integer.

```julia
ERROR: ArgumentError: invalid digit: ' '

```

* * *

If you really want to obtain the sum while reading the file character by character, you can use an approach similar to @rocco_sprmnt21 's `sumchars`, though you might want to change `codepoint(e) - 0x30` into `parse(Int, e)` for clarity.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [November 19, 2024, 11:27am UTC](https://discourse.julialang.org/t/how-to-read-a-line-character-by-character/122734/15 "2024-11-19T11:27:03Z")

</div>

> [@eldee](#):
>
> you might want to change `codepoint(e) - 0x30` into `parse(Int, e)` for clarity.

or

```julia
UInt8(e)-0x30

```

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [November 19, 2024, 11:53am UTC](https://discourse.julialang.org/t/how-to-read-a-line-character-by-character/122734/16 "2024-11-19T11:53:02Z")

</div>

Well, my point was mainly that for educational purposes it’s best to not use a ‘magic’ hexadecimal number 🙂 . (`Int(e) - Int('0')` would be clearer, but also that still requires a little bit of knowledge on character encoding.)
