# Extract vector of values from vector of T

**URL:** <https://discourse.julialang.org/t/extract-vector-of-values-from-vector-of-t/9693>\
**Category:** New to Julia\
**Created:** [March 13, 2018, 6:22pm UTC](https://discourse.julialang.org/t/extract-vector-of-values-from-vector-of-t/9693 "2018-03-13T18:22:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pier/32/7335_2.png) [@Pier](https://discourse.julialang.org/u/Pier)\
**Post date:** [March 13, 2018, 6:22pm UTC](https://discourse.julialang.org/t/extract-vector-of-values-from-vector-of-t/9693/1 "2018-03-13T18:22:56Z")

</div>

Imagine I have

```julia
type T
   x::Array{Float64,1}
   f::Array{Float64,1}
   T()=new(zeros(3),zeros(2))
end

v=Array{T}(10)

```

I would then like to write something like

```julia
x1[1:10]=v[1:10].x[1]

```

this doesn’t work because “type Array has no field x”. I can certainly extract all the x[1] from the individual members of the array v and put them in a vector x1 with a loop but I was wondering if there is a more Julia-esque way of doing it.

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [March 13, 2018, 6:30pm UTC](https://discourse.julialang.org/t/extract-vector-of-values-from-vector-of-t/9693/2 "2018-03-13T18:30:51Z")

</div>

```julia
v=Array{T}(10)

```

Note that this doesn’t create any `T` objects (elements of `v` are `#undef`). You probably want

```julia
v = [T() for _ = 1 : 10]

```

Then to get the `x1`s you can do e.g.

```julia
map(t -> t.x[1], v)

```

or

```julia
(t -> t.x[1]).(v)

```

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [March 13, 2018, 6:34pm UTC](https://discourse.julialang.org/t/extract-vector-of-values-from-vector-of-t/9693/3 "2018-03-13T18:34:00Z")

</div>

Interestingly, this is a good example of why the `Array` constructors have been changed in 0.7. In 0.7, to declare `v` the way you did you’d have to explicitly do

```julia
v = Array{T}(undef, 10)

```

---

<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 13, 2018, 6:35pm UTC](https://discourse.julialang.org/t/extract-vector-of-values-from-vector-of-t/9693/4 "2018-03-13T18:35:01Z")

</div>

> `v = Array{T}(undef, 10)`

Fixed that for you 🙂

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [March 13, 2018, 6:39pm UTC](https://discourse.julialang.org/t/extract-vector-of-values-from-vector-of-t/9693/5 "2018-03-13T18:39:34Z")

</div>

Oh god, did this seriously just get changed? I had done so much inserting of `uninitialized`… When did this happen?

---

<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 13, 2018, 6:40pm UTC](https://discourse.julialang.org/t/extract-vector-of-values-from-vector-of-t/9693/6 "2018-03-13T18:40:33Z")

</div>

[https://github.com/JuliaLang/julia/pull/26316](https://github.com/JuliaLang/julia/pull/26316)

Search and replacing `uninitialized´ should be fairly easy though.  
Took me only a few minutes for whole of Base.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [March 13, 2018, 6:43pm UTC](https://discourse.julialang.org/t/extract-vector-of-values-from-vector-of-t/9693/7 "2018-03-13T18:43:50Z")

</div>

Well, not sure I would have voted for `undef` but `uninitialized` is definitely one of the hardest words to type in the English language, so I’m not complaining.

---

<div class="post-metadata">

**Author:** ![Pier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pier/32/7335_2.png) [@Pier](https://discourse.julialang.org/u/Pier)\
**Post date:** [March 13, 2018, 7:00pm UTC](https://discourse.julialang.org/t/extract-vector-of-values-from-vector-of-t/9693/8 "2018-03-13T19:00:16Z")

</div>

Yes, I knew that my elements of v are #undef, just wanted to keep the description to a minimum. The interesting part for me was the

map(t → t.x[1], v)

Thanks! (for all the help I am receiving here I owe you all beer/coffee/whatever… ring a bell if you ever come to Padua/Venice, Italy)

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 13, 2018, 7:46pm UTC](https://discourse.julialang.org/t/extract-vector-of-values-from-vector-of-t/9693/9 "2018-03-13T19:46:07Z")

</div>

By the way, all of the suggestions here are good ones, but it’s worth noting that they’re all just helpful shortcuts for extracting each `x` element in a loop. One of the nicest things about Julia is that your first instinct (“extract all the x[1] from the individual members of the array v and put them in a vector x1 with a loop”) is basically the right thing to do and will be just as efficient as the “built-in” solutions like `map`. The `map` function is just nice because it creates that loop for you and makes the intent of your code clear.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 14, 2018, 5:31am UTC](https://discourse.julialang.org/t/extract-vector-of-values-from-vector-of-t/9693/10 "2018-03-14T05:31:07Z")

</div>

This is a lengthy thread, but you might find the discussion interesting

> [@Get values of symbols inside a function](https://discourse.julialang.org/t/get-values-of-symbols-inside-a-function/848):
>
> Hi guys! I have one problem: if I have an array of symbols, how can I get their values inside a function scope? Something like this: function f(a::Array{Symbols,1}) a = 1 b = 2 c = 3 # Get the values of the symbols in `a` and return. end f([:a,:b]) Then it should return [1;2]. I know that eval will not work because it operates at global scope. So, is there any way to do this? Thanks, Ronan
