Hi Alex,
In your example, why do you not issue ar.end() when ar goes out of scope?
In my OPL code, following the examples in CPLEX documentation, I carefully issue cplex.end(), for instance, where previously I have instantiated it via a new such as cplex = new IloCplex() to avoid memory leakage.
When I tried ar.end() at my end, OPL complains that end is an undefined method. Perhaps .end() give one the end of array position like in STL iterators?
Like in C/C++, is there no need to issue ar.end() to clean up the allocated memory? Is there some other method that cleans up the allocated memory?
------------------------------
CPLEX User
------------------------------
Original Message:
Sent: Wed October 13, 2021 03:37 AM
From: ALEX FLEISCHER
Subject: OPL -- Array of floats does not sort correctly
Hi,
I shared a full example at https://github.com/AlexFleischerParis/oplscripting/blob/main/arraymethods.mod
execute{ var ar=new Array(4); ar[0]=2; ar[1]=10; ar[2]=4; ar[3]=1; ar[4]=7; writeln("length = ",ar.length); writeln("join = ",ar.join(" , ")); ar.reverse(); writeln("reverse = ",ar.join(" , ")); ar.sort(); writeln("sort default (string) = ",ar.join(" , ")); function numberorder(a,b){ return ((a==b)?(0):((a<b)?-1:1)); } ar.sort(numberorder); writeln("sort number = ",ar.join(" , "));} /*which giveslength = 5join = 2 , 10 , 4 , 1 , 7reverse = 7 , 1 , 4 , 10 , 2sort default (string) = 1 , 10 , 2 , 4 , 7sort number = 1 , 2 , 4 , 7 , 10*/
Which is part of https://www.linkedin.com/pulse/javascript-within-opl-cplex-alex-fleischer/
------------------------------
[Alex] [Fleischer]
[EMEA CPLEX Optimization Technical Sales]
[IBM]
------------------------------
Original Message:
Sent: Wed October 13, 2021 02:06 AM
From: Vincent Beraudier
Subject: OPL -- Array of floats does not sort correctly
The sort() method sorts the values as strings in alphabetical and ascending order.
Because of this, the sort() method will produce an incorrect result when sorting numbers.
In order to sort numbers, you need to pass a comparison function to sort:
function c(a,b){
if (a == 0) return 0;
if (a < b) return -1;
return 1;
}
yvalues.sort(c);
will produce the correct array.
------------------------------
Vincent Beraudier