Jump to content

How to select smallest value from a string list


valkk
 Share

Recommended Posts

Ok I have a relatively simple problem yet the solution escapes me :ph34r:

I'll spare ya the details of my script, just get down to the problem. I need to be able to determine which number is smallest in a list, and select the point that number refers to. The list is in a format like this,

point#1,distance,point#2,distance, etc.

The list's lenght changes, but it's composed of pair values (point#,distance)

so an example would be,

1,80,2,100,3,60,4,150

The smallest distance is 60 from the above example, so therefore the script should return the point number 3. I have no idea how to make the script determine which of the distance numbers given in that string is the smallest and return the point number it refers to. Unfortunately in my case, this string format is the only option.

Anyone can help me out with this?

Edited by valkk
Link to comment
Share on other sites

Because your list is structured, the simplest way is using StringSplit and then building new array with every second value from splitteed array. Something like:

Dim $list = "1,80,2,100,3,60,4,150", $cnt

$aList = StringSplit($list, ",")
Dim $aDistList[UBound($aList)/2]

For $cnt = 0 to UBound($aList)/2-1
    $aDistList[$cnt] = $aList[($cnt+1)*2]
Next

Dim $min = $aDistList[0], $num = 1

For $cnt = 1 to UBound($aDistList)-1
    If Number($aDistList[$cnt]) < Number($min) Then 
        $min = $aDistList[$cnt]
        $num = $cnt + 1
    Endif
Next

MsgBox (0, "title", "Min distance (" & $min & ") is point " & $num)

But it will work if you points are ordered (1,2,3... etc). If they numbers are mixed you need a bit more complex:

$list = "1,80,2,100,3,60,4,150"

$aList = StringSplit($list, ",")
Dim $aDistList[UBound($aList)/2][2]

For $cnt = 0 to UBound($aList)/2-1
    $aDistList[$cnt][0] = $aList[($cnt+1)*2-1]
    $aDistList[$cnt][1] = $aList[($cnt+1)*2]
Next

Dim $min = $aDistList[0][1], $num = $aDistList[0][0]

For $cnt = 1 to UBound($aDistList)-1
    If Number($aDistList[$cnt][1]) < Number($min) Then 
        $num = $aDistList[$cnt][0]
        $min = $aDistList[$cnt][1]
    Endif
Next

MsgBox (0, "title", "Min distance (" & $min & ") is point " & $num)
Edited by Kot
Link to comment
Share on other sites

$anValues = StringSplit ($sString, ",")
;Prep Min markers
    $nMinDistance = 99999
    $nMinPoint = 0
    for $nX = 2 to $anValues[0] step 2
        if $anValue[$nX] < $nMinDistance then
            ;Found a new Min
            $nMinDistance = $anValue[$nx]
            $nMinPoint = $anValue[$nx-1]
        end if
    next
;return results
    $sMsg = "Min Distance = " & $nMinDistance & @LF & "Min Point = " & $nMinPoint
    Msgbox (0,"Min", $sMsg )

Link to comment
Share on other sites

Thank you for support :(

I thougt I'll try mix your two examples ^^ and it works but during one test I got some strange result. At one point I have this statement, If 80 < 130 Then, which is true 80 is smaller then 130 BUT it skips it. Here's my example,

$listchoices = "17,130,22,80";

$listnum = stringsplit($listchoices,",");

$Smallest = 10000;
For $count = 1 to ($listnum[0]/2);
  If $listnum[$count*2] < $Smallest Then
    $Smallest = $listnum[$count*2]
    $point = $listnum[($count*2)-1];
  EndIf
Next
msgbox(0, "test", "Choosen:"&$point);

For that particular list of values, it chooses #17 which refers to 130. This is wrong, 80 is smaller then 130. After playing around I learned that IF statement was getting skipped, leading me to conclude that it thinks 80 is NOT smaller then 130. I put a msgbox returning the exact values of the IF statement, $listnum[$count*2] < $Smallest, and it returned If 80 < 130. Umm doesn't take a genious to realize that's true, yet the script thinks it's false. :ph34r:

Here's the funny thing. If I replace $listnum[$count*2] with the number 80, as it should be, it works; the statement is true. What's weird is $listnum[$count*2] is equal to 80 on the 2nd loop.

Edited by valkk
Link to comment
Share on other sites

  • Developers

your script does a string compare in stead of a number compare..

this will do it:

$listchoices = "17,130,22,80";

$listnum = stringsplit($listchoices,",");

$Smallest = 10000;
For $count = 1 to ($listnum[0]/2);
  If number($listnum[$count*2]) < number($Smallest) Then
    $Smallest = $listnum[$count*2]
    $point = $listnum[($count*2)-1];
  EndIf
Next
msgbox(0, "test", "Choosen:"&$point);

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Ohhhhhhhhh  :ph34r:.  That I didn't know, guess I thought it would consider it as a number automatically  :(.

<{POST_SNAPBACK}>

I wish

Returns the numeric representation of an expression.

Number ( expression )

Became my favorite section of the help file while writing a complicated generatesalesreport script

Rick

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...