# Find the smallest number in a file

**URL:** <https://discourse.julialang.org/t/find-the-smallest-number-in-a-file/122219>\
**Category:** General Usage\
**Created:** [November 4, 2024, 11:39am UTC](https://discourse.julialang.org/t/find-the-smallest-number-in-a-file/122219 "2024-11-04T11:39:30Z")\
**Posts on this page:** 14\
**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 4, 2024, 11:39am UTC](https://discourse.julialang.org/t/find-the-smallest-number-in-a-file/122219/1 "2024-11-04T11:39:31Z")

</div>

Hello,  
I have a file like below:

```julia
1
-1
2
-2
3
4
-4

```

I want to find the smallest number in this file and I wrote the following program:

```julia
function smaller()
    small = 0
    for line in eachline("file.txt")
        if (small == 0)
        small = line
        elseif (small > line)
            small = line
        end
    end
    println(small)
end
smaller()

```

I am a newbie and I want to know where my problem is and I don’t want to use other methods.

Thank you.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [November 4, 2024, 11:51am UTC](https://discourse.julialang.org/t/find-the-smallest-number-in-a-file/122219/2 "2024-11-04T11:51:51Z")

</div>

Some tips:

- is `line` from `eachline` a number or a string?
- the only case you’d want to update `small` with `line` is if `small > line`, so your if and else statements could be simpler
- traditionally, one might expect your `smaller` function to accept the file name and return the smallest integer (i.e. not print it out).
- I don’t know for sure, but I bet that the error messages you got would have helped you in improving this code. Maybe you can match the messages to some of these tips?

---

<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 4, 2024, 12:15pm UTC](https://discourse.julialang.org/t/find-the-smallest-number-in-a-file/122219/3 "2024-11-04T12:15:13Z")

</div>

Hi,  
Thank you so much for you reply.  
The file contains the same numbers that I wrote above. Ah, numbers are string:

```julia
 println(typeof(line))
> string

```

I changed the code to below, but I got an error:

```julia
small = parse(Int, line)

```

Why?

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [November 4, 2024, 12:28pm UTC](https://discourse.julialang.org/t/find-the-smallest-number-in-a-file/122219/4 "2024-11-04T12:28:20Z")

</div>

I do not know:

```julia
julia> for line in eachline("file.txt")
       i = parse(Int, line)
       println(i)
       end
1
-1
2
-2
3
4
-4

```

is there an empty line in that file?

---

<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 4, 2024, 5:43pm UTC](https://discourse.julialang.org/t/find-the-smallest-number-in-a-file/122219/5 "2024-11-04T17:43:20Z")

</div>

> [@hack3rcon](#):
>
> I changed the code to below, but I got an error:

What precisely did you change? If you just replaced the two occurrences of `small = line` by `small = parse(Int, line)`, then the comparison `small > line` would yield an error, as you are comparing an `Int` (`small`) to a `String` (`line`).

---

<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 5, 2024, 7:15am UTC](https://discourse.julialang.org/t/find-the-smallest-number-in-a-file/122219/6 "2024-11-05T07:15:45Z")

</div>

Hi,  
Thank you so much for your note.  
I changed the code as follows:

```julia
function smaller()
    small = 0
    for line in eachline("file.txt")
        if (small == 0)
        small = parse(Int,line)
        elseif (small > parse(Int,line))
            small = parse(Int,line)
        end
    end
    println(small)
end
smaller()

```

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [November 5, 2024, 7:48am UTC](https://discourse.julialang.org/t/find-the-smallest-number-in-a-file/122219/7 "2024-11-05T07:48:19Z")

</div>

Another note for style: You can simplify your logic if you initialize `small` with the greatest possible number. Then you do don’t need the `if small == 0` stuff and only need the comparison 🙂

You can get the biggest value of a datatype with `typemax(datatype)` so in your case that’s `typemax(Int)`.

---

<div class="post-metadata">

**Author:** ![tecosaur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tecosaur/32/23206_2.png) [@tecosaur](https://discourse.julialang.org/u/tecosaur)\
**Post date:** [November 5, 2024, 8:30am UTC](https://discourse.julialang.org/t/find-the-smallest-number-in-a-file/122219/8 "2024-11-05T08:30:01Z")

</div>

The need for parsing has already been mentioned, but I thought I’d just add that you can solve this in a one-liner:

```julia
julia> minimum(l -> parse(Int, l), eachline("/tmp/numlist"), init=0)
-4

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [November 5, 2024, 9:29am UTC](https://discourse.julialang.org/t/find-the-smallest-number-in-a-file/122219/9 "2024-11-05T09:29:09Z")

</div>

> [@tecosaur](#):
>
> `init=0`

Initializing with zero seems like a bad idea. What if all the numbers in the file are positive?

---

<div class="post-metadata">

**Author:** ![tecosaur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tecosaur/32/23206_2.png) [@tecosaur](https://discourse.julialang.org/u/tecosaur)\
**Post date:** [November 5, 2024, 9:57am UTC](https://discourse.julialang.org/t/find-the-smallest-number-in-a-file/122219/10 "2024-11-05T09:57:39Z")

</div>

I’m just matching the behavior in the original code, which initialised `small = 0`. Personally I’d go with:

```julia
minimum(Iterators.filter(!isnothing, Iterators.map(l -> tryparse(Int, l), eachline("file")), init=typemin(Int))

```

---

<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 5, 2024, 10:29am UTC](https://discourse.julialang.org/t/find-the-smallest-number-in-a-file/122219/11 "2024-11-05T10:29:48Z")

</div>

> [@tecosaur](#):
>
> I’m just matching the behavior in the original code, which initialised `small = 0`

The original code overwrites the initialisation in the first iteration step, which `minimum(..., init=...)` does not. So this is not equivalent, and the original code (with `parse`) is correct (though not particularly concise).

> [@tecosaur](#):
>
> `minimum(..., init=typemin(Int))`

You probably meant `typemax(Int)`, cf. @abraemer 's post. Alternatively, you could simply leave out the explicit `init`.

---

<div class="post-metadata">

**Author:** ![tecosaur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tecosaur/32/23206_2.png) [@tecosaur](https://discourse.julialang.org/u/tecosaur)\
**Post date:** [November 5, 2024, 10:34am UTC](https://discourse.julialang.org/t/find-the-smallest-number-in-a-file/122219/12 "2024-11-05T10:34:16Z")

</div>

> [@eldee](#):
>
> You probably meant `typemax(Int)`

I did indeed.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 5, 2024, 12:26pm UTC](https://discourse.julialang.org/t/find-the-smallest-number-in-a-file/122219/13 "2024-11-05T12:26:17Z")

</div>

Independently of the one-liners above, which might be confusing, the simple loop is probably written like this (and it is perfectly fine):

```julia
function smaller(file) # provide the file as a parameter to the function
    small = typemax(Int)
    for line in eachline(file)
        small = min(small, parse(Int, line))
    end
    return small
end

```

To be run with:

```julia-repl
julia> smaller("file.txt")

```

And there’s nothing wrong in substituting the `min(...)` by

```julia
    value = parse(Int,line)
    if value < small
        small = value
    end

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [November 5, 2024, 12:27pm UTC](https://discourse.julialang.org/t/find-the-smallest-number-in-a-file/122219/14 "2024-11-05T12:27:17Z")

</div>

> [@tecosaur](#):
>
> I’m just matching the behavior in the original code

My philosophy is to tell people when their code/question is wrong or misguided, rather than to just give them what they are asking for😉
