Sign in to follow this  
Followers 0
nf67

Finding the highest number in a number list of undefined size

7 posts in this topic

Hi there,

this question is really maths related, fyi, but here goes:

If you have a list of numbers where:

  • any number can be deleted
  • numbers are always added at the end of the list
  • if you add a number, it'll be the first unused number, like so:

    before: 1-2-3-4 & after: 1-2-3-4-5

    before: 1-3-4 & after: 1-3-4-2

Is it possible to always (after any operation, either adding or deleting a number) know the highest number in the list?

If so, how?

Thanks in advance Posted Image

P.S: These numbers are all in a ListView component, in one of many columns, so I don't think I can just take all of them and compare, 'cause that would require a lot of StringSplit() - GUICtrlRead() returns the data in one string where | indicates a next column.

P.P.S: I thought of adding a variable which counts the amount of items in a list (+1 when adding, -1 when deleting), and then compare it to the number that's being added. If the number being added is greater than that variable (the amount of items in a list), I know it's getting added as highest number and set the highest number variable to that number. However, when deleting the highest number from the list, I can't revert to a new highest number that way if nothing new gets added Posted Image... Feel free to disregard this thought/idea.

Share this post


Link to post
Share on other sites



This could be a dirty way:

#Include <GuiListView.au3>

Global $MAX = 0

$MAIN = GUICreate("Example")
$LISTVIEW = GUICtrlCreateListView("Number",5,5,390,390)
$CONTEXT_MENU = GUICtrlCreateContextMenu($LISTVIEW)
$ADD = GUICtrlCreateMenuItem("Add",$CONTEXT_MENU)
$DEL = GUICtrlCreateMenuItem("Del",$CONTEXT_MENU)
$SEP = GUICtrlCreateMenuItem("",$CONTEXT_MENU)
$MXI = GUICtrlCreateMenuItem("Max",$CONTEXT_MENU)
_GUICtrlListView_SetColumnWidth($LISTVIEW,0,100)
GUISetState(@SW_SHOW,$MAIN)

While True
    Switch GUIGetMsg()
        Case $ADD
            $INPUT = InputBox("Number","Type your number here")
            If Not @error Then
                If $INPUT > $MAX Then $MAX = $INPUT
                GUICtrlCreateListViewItem($INPUT,$LISTVIEW)
            EndIf
        Case $DEL
            $CTRL = GUICtrlRead($LISTVIEW)
            $NUM = StringTrimRight(GUICtrlRead($CTRL),1)
            GUICtrlDelete($CTRL)
            If $NUM = $MAX Then
                $LM = 0
                For $INDEX = 0 To _GUICtrlListView_GetItemCount($LISTVIEW)-1
                    $CN = _GUICtrlListView_GetItemText($LISTVIEW,$INDEX,0)
                    If $CN > $LM Then $LM = $CN
                Next
                $MAX = $LM
            EndIf
        Case $MXI
            MsgBox(0,"Max number",$MAX)
        Case -3
            Exit
    EndSwitch
    Sleep(10)
WEnd

You don't need a lot of string splits just this line where you need to be careful because here will be char "|", in this case with just one column I used StringTrimRight() but if you need to use in a multi column list view, you can get it with one StringSplit().

$NUM = StringTrimRight(GUICtrlRead($CTRL),1)

All others read will be performed with _GUICtrlListView_GetItemText() on a specific column.

Anyway I think a better way can be using an array where to store values.


When the words fail... music speaks

Share this post


Link to post
Share on other sites

Thank you, Andreik, I didn't know about that function. And I could use StringLeft() to get the numbers as they're in the first column, didn't realise that until you mentioned the StringTrimRight() func. I was also thinking of an array by the way, but I don't have much experience reading from them in autoit, could you suggest some functions I should take a look at in order to retrieve the highest number from an array of all numbers ever added?

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Sort the array descending and the 1st element is the highest one!

You can reflect the array to the ListView - that's easier to manipulate the values in an array.

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Ah and there's also _ArrayMax() apparently. But what do you mean by 'reflect the array to the listview'? Looks like I need the opposite as I need to generate an array from my listview, as an alternative to adding every number to the array when it's put on the list.

Edited by nf67

Share this post


Link to post
Share on other sites

This is an example using an array:

#Include <Array.au3>

Global $STACK[1] = [0]

$MAIN = GUICreate("Example")
$LISTVIEW = GUICtrlCreateListView("Number",5,5,390,390)
$CONTEXT_MENU = GUICtrlCreateContextMenu($LISTVIEW)
$ADD = GUICtrlCreateMenuItem("Add",$CONTEXT_MENU)
$DEL = GUICtrlCreateMenuItem("Del",$CONTEXT_MENU)
$SEP = GUICtrlCreateMenuItem("",$CONTEXT_MENU)
$MAX = GUICtrlCreateMenuItem("Max",$CONTEXT_MENU)
GUISetState(@SW_SHOW,$MAIN)

While True
    Switch GUIGetMsg()
        Case $ADD
            $INPUT = InputBox("Number","Type your number here")
            If Not @error Then
                GUICtrlCreateListViewItem($INPUT,$LISTVIEW)
                _ArrayAdd($STACK,$INPUT)
            EndIf
        Case $DEL
            $CTRL = GUICtrlRead($LISTVIEW)
            $NUM = StringTrimRight(GUICtrlRead($CTRL),1)
            GUICtrlDelete($CTRL)
            $FIND = _ArraySearch($STACK,$NUM)
            If Not @error Then _ArrayDelete($STACK,$FIND)
        Case $MAX
            MsgBox(0,"Max number",_ArrayMax($STACK))
        Case -3
            Exit
    EndSwitch
    Sleep(10)
WEnd

When the words fail... music speaks

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Thanks a bunch for your help Posted Image I'll just go with adding every number to the array then, like you did.

Edited by nf67

Share this post


Link to post
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
Sign in to follow this  
Followers 0