nf67 Posted June 11, 2011 Share Posted June 11, 2011 Hi there,this question is really maths related, fyi, but here goes:If you have a list of numbers where:any number can be deletednumbers are always added at the end of the listif 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-2Is 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 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 ... Feel free to disregard this thought/idea. Link to comment Share on other sites More sharing options...
Andreik Posted June 11, 2011 Share Posted June 11, 2011 This could be a dirty way: expandcollapse popup#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. Link to comment Share on other sites More sharing options...
nf67 Posted June 11, 2011 Author Share Posted June 11, 2011 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? Link to comment Share on other sites More sharing options...
UEZ Posted June 11, 2011 Share Posted June 11, 2011 (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 June 11, 2011 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!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
nf67 Posted June 11, 2011 Author Share Posted June 11, 2011 (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 June 11, 2011 by nf67 Link to comment Share on other sites More sharing options...
Andreik Posted June 11, 2011 Share Posted June 11, 2011 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. Link to comment Share on other sites More sharing options...
nf67 Posted June 12, 2011 Author Share Posted June 12, 2011 (edited) Thanks a bunch for your help I'll just go with adding every number to the array then, like you did. Edited June 12, 2011 by nf67 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now