# How to use the iterator with strings with NamedTuple?

**URL:** https://discourse.julialang.org/t/how-to-use-the-iterator-with-strings-with-namedtuple/23730
**Category:** General Usage
**Created:** [May 1, 2019, 10:44am UTC](https://discourse.julialang.org/t/how-to-use-the-iterator-with-strings-with-namedtuple/23730 "2019-05-01T10:44:55Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)
#### Post date: [May 1, 2019, 10:44am UTC](https://discourse.julialang.org/t/how-to-use-the-iterator-with-strings-with-namedtuple/23730/1 "2019-05-01T10:44:55Z")

</div>

I have NamedTuple with many Arrays  
`kolumny`

julia\> kolumny.M1  
1020-element Array{Any,1}:  
2.0  
…  
1.0

````julia
Is OK ,is working 

But my iterator B is a array of string
```julia> B
5-element Array{String,1}:
 "M0"
 "M1"
 "M2"
 "M3"
 "M4"

````

How to use the B iterator with my NamedTuple ?

```julia
julia> kolumny.(B[2])
ERROR: MethodError: objects of type NamedTuple{(:M0, :id

julia> kolumny.(B[2]...,)
ERROR: MethodError: objects

```

Thanks for help , Paul

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [May 1, 2019, 10:46am UTC](https://discourse.julialang.org/t/how-to-use-the-iterator-with-strings-with-namedtuple/23730/2 "2019-05-01T10:46:59Z")

</div>

`getfield(kolumny, B[2])`

---

<div class="post-metadata">

### Author: ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)
#### Post date: [May 1, 2019, 10:57am UTC](https://discourse.julialang.org/t/how-to-use-the-iterator-with-strings-with-namedtuple/23730/3 "2019-05-01T10:57:19Z")

</div>

> [@yakir12](#):
>
> getfield(kolumny, B[2])

```julia
julia> getfield(kolumny,B[2])
ERROR: TypeError: in getfield, expected Symbol, got String
Stacktrace:
 [1] top-level scope at none:0

julia> getfield(kolumny,B[2]...,)
ERROR: TypeError: non-boolean (Char) used in boolean context
Stacktrace:
 [1] top-level scope at none:0

julia> typeof(B[2])
String

julia> eltype(B[2])
Char

```

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [May 1, 2019, 10:58am UTC](https://discourse.julialang.org/t/how-to-use-the-iterator-with-strings-with-namedtuple/23730/4 "2019-05-01T10:58:42Z")

</div>

```julia
getfield(kolumny, Symbol(B[2]))

```

---

<div class="post-metadata">

### Author: ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)
#### Post date: [May 1, 2019, 11:02am UTC](https://discourse.julialang.org/t/how-to-use-the-iterator-with-strings-with-namedtuple/23730/5 "2019-05-01T11:02:35Z")

</div>

Thanks, this working

```julia
getfield(kolumny, Symbol(B[2]))

```

I am starting with NamedTuples becouse the shortly formula `x.y[2]` loock nice and cleary ! But ` getfield(x, Symbol(y[2]))` is like normal code 😕 No way to short `x.y[2]` ?  
Paul

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [May 1, 2019, 11:07am UTC](https://discourse.julialang.org/t/how-to-use-the-iterator-with-strings-with-namedtuple/23730/6 "2019-05-01T11:07:21Z")

</div>

Not the way your code is setup now. But you could reconsider your choice to reference into the named tuple by referencing into an array of strings that represent the field/key of the named tuple. Instead, maybe you can keep your data in an array and just reference directly to that:

```julia
kolumny = [rand(1020) for _ in 1:5]
kolumny[1]
kolumny[2]
...

```

---

<div class="post-metadata">

### Author: ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)
#### Post date: [May 1, 2019, 11:12am UTC](https://discourse.julialang.org/t/how-to-use-the-iterator-with-strings-with-namedtuple/23730/7 "2019-05-01T11:12:35Z")

</div>

> [@yakir12](#):
>
> kolumny = [rand(1020) for \_ in 1:5]

Thanks,  
I can changing all, it is my first step. How to prepare vector y for using x.y[2], x.y[3]

I am looking for somethinkg like

```julia
for i in y
do something with x.i
end

```

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [May 1, 2019, 12:30pm UTC](https://discourse.julialang.org/t/how-to-use-the-iterator-with-strings-with-namedtuple/23730/8 "2019-05-01T12:30:48Z")

</div>

I don’t know what you mean by “vector y”. I suggest you read [this](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757) post and make a MWE. It’s really hard to follow what exactly you’re trying to do without a MWE or some context.
