How to use the iterator with strings with NamedTuple?

I have NamedTuple with many Arrays

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


Is OK ,is working 

But my iterator B is a array of string
```julia> B
5-element Array{String,1}:

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
 [1] top-level scope at none:0

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

julia> typeof(B[2])

julia> eltype(B[2])
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] ?

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]

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

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