# Problem updating a variable in a loop

**URL:** <https://forum.elixirforum.com/t/problem-updating-a-variable-in-a-loop/51195>\
**Category:** Questions\
**Tags:** opencv, evision\
**Created:** [October 20, 2022, 7:01am UTC](https://forum.elixirforum.com/t/problem-updating-a-variable-in-a-loop/51195 "2022-10-20T07:01:34Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gupta](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/g/ad7895/32.png) [@Gupta](https://forum.elixirforum.com/u/Gupta)\
**Post date:** [October 20, 2022, 7:01am UTC](https://forum.elixirforum.com/t/problem-updating-a-variable-in-a-loop/51195/1 "2022-10-20T07:01:34Z")

</div>

How i acn upadate the value of the variable “square=0” as mention in my code so that after every loop it will update it by addind + 1 in “square=0”

this is my code :

```elixir
def findcontours(path) do
    #squares=0
    {:ok, imgGrey} = OpenCV.imread(path, flags: OpenCV.cv_IMREAD_GRAYSCALE())
    {:ok, {_, thrash}} = OpenCV.threshold(imgGrey, 240, 255, OpenCV.cv_THRESH_BINARY())
    {:ok, {contours, _}} =OpenCV.findContours(thrash, OpenCV.cv_RETR_TREE(), OpenCV.cv_CHAIN_APPROX_NONE())

    square = 0
    for contour <- contours do
      {:ok,peri} = OpenCV.arcLength(contour,true)
      {:ok,approx} = OpenCV.approxPolyDP(contour, 0.01 * peri,true)
      ap=OpenCV.Nx.to_nx(approx)
      {len,_,_}=Nx.shape(ap)

      if len==4 do #to bound squares from the countor
        {:ok,{x1, y1, w, h}}= OpenCV.boundingRect(approx)
        aspectRatio = (w/1)/h
        if aspectRatio >= 0.95 and aspectRatio <= 1.05 and (y1+h)*(x1+w) < 985000 do
          square=square +1
        end
      end
    end
  end

```

pls help me to update my value of square as i know there i a big problem of scope in elixir and also it is immutable so pls help me for this.

or suggest me another way so that i can do the whole process by another way if possible.

---

<div class="post-metadata">

**Author:** ![Aetherus](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/aetherus/32/17203_2.png) [@Aetherus](https://forum.elixirforum.com/u/Aetherus)\
**Post date:** [October 20, 2022, 7:20am UTC](https://forum.elixirforum.com/t/problem-updating-a-variable-in-a-loop/51195/2 "2022-10-20T07:20:54Z")

</div>

In Elixir, variables are actually constants. You can’t reassign values to an existing variable. You can only create a new variable with the same name and shadow the old one.

This line `square = square + 1` reads the value of `square` outside the for-loop, adds 1 to it, and creates a new variable also named `square`, which is not used at all, because this new `square` variable is inside a sub-sub scope of the for-loop, and the old `square` is outside the for-loop. They are in different scopes.

How to fix this?

```elixir
square = for contour <- contours, reduce: 0 do
  acc ->
    {:ok,peri} = OpenCV.arcLength(contour,true)
    {:ok,approx} = OpenCV.approxPolyDP(contour, 0.01 * peri,true)
    ap=OpenCV.Nx.to_nx(approx)
    {len,_,_}=Nx.shape(ap)

    if len==4 do #to bound squares from the countor
      {:ok,{x1, y1, w, h}}= OpenCV.boundingRect(approx)
      aspectRatio = (w/1)/h
      if aspectRatio >= 0.95 and aspectRatio <= 1.05 and (y1+h)*(x1+w) < 985000 do
        acc + 1
      else
        acc
      end
    else
      acc
    end
end

```

---

<div class="post-metadata">

**Author:** ![Gupta](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/g/ad7895/32.png) [@Gupta](https://forum.elixirforum.com/u/Gupta)\
**Post date:** [October 20, 2022, 7:40am UTC](https://forum.elixirforum.com/t/problem-updating-a-variable-in-a-loop/51195/3 "2022-10-20T07:40:05Z")

</div>

can u explain me briefly what u have done in this

```elixir
square = for contour <- contours, reduce: 0 do
  acc ->
    {:ok,peri} = OpenCV.arcLength(contour,true)
    {:ok,approx} = OpenCV.approxPolyDP(contour, 0.01 * peri,true)
    ap=OpenCV.Nx.to_nx(approx)
    {len,_,_}=Nx.shape(ap)

    if len==4 do #to bound squares from the countor
      {:ok,{x1, y1, w, h}}= OpenCV.boundingRect(approx)
      aspectRatio = (w/1)/h
      if aspectRatio >= 0.95 and aspectRatio <= 1.05 and (y1+h)*(x1+w) < 985000 do
        acc + 1
      else
        acc
      end
    end
end

```

so that in further code if i will face problem regarding this i can rectify it

---

<div class="post-metadata">

**Author:** ![Aetherus](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/aetherus/32/17203_2.png) [@Aetherus](https://forum.elixirforum.com/u/Aetherus)\
**Post date:** [October 20, 2022, 8:36am UTC](https://forum.elixirforum.com/t/problem-updating-a-variable-in-a-loop/51195/4 "2022-10-20T08:36:27Z")

</div>

Here’s a simple example of the `for` comprehension with the option `:reduce`:

```elixir
for n <- 1..10, reduce: 0 do
  acc -> acc + n
end

# returns 55

```

It’s equivalent to this code (I’m using the same variable names as in the snippet above):

```elixir
Enum.reduce(1..10, 0, fn n, acc ->
  acc + n
end)

```

The `for` comprehension first passes `0`, the value of the option `:reduce`, to `acc`, the first element in `1..10` to `n`, and apply the anonymous function to `acc` and `n`. Then it passes the return value of the anonymous function to `acc`, the second element in `1..10` to `n`, and apply the anonymous function again. So on and so forth, until there’s no element to take from `1..10`, then it returns the return value of the last call of that anonymous function.

---

<div class="post-metadata">

**Author:** ![Aetherus](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/aetherus/32/17203_2.png) [@Aetherus](https://forum.elixirforum.com/u/Aetherus)\
**Post date:** [October 20, 2022, 8:42am UTC](https://forum.elixirforum.com/t/problem-updating-a-variable-in-a-loop/51195/5 "2022-10-20T08:42:49Z")

</div>

And note that both `for ... do ... end` and `if ... else ... end` are expressions, and they have return values.

The `if` without `else` returns whatever inside the `if` block returns, if the condition is met, otherwise, it returns `nil`, which usually is not what you want, so be careful.

---

<div class="post-metadata">

**Author:** ![Gupta](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/g/ad7895/32.png) [@Gupta](https://forum.elixirforum.com/u/Gupta)\
**Post date:** [October 22, 2022, 4:44pm UTC](https://forum.elixirforum.com/t/problem-updating-a-variable-in-a-loop/51195/6 "2022-10-22T16:44:01Z")

</div>

I was trying to use tesseract-Ocr in elixir  
but i am getting error pls :

```elixir
iex(4)> import TesseractOcr.Utils
TesseractOcr.Utils
iex(5)> path="lib/grid_1.png"    
"lib/grid_1.png"
iex(6)> TesseractOcr.read(path)  
** (ErlangError) Erlang error: :enoent
    (elixir 1.13.4) lib/system.ex:1044: System.cmd("tesseract", ["lib/grid_1.png", "stdout"], [])
    (tesseract_ocr 0.1.5) lib/tesseract_ocr.ex:19: TesseractOcr.read/2
iex(6)>

```

pls help me to rectify it

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/32/38664_2.png) [@dimitarvp](https://forum.elixirforum.com/u/dimitarvp)\
**Post date:** [October 22, 2022, 5:59pm UTC](https://forum.elixirforum.com/t/problem-updating-a-variable-in-a-loop/51195/7 "2022-10-22T17:59:24Z")

</div>

> <https://stackoverflow.com/questions/19902828/why-does-enoent-mean-no-such-file-or-directory>

It means the file path you specified does not exist.

Use [`File.exists?`](https://hexdocs.pm/elixir/1.14.1/File.html#exists?/2) inside `iex` to find the right file path.

---

<div class="post-metadata">

**Author:** ![Gupta](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/g/ad7895/32.png) [@Gupta](https://forum.elixirforum.com/u/Gupta)\
**Post date:** [October 22, 2022, 6:46pm UTC](https://forum.elixirforum.com/t/problem-updating-a-variable-in-a-loop/51195/8 "2022-10-22T18:46:47Z")

</div>

> [@Gupta](#):
>
> `TesseractOcr.read(path)`

I am getting error again :

```elixir
iex(6)> import TesseractOcr.Utils                                     
TesseractOcr.Utils
iex(7)> path="/home/erts/Downloads/task1b_evision_resource/grid_1.png"
"/home/erts/Downloads/task1b_evision_resource/grid_1.png"
iex(8)> File.exists?(path)                                            
true
iex(9)> TesseractOcr.read(path)
** (ErlangError) Erlang error: :enoent
    (elixir 1.13.4) lib/system.ex:1044: System.cmd("tesseract", ["/home/erts/Downloads/task1b_evision_resource/grid_1.png", "stdout"], [])
    (tesseract_ocr 0.1.5) lib/tesseract_ocr.ex:19: TesseractOcr.read/2
iex(9)> 
nil

```

why it is showing again this .

also i think i have the tesseract dependencies in mix.exs

```elixir
  defp deps do
    [
      {:evision, "~> 0.1.0-dev", github: "cocoa-xu/evision", branch: "main"},
      {:nx, "~> 0.2"},
      {:tesseract_ocr, "~> 0.1.5"}
    ]
  end
end

```

but still it is showing me the error

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/32/38664_2.png) [@dimitarvp](https://forum.elixirforum.com/u/dimitarvp)\
**Post date:** [October 22, 2022, 7:01pm UTC](https://forum.elixirforum.com/t/problem-updating-a-variable-in-a-loop/51195/9 "2022-10-22T19:01:57Z")

</div>

It’s expecting you to have `tesseract` installed and put in the system PATH.

What does `which tesseract` show you when you run it in a terminal?

---

<div class="post-metadata">

**Author:** ![Gupta](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/g/ad7895/32.png) [@Gupta](https://forum.elixirforum.com/u/Gupta)\
**Post date:** [October 25, 2022, 6:40pm UTC](https://forum.elixirforum.com/t/problem-updating-a-variable-in-a-loop/51195/10 "2022-10-25T18:40:30Z")

</div>

This the path i have added in my system.

```elixir
erts@eyrc:~/Downloads/task1b_evision_resource$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/erts/Downloads/task1b_evision_resource/deps/tesseract_ocr
erts@eyrc:~/Downloads/task1b_evision_resource$ ^C

```

and this is my **file location** and **location of my image grid\_1**.png is also there:

 ![tesseract file location](https://forum.elixirforum.com/uploads/default/original/3X/4/c/4c65df05dffa3c51ee9c87fe911f1983c837a210.png)

and this is my **code with error** i have written :

```elixir
erts@eyrc:~/Downloads/task1b_evision_resource$ iex -S mix
Erlang/OTP 25 [erts-13.0.4] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:1] [jit:ns]

Compiling 1 file (.ex)
Interactive Elixir (1.13.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> import TesseractOcr.Utils
TesseractOcr.Utils
iex(2)> path="/home/erts/Downloads/task1b_evision_resource/grid_1.png"
"/home/erts/Downloads/task1b_evision_resource/grid_1.png"
iex(3)> TesseractOcr.read(path)                                       
** (ErlangError) Erlang error: :enoent
    (elixir 1.13.4) lib/system.ex:1044: System.cmd("tesseract", ["/home/erts/Downloads/task1b_evision_resource/grid_1.png", "stdout"], [])
    (tesseract_ocr 0.1.5) lib/tesseract_ocr.ex:19: TesseractOcr.read/2
iex(3)> 

```

Now I am not getting where is the error , i am not able to read a single image. 😔

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/32/38664_2.png) [@dimitarvp](https://forum.elixirforum.com/u/dimitarvp)\
**Post date:** [October 25, 2022, 6:49pm UTC](https://forum.elixirforum.com/t/problem-updating-a-variable-in-a-loop/51195/11 "2022-10-25T18:49:17Z")

</div>

Have you run `which tesseract`? If so, can you paste the output of the command?

---

<div class="post-metadata">

**Author:** ![Gupta](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/g/ad7895/32.png) [@Gupta](https://forum.elixirforum.com/u/Gupta)\
**Post date:** [October 25, 2022, 6:59pm UTC](https://forum.elixirforum.com/t/problem-updating-a-variable-in-a-loop/51195/12 "2022-10-25T18:59:06Z")

</div>

I am getting this :

```elixir
erts@eyrc:~/Downloads/task1b_evision_resource$ which tesseract
erts@eyrc:~/Downloads/task1b_evision_resource$ 

```

no output is there

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/32/38664_2.png) [@dimitarvp](https://forum.elixirforum.com/u/dimitarvp)\
**Post date:** [October 25, 2022, 7:15pm UTC](https://forum.elixirforum.com/t/problem-updating-a-variable-in-a-loop/51195/13 "2022-10-25T19:15:00Z")

</div>

Then you have a very easy problem: find exactly in which directory the `tesseract` program binary resides and add that to $PATH. Because right now it’s not there.

---

<div class="post-metadata">

**Author:** ![Gupta](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/g/ad7895/32.png) [@Gupta](https://forum.elixirforum.com/u/Gupta)\
**Post date:** [October 25, 2022, 7:59pm UTC](https://forum.elixirforum.com/t/problem-updating-a-variable-in-a-loop/51195/14 "2022-10-25T19:59:03Z")

</div>

now its working
