# Putting results of diffrent arrays for one variable in one array

Basically i have this small piece of code when i get:

``````function scan_maker(A)
m = JuMP.Model(solver=ClpSolver(PrimalTolerance=1e-3, DualTolerance=1e-3, InfeasibleReturn=1, PresolveType=1))
# m = Model(solver=GurobiSolver())
level = size(A, 2)
v = zeros(Int, level)
ub = zeros(Int, level)
lb = zeros(Int, level)

@variable(m, x[1:level])
@constraint(m, con, A*x.>=0)

function setc(c)
for i = 1:size(A, 1)
m.linconstr[i].lb = float(c[i])
end
end

function scan()
i = 1
init = 1
while i > 0
if i >= init
@objective(m, Max, x[i])
res = JuMP.solve(m, suppress_warnings=true)
if res==:Optimal || res==:Unbounded
ub[i] = round(Int, getvalue(x[i]))
setobjectivesense(m, :Min)
res = JuMP.solve(m, suppress_warnings=true)
@assert res==:Optimal || res==:Unbounded
lb[i] = round(Int, getvalue(x[i]))

v[i] = lb[i]
init += 1
else
@assert res==:Infeasible
i -= 1
continue
end
elseif v[i] < ub[i]
v[i] += 1
else
setupperbound(x[i], Inf)
setlowerbound(x[i], -Inf)
init -= 1
i -= 1
continue
end

if i >= level
vs = []
for v in vs
push!(vs,v)
end

@show(vs)
continue

else
setupperbound(x[i], v[i])
setlowerbound(x[i], v[i])
i += 1
end
end

end

return setc, scan
end
``````

i get this results :

``````vs = [[-1, 5]]
vs = [[0, 3]]
vs = [1, 1]]
vs = [[1, 2]]
``````

now i want to get them as one array of multiple arrays as elements, something like this:
`vs=[[-1,5], [0, 3],[1, 1],[1, 2]]`
i’m facing problems to get that

you can have an empty `vs=[]` outside and then inside the loop: ` push!(vs, v)`

1 Like

in this case i get :

``````vs = [[-1, 5]]
vs = [[0, 3]]
vs = [1, 1]]
vs = [[1, 2]]
``````
``````julia> vs = []
0-element Array{Any,1}

julia> for i = 1:10
if rand()>0.5
v = rand(2)
push!(vs, v)
end
end

julia> vs
6-element Array{Any,1}:
[0.14439881432341228, 0.22161669979901455]
[0.09006815010462144, 0.6563286253310212]
[0.4658664346920076, 0.3135735783668123]
[0.8744194417513451, 0.9958832473845809]
[0.8495821037715046, 0.45366928857817346]
[0.8439848284221538, 0.9645444501222658]
``````

I assume your mistake is that you’re remaking `vs=[]` empty every loop

1 Like

when i do this :

``````                vs = []
for i=1:length(v)
push!(vs,v)
end

@show(vs)
``````

this is what i get:

``````vs = Any[[-1, 5], [-1, 5]]
vs = Any[[0, 3], [0, 3]]
vs = Any[[1, 1], [1, 1]]
vs = Any[[1, 2], [1, 2]]
``````

you’re pushing many of the same `v` into `vs`, can you paste more code? I mean, you saw how my example worked, you just gotta match them

1 Like

i have no problem of posting more code which i will do by editing the question i just edited it

clearly you’re re-defining `vs = []` every `while i > 0`

and why are you pushing nothing into itself?

``````        vs = []
for v in vs #vs here is empty, as defined in last line
push!(vs,v)
end
``````
1 Like

yes that was inside a `while` loop, isn’t it possible inside `while` loop?

The kind of loop isn’t important. The general pattern of this construction is

``````collection = []
<some loop construction that produces items>
...
push!(collection, item)
...
end
``````

In this case the loop that produces items is the `while i > 0` on the third line of your `scan` function, so the initialization `vs = []` must happen before that, i.e. at the very start of `scan`. Then inside the `if i >= level` condition you should only do `push!(vs, v)`.

1 Like

in the code that i posted even when i put an empty array at the very begining of the function and put show after the loop i get only the last array in the following form:

``````vs = Any[[1, 2], [1, 2], [1, 2], [1, 2]]

4-element Array{Any,1}:
[1, 2]
[1, 2]
[1, 2]
[1, 2]
``````

and i’m supposed to get :

``````[[-1,5],[0,3],[1,1],[1,2]]
``````

Right, you are repeatedly modifying the same vector. In that case you need to do

``````push!(vs, copy(v))
``````

to save the contents you had at respective time.

1 Like

yeas that’s what i did and it worked, finally!! 