How to use the iterator with strings with NamedTuple?

I have NamedTuple with many Arrays
kolumny

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

1.0

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> kolumny.(B[2])
ERROR: MethodError: objects of type NamedTuple{(:M0, :id

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

Thanks for help , Paul

getfield(kolumny, B[2])

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
getfield(kolumny, Symbol(B[2]))
1 Like

Thanks, this working

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 :confused: No way to short x.y[2] ?
Paul

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:

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

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

for i in y
do something with x.i
end

I don’t know what you mean by “vector y”. I suggest you read this post and make a MWE. It’s really hard to follow what exactly you’re trying to do without a MWE or some context.

1 Like