# Question about concatenation using for loops

**URL:** https://discourse.julialang.org/t/question-about-concatenation-using-for-loops/52470
**Category:** New to Julia
**Tags:** question
**Created:** [December 27, 2020, 6:56pm UTC](https://discourse.julialang.org/t/question-about-concatenation-using-for-loops/52470 "2020-12-27T18:56:04Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Brian\_Becsi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian_becsi/32/19925_2.png) [@Brian\_Becsi](https://discourse.julialang.org/u/Brian_Becsi)
#### Post date: [December 27, 2020, 6:56pm UTC](https://discourse.julialang.org/t/question-about-concatenation-using-for-loops/52470/1 "2020-12-27T18:56:04Z")

</div>

Hello,  
I am new to coding in general. I know this is probably a simple question, but I’ve been looking through documentation for about an hour.

I have an array of eigenvalues, and I want to return a list of the real eigenvalues that fall between -1 and 1. In Python, I use a for loop and two if statements and the numpy function append to append the values to the end of an empty vector.

```nohighlight
 xvals = []
  for eval in evals:
    if(np.abs(np.imag(eval)) < 1e-6):
      xval = np.real(eval)
      if(-1 <= xval and xval <= 1):
        xvals.append(xval)

```

I’m translating this into Julia and I’m having a tough time. Here is my code so far:

```
e= length(evals)
xvals = []
xval=[]
for k=1:e
    if imag(evals[k]) < 1e-6 && imag(evals[k]) > -1e-6
        xval = real(evals[k])
        if xval <= 1 && xval >= -1
            xvals = hcat(xvals, xval)
        end
    end
    println(xvals)
end

```

Specifically, I’m getting a dimensions mismatch error “mismatch in dimension 1, expected 0 got 1”.

I’m also noticing if I try to concatenate an array in a for loop, that the for loop doesn’t seem to recognize global variables. For example the code:

```julia
A = [0,]
for i in 1:10
    A = hcat(A,i)
end

```

results in an error: a not defined in expression (starting at for loop).

So what is going on here? And, what is the preferred method to create such a vector?

Thanks!

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [December 27, 2020, 7:07pm UTC](https://discourse.julialang.org/t/question-about-concatenation-using-for-loops/52470/2 "2020-12-27T19:07:23Z")

</div>

First, the function you want is `push!`, not `hcat`: `push!` is the function to append a single element to an array, whereas `hcat` is for concatenating _matrices_ horizontally (by columns).

Second, you can accomplish what you are trying much more simply with:

```julia
xvals = [real(e) for e in evals if abs(imag(e)) < 1e-6 && -1 <= real(e) <= 1]

```

Third, you are hitting the infamous global scope rule: when writing to a global variable `A` in a loop, you need to declare it as `global A`:

```julia
A = [0,]
for i in 1:10
    global A = push!(A,i)
end

```

This is not needed in the REPL in Julia 1.5 (or in Jupyter in any version), and in scripts there is a clearer error message, so I’m guessing you are using a very old version of Julia? (If you are writing code inside functions rather than using globals in scripts, which is what you would do for most “serious” code, there is no need for any special declaration.)

---

<div class="post-metadata">

### Author: ![Brian\_Becsi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian_becsi/32/19925_2.png) [@Brian\_Becsi](https://discourse.julialang.org/u/Brian_Becsi)
#### Post date: [December 27, 2020, 7:21pm UTC](https://discourse.julialang.org/t/question-about-concatenation-using-for-loops/52470/3 "2020-12-27T19:21:58Z")

</div>

thank you for the help!

I’m using Julia 1.4.1.
