RareEarth --
Just saw this one... I've been spending a lager fraction of my time on the Siemens NX Design Forum lately.
Here's a brute-force answer (like multicaduser described) that gets you the closest value, with expression1 as the input and expression2 as the list. For your example, you would cut and paste this code into expression3:
[pre]
@{
$input << expression1;
$sorted_list << sort( expression2 , Ascending );
$num << length($sorted_list);
$closest << loop
{
For $value from 1 to $num by 1;
if ( $input <= first($sorted_list) ) return first($sorted_list);
if ( $input >= nth($num, $sorted_list) ) return nth($num, $sorted_list);
if
(
(( nth($value, $sorted_list) <= $input ) && ( $input <= nth($value+1, $sorted_list)))
&&
(( $input - nth($value, $sorted_list)) < ( nth($value+1, $sorted_list) - $input ))
) return nth($value, $sorted_list);
if
(
(( nth($value, $sorted_list) <= $input ) && ( $input <= nth($value+1, $sorted_list)))
&&
(( $input - nth($value, $sorted_list)) >= ( nth($value+1, $sorted_list) - $input ))
) return nth($value+1, $sorted_list);
return is 999;
};
};
[/pre]
Many of you will recognize this as Knowledge Fusion syntax -- the language underpinning NX Expressions. Basically, this code starts by:
a) sorting the list (into $sorted_list), and then
b) getting the length of the list (as $num).
It then loops through the values in the list and asks:
for each $value in the $sorted_list
if the $input is smaller than the first item in the list, return the first item.
if the $input is larger than the last item in the list, return the last item.
[and then if neither of the two easy cases are true...]
if the $input is:
(between the current value and the next higher value) AND
(the difference between the $input and the current value is LESS THAN the difference between the $input and the next higher value) THEN
return the current value.
if the $input is:
(between the current value and the next higher value) AND
(the difference between the $input and the current value is GREATER THAN the difference between the $input and the next higher value) THEN
return the next higher value.
And if after all that you don't find any matches at all, return 999.
...Which should be a signal to design the ranges better.
Hopefully that all makes sense. Enjoy!
Taylor Anderson
NX Product Manager, Knowledge Reuse and NX Design
Product Engineering Software
Siemens Product Lifecycle Management Software Inc.
(Phoenix, Arizona)