Accessing an array of strings with the value of integer variable

25 views
Skip to first unread message

Nevena Gincheva

unread,
May 12, 2024, 7:23:12 AMMay 12
to MiniZinc
Hello,

Is there a way I can use the solution of an integer variable to access an element of the array with strings and print that element in the output?
I have searched through the documentation, however, I could only see obtaining the value of a variable as a string with show(), but then I can not use it as an index in the array.

So in the output, I want  to do something like this:
% Instance data
array[_] of string: Names
var int: selectedName;

output["Selected name" + Names[ obtain_solution_of_as_int(selectedName) ]...

However, this does not work, as it does not permit to have a variable as an index of an array of strings, but I want to index the array with the solution of selectedName.


Thank you for considering my message!
Nevena 

guido.tack

unread,
May 12, 2024, 10:15:39 PMMay 12
to MiniZinc
Hi,

Yes, this is possible, as long as you only want to output the result. You can use the fix function inside the output expression like this:

output["Selected name" + Names[ fix(obtain_solution_of_as_int(selectedName)) ]...

Cheers,
Guido

Nevena Gincheva

unread,
May 15, 2024, 7:15:06 PMMay 15
to MiniZinc
Thank you, that worked awesome!
Reply all
Reply to author
Forward
0 new messages