Jump to content

_RoundList()


james3mg
 Share

Recommended Posts

OK, a nice 'n easy one I made for a project I thought I'd throw out there:

Func _RoundList($_rl_num,$_rl_list)
    $_rl_list=StringSplit($_rl_list,"|,");split the list of possibilities into an array
    If $_rl_list[0]=1 Then Return $_rl_list[1];if there's only one possibility in the list, return the list (single value)
    Local $_rl_lowestIndex=1;this var will determine which possibility will be returned
    For $_rl_i=1 To $_rl_list[0];start looping through each possibility in the list
        If Abs($_rl_num-$_rl_list[$_rl_i])<Abs($_rl_num-$_rl_list[$_rl_lowestIndex]) Then $_rl_lowestIndex=$_rl_i;if the next possibility is closer than the current closest, store the index of the next possiblity
    Next
    Return $_rl_list[$_rl_lowestIndex];return the list value at the index determined to be the closest
EndFunc
True to my usual form, I don't tend to do much error checking, because other than throwing in some non-numerals in the list, I can't think of a way to make this error! Oh well, at least I'm trying to work on my commenting (actually doing it)

Call this function like so:

_RoundList(5.1212,"1,2,4,8,16")

and it will return the value from your list that's closest to the first parameter. You can also separate your list elements with a pipe ( | ) character, or a mix of pipes and commas. Giving it a string with non-numerals will just treat it like AutoIt always does - either truncate at the character or convert it to be equal to 0. However, the return value will not be converted - it will be given back to you as you provided it. That is, if you run:

_RoundList(1,"5,a,-2)

it will return "a" since a is considered to be 0.

Anyway, I don't think there's a huge demand for this (I've only had use for it a few times), so I thought I'd throw it out, and hopefully it helps someone someday!

Later :rolleyes:

Edit: I posted a slightly older (and longer) version at first...my mistake.

Edited by james3mg
"There are 10 types of people in this world - those who can read binary, and those who can't.""We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true." ~Robert Wilensky0101101 1001010 1100001 1101101 1100101 1110011 0110011 1001101 10001110000101 0000111 0001000 0001110 0001101 0010010 1010110 0100001 1101110
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...