Indexing using tuples

I’m trying to use a tuple of strings for indexing a variable (in JuMP). I’m able to loop, etc. but can’t figure out how index a single element. Example, Products is my index tuple, x is my variable.

julia> Products
(“Product1”, “Product2”, “Product3”, “Product4”)

julia> x
1-dimensional DenseAxisArray{VariableRef,1,…} with index sets:
Dimension 1, (“Product1”, “Product2”, “Product3”, “Product4”)
And data, a 4-element Vector{VariableRef}:
x[Product1]
x[Product2]
x[Product3]
x[Product4]

julia>

I want to write x[“Product2”], but this does not work. x[Products[2]] works, but I want to use the actual name, “Product2”.

looks identical to me i.e. should just work, can you post runnable code?

It seems to have to do with the way the tuple is generated.

When I “hardcode” the tuple, then it works:

julia> Products = (“Product1”, “Product2”, “Product3”, “Product4”)
(“Product1”, “Product2”, “Product3”, “Product4”)

But if I construct the tuple by reading from a file, it does not work:

julia> Products = tuple(Example3_df[:,1]…); # Names of products

julia> Products
(“Product1”, “Product2”, “Product3”, “Product4”)

But the tuple (Products) looks identical to me!

What is typeof(Products) when you hardcode it and when you construct it from the file?

There is a small difference:
julia> Products = tuple(data_df[:,1]…)
(“Product1”, “Product2”, “Product3”, “Product4”)

julia> typeof(Products)
NTuple{4, String15}

julia> Products = (“Product1”, “Product2”, “Product3”, “Product4”)
(“Product1”, “Product2”, “Product3”, “Product4”)

julia> typeof(Products)
NTuple{4, String}

GitHub - JuliaStrings/InlineStrings.jl: Fixed-width string types for Julia gripe I guess…the bandaid solution right now is to String.(tuple(data_df[:,1]…)) probably

2 Likes