Topic
3 replies Latest Post - ‏2013-12-20T10:28:43Z by AlexFleischer
davidoff
davidoff
55 Posts
ACCEPTED ANSWER

Pinned topic OPL Script : how to check that an element is contained in a set ?

‏2013-12-20T09:31:34Z |
{int} truc = {1,2,3};
int a= 4;
int inset = a in truc;

 

How to write such code in scripting ?

  • AlexFleischer
    AlexFleischer
    983 Posts
    ACCEPTED ANSWER

    Re: OPL Script : how to check that an element is contained in a set ?

    ‏2013-12-20T09:37:34Z  in response to davidoff

    Hi,

    {int} truc = {1,2,3};
    int a= 4;
    int b=3;
    int ainset = a in truc;
    int binset = b in truc;

    execute
    {
     writeln(ainset);
     var ainset2=truc.contains(a);
     writeln(ainset2);
     
     writeln(binset);
     var binset2=truc.contains(b);
     writeln(binset2);
    }

     

    gives

    0
    false
    1
    true

     

    regards

     

  • davidoff
    davidoff
    55 Posts
    ACCEPTED ANSWER

    Re: OPL Script : how to check that an element is contained in a set ?

    ‏2013-12-20T09:44:41Z  in response to davidoff

    Thanks.

    Now, if I work with this example

     tuple Truc{
       int first;
       int second;
     }   
     {Truc} trucs = { <t,t+1> | t in truc};
     Truc atruc = <1,3>;
     execute{
       var inset3 = trucs.contains(atruc);
       writeln("inset3=",inset3);
     }
    

    It works fine too.

    Now, I'm wondering how I could build the tuple atruc within the script

    • AlexFleischer
      AlexFleischer
      983 Posts
      ACCEPTED ANSWER

      Re: OPL Script : how to check that an element is contained in a set ?

      ‏2013-12-20T10:28:43Z  in response to davidoff

      tuple Truc{
         int first;
         int second;
       }   
       
       Truc atruc;
       
       execute
       {
        atruc.first=1;
        atruc.second=2;
        writeln(atruc);
       }