Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

 View Only
  • 1.  Convert a string to an integer/float

    Posted Thu July 07, 2011 09:28 AM

    Originally posted by: SystemAdmin


    Howdy,

    is there a neat way to convert a string to an integer (or float).
    The main job will be to decide if the string corresponds
    to an integer. There is the parseInt() function that returns
    NaN in case that the string does not represent a valid integer.

    But it is not obvious how this can be checked

    
    execute
    { s = 
    "123" t = 
    "H235" writeln( s ,
    "-->",parseInt(s)); writeln( t ,
    "-->",parseInt(t)); 
    
    if(parseInt(t) == NaN) writeln(
    "NO Int") 
    }
    


    yield to

    
    123-->123 H235-->NaN
    


    which is cossistent with the doc.

    
    The NaN value is contagious, and a numeric operation involving NaN always returns NaN. A comparison operation involving NaN always returns 
    
    false — even the NaN == NaN comparison.
    


    A construction like
    
    
    
    if(!(parseInt(t) < 0 ||  parseInt(t) >= 0)) writeln(
    "NO Int")
    

    works - but it looks weird to me. There must be easier ways to do so.,

    Any ideas or hints?

    Regards
    Norbert
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Convert a string to an integer/float

    Posted Thu July 07, 2011 09:45 AM
    Hi

    what about

    writeln( s ,"-->",Opl.intValue(s));
    writeln( t ,"-->",Opl.intValue(t));
    


    which will give you

    123-->123
    *** ERROR[GENERATE_113] : Not an integer value "H235".
    


    ?
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: Convert a string to an integer/float

    Posted Thu July 07, 2011 09:51 AM

    Originally posted by: SystemAdmin


    Alex,

    the problem with that approach is that it raises an
    exception and execution is stopped.
    I want to keep control in OPL script, e.g.,
    if the string is a parameter, generate a warning and
    switch to some default setting (or alike)

    Regards
    Norbert
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: Convert a string to an integer/float

    Posted Thu July 07, 2011 10:00 AM
    Hi

    Then you can try

    execute{
      s = "123"
      t = "H235"
      
      var defaultValue=-10;
      
      s2=parseInt(s);
      t2=parseInt(t);
      
      if (s2!=s2) s2=defaultValue;
      if (t2!=t2) t2=defaultValue;
      
      writeln( s ,"-->",s2);
      writeln( t ,"-->",t2);
      
      
    }
    


    which gives

    123-->123
    H235-->-10
    


    Alex
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: Convert a string to an integer/float

    Posted Thu July 07, 2011 10:29 AM

    Originally posted by: SystemAdmin


    Alex,

    thanks - the construction
    if (s2!=s2) s2=defaultValue;
    

    looks weird as well, but it seems to be better
    than what I posted.

    Is there a neat way to reject
    s = "12xx3"
    

    as a valid integer? (parseInt() gives 12)

    Regards
    Norbert
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: Convert a string to an integer/float

    Posted Thu July 07, 2011 11:00 AM
    Hi,

    to reject 12xx3 you can write a loop on the string and check that all characters are figures.

    The loop would look a bit like

    execute{
      s = "hello"
      for(var i=0;i<=s.length-1;i++) write(s.charAt(i),"-");
    }
    


    that gives

    h-e-l-l-o-
    


    Regards
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 7.  Re: Convert a string to an integer/float

    Posted Fri July 08, 2011 02:55 AM

    Originally posted by: SystemAdmin


    Alex,

    obviously this can be done by checking char by char.
    But this is far away from being neat.
    What I learned is that there is no compact way to do so.

    Thanks for all the feedback.

    Best regards
    Norbert
    #DecisionOptimization
    #OPLusingCPLEXOptimizer