# Array of structs can be seen in the outer loop only

**URL:** <https://discourse.julialang.org/t/array-of-structs-can-be-seen-in-the-outer-loop-only/36615>\
**Category:** General Usage\
**Created:** [March 27, 2020, 8:47pm UTC](https://discourse.julialang.org/t/array-of-structs-can-be-seen-in-the-outer-loop-only/36615 "2020-03-27T20:47:39Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [March 27, 2020, 8:47pm UTC](https://discourse.julialang.org/t/array-of-structs-can-be-seen-in-the-outer-loop-only/36615/1 "2020-03-27T20:47:39Z")

</div>

Hello guys,

I have an array of structs and I had no problem with it until I made a double loop  
my array of structs is not defined inside the inner loop whereas in the outer loop, its is fine  
How can this be explained? why does this happen…  
look here

 ![wth](https://global.discourse-cdn.com/julialang/original/3X/c/6/c6cbd9691c7c351cbd3034a9e660bdc1c3abb0d5.png)

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 27, 2020, 8:54pm UTC](https://discourse.julialang.org/t/array-of-structs-can-be-seen-in-the-outer-loop-only/36615/2 "2020-03-27T20:54:04Z")

</div>

Please post code instead of a screenshot.

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [March 27, 2020, 9:02pm UTC](https://discourse.julialang.org/t/array-of-structs-can-be-seen-in-the-outer-loop-only/36615/3 "2020-03-27T21:02:28Z")

</div>

```julia
using CSV
df=CSV.read("F:/B/Mayar/lineData.CSV")

mutable struct Linestruct
    buses::Vector{Int}
    res::Float64
    ind::Float64
    imp_mag::Float64
    imp_angle::Float64
    y::Complex{Float64}
    p::Float64
    q::Float64
    state::String
end
CREATE_Linestruct() = Linestruct([0,0],0.0,0.0,0.0,0.0,0.0,0.0,0.0,"unknown")
#Linestruct(buses_line, res_line, ind_line) = Linestruct(buses_line, res_line,
    #ind_line, 0.0, 0.0, 0,0, 0.0, "unknown")
#l2 = Linestruct([1,2,3], 0.0, 0.0)

l3=CREATE_Linestruct()
number_lines=size(df,1)
x=1;
array_lines=Array{Linestruct,1}()

for x in 1:number_lines
temp_struct=CREATE_Linestruct()
push!(array_lines,temp_struct)
x=10
end

f=0
for x in 1:number_lines
#*#*#*#*#*#*#*#*# physical values #*#*#*#*#*#*#*#
array_lines[x].res=df[x,3] # line resistance
array_lines[x].ind=df[x,4] # line inductance
#*#*#*#*#*#*#*#*# impedance calculation - magnitude #*#*#*#*#*#*#*#
temp_mag=(array_lines[x].res)^2+(array_lines[x].ind)^2
array_lines[x].imp_mag=round(sqrt(temp_mag),digits=5)
#*#*#*#*#*#*#*#*# impedance calculation - angle #*#*#*#*#*#*#*#
temp_ang=atan(array_lines[x].ind,array_lines[x].res)
array_lines[x].imp_angle=round(temp_ang,digits=5)
#*#*#*#*#*#*#*#*# line admittance #*#*#*#*#*#*#*#
# begin by calculating y magnitude as the reciprocal of impedance
temp_y_mag=1/array_lines[x].imp_mag
# then calculate the angle of line admittance by putting a negative
temp_y_angle=-1*array_lines[x].imp_angle
# x= rcostheta # y=rsintheta
temp_x=round(temp_y_mag*cos(temp_y_angle),digits=5)
temp_y=round(temp_y_mag*sin(temp_y_angle),digits=5)
array_lines[x].y=temp_x+(temp_y)im
#*#*#*#*#*#*#*#*# Joining busses #*#*#*#*#*#*#*#
array_lines[x].buses=[df[x,1],df[x,2]] # buses
end
#*#*#*#*#*#*#*#*#*#*#* generating the ybus *#*#*#*#*#*#*#*#
x=1;
xx=1;
number_buses=3 # to try only
y_bus=zeros(Complex{Float64},number_buses,number_buses)
for x in 1:number_buses
#outer loop to create Y11, Y22, Y33 ....YNN
temp_accumulator=0+0im
      for xx in 1:number_lines
#inner loop
            if (array_lines[xx].buses[1]===x) || array_lines.buses[2]===x
                  temp_accumulator=array_lines[xx].y
                  y_bus[x,x]=temp_accumulator+array_lines[xx].y

            end

      end

end

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 27, 2020, 9:09pm UTC](https://discourse.julialang.org/t/array-of-structs-can-be-seen-in-the-outer-loop-only/36615/4 "2020-03-27T21:09:52Z")

</div>

The next step is to make it reproducible without that file. Someone should be able to copy paste it into the REPL and have the error reproduce.

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [March 27, 2020, 9:10pm UTC](https://discourse.julialang.org/t/array-of-structs-can-be-seen-in-the-outer-loop-only/36615/5 "2020-03-27T21:10:33Z")

</div>

sorry, I do not get that

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [March 27, 2020, 9:12pm UTC](https://discourse.julialang.org/t/array-of-structs-can-be-seen-in-the-outer-loop-only/36615/6 "2020-03-27T21:12:10Z")

</div>

We don’t have that .csv flie on your computer.

You should construct a DataFrame that _looks_ like the file on your computer using your script. Something like

```julia
df = DataFrame(a = rand(10), b = rand(10))

```

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [March 27, 2020, 9:14pm UTC](https://discourse.julialang.org/t/array-of-structs-can-be-seen-in-the-outer-loop-only/36615/8 "2020-03-27T21:14:05Z")

</div>

aha Okay, I will

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [March 27, 2020, 9:15pm UTC](https://discourse.julialang.org/t/array-of-structs-can-be-seen-in-the-outer-loop-only/36615/9 "2020-03-27T21:15:57Z")

</div>

I need to create a data frame with size of 3 x 6 .

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [March 27, 2020, 9:19pm UTC](https://discourse.julialang.org/t/array-of-structs-can-be-seen-in-the-outer-loop-only/36615/10 "2020-03-27T21:19:11Z")

</div>

```julia
df = DataFrame(A = 1:3, B = 4:6,C=7:9,D=10:12,E=13:15,F=16:18])

```

---

<div class="post-metadata">

**Author:** ![Pbellive](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbellive/32/3604_2.png) [@Pbellive](https://discourse.julialang.org/u/Pbellive)\
**Post date:** [March 27, 2020, 11:22pm UTC](https://discourse.julialang.org/t/array-of-structs-can-be-seen-in-the-outer-loop-only/36615/11 "2020-03-27T23:22:55Z")

</div>

I agree with the above replies that it’s a lot easier to help when you post a self-contained piece of code that reproduces the error you’re seeing. However, looking at the code in your second post and then at the error message in your first post, I think I see what’s happening. You have a variable `array_lines`, which is a vector with elements of type `Linestruct`. `Linestruct` has a field called `buses`. The error in your first post is not saying that your array is not defined. It is saying that the type `Array` has no field called `buses`. This occurs because in your inner loop you have the statement

```julia
array_lines.buses[2]===x

```

which I think is a typo. It should probably be

```julia
array_lines[xx].buses[2]===x

```

`array_lines` is just an array. The array itself has no field called `buses`.
