error471 Posted February 1, 2016 Posted February 1, 2016 (edited) Hey folks. I have (another) seemingly simple problem with an array. I want to count the number off all same items and output the result. The result for the uploaded example array would be: 0: 5 1: 1 3: 7 4: 3 5: 5 7: 1 8: 3 Hope the problem is not new and someone knows a solution. Best wishes. error471 Edited February 1, 2016 by error471
Moderators JLogan3o13 Posted February 1, 2016 Moderators Posted February 1, 2016 Something like this, perhaps? #include <Array.au3> Local $aArray[7] = [5, 1, 7, 3, 5, 1, 3] Local $aResult = _ArrayFindAll($aArray, 5) MsgBox($MB_OK, "Number of 5's in Array", UBound($aResult)) "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum!
Moderators Melba23 Posted February 1, 2016 Moderators Posted February 1, 2016 error471, I know you use my GUIListViewEx UDF, so just use that to get the ListView content into an array and then loop through each item, adding one to the relevant count in a separate array as each item is checked: expandcollapse popup#include <GUIConstantsEx.au3> #include <Array.au3> #include "GUIListViewEx.au3" Global $aLV_Array[][] = [["Kent, Clark", 3, 8, 8, 7, 3], _ ["Wayne, Bruce", 3, 1, 3, 3, 3], _ ["Parker, Peter", 0, 3, 4, 5, 0], _ ["Duck, Donald", 0, 5, 4, 4, 0], _ ["Mouse, Mickey", 0, 5, 5, 5, 8]] $hGUI = GUICreate("Test", 500, 500) $cLV = GUICtrlCreateListView("Col 0 |Col 1|Col 2|Col 3|Col 4|Col 5", 10, 10, 480, 300, $LVS_SINGLESEL) _GUICtrlListView_SetExtendedListViewStyle($cLV, $LVS_EX_FULLROWSELECT) For $i = 0 To 4 $sData = "" For $j = 0 To 5 $sData &= $aLV_Array[$i][$j] & "|" Next GUICtrlCreateListViewItem(StringTrimRight($sData, 1), $cLV) Next $cGo = GUICtrlCreateButton("Go", 10, 450, 80, 30) GUISetState() $iLVIndex = _GUIListViewEx_Init($cLV, $aLV_Array) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $cGo $aRet = _GUIListViewEx_ReturnArray($iLVIndex) $aCount = _CountItems($aRet) _ArrayDisplay($aCount, "", Default, 8) EndSwitch WEnd Func _CountItems($aArray) Local $aRet[10] For $i = 0 To UBound($aArray) - 1 For $j = 1 To UBound($aArray, 2) - 1 $iValue = $aArray[$i][$j] $aRet[$iValue] += 1 Next Next Return $aRet EndFunc M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
error471 Posted February 1, 2016 Author Posted February 1, 2016 Thanks to both of you. I will test your scripts when I am home later.
czardas Posted February 1, 2016 Posted February 1, 2016 (edited) Another (simple) method: expandcollapse popup#include <Array.au3> Local $aArray = [['Kent, Clark',3,8,8,7,3], _ ['Wayne, Bruce',3,1,3,3,3], _ ['Parker, Peter',0,3,4,5,0], _ ['Duck, Donald',0,5,4,4,0], _ ['Mouse, Mickey',0,5,5,5,8]] _ArrayDisplay($aArray, 'Original Array') Local $iRows = UBound($aArray) Local $iCols = UBound($aArray, 2) Local $aData[$iRows * ($iCols -1)] Local $iCount = 0 For $i = 0 To $iRows -1 For $j = 1 To $iCols -1 $aData[$iCount] = $aArray[$i][$j] $iCount += 1 Next Next $aData = _ArrayUnique($aData) ; _ArrayDisplay($aData) Local $aOccurrence[$aData[0]][2] For $i = 1 To $aData[0] $aOccurrence[$i -1][0] = $aData[$i] $aOccurrence[$i -1][1] = 0 For $j = 0 To $iRows -1 For $k = 1 To $iCols -1 If $aArray[$j][$k] = $aOccurrence[$i -1][0] Then $aOccurrence[$i -1][1] += 1 Next Next Next _ArrayDisplay($aOccurrence, 'Occurrence') Edited February 1, 2016 by czardas operator64 ArrayWorkshop
error471 Posted February 1, 2016 Author Posted February 1, 2016 I tried all methods. They all work very well. Thank you.
czardas Posted February 1, 2016 Posted February 1, 2016 Notice that my method ignores Col 0. It was also written in a bit of hurry, so you might want to make modifications to it: for the exercise if not for any other reason. operator64 ArrayWorkshop
error471 Posted February 1, 2016 Author Posted February 1, 2016 18 minutes ago, czardas said: Notice that my method ignores Col 0. It was also written in a bit of hurry, so you might want to make modifications to it: for the exercise if not for any other reason. Ignoring 0 was exactly what I needed.
Moderators Melba23 Posted February 1, 2016 Moderators Posted February 1, 2016 error471, Just for info, so does my code - as I knew that is what you wanted! M23 error471 1 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
czardas Posted February 1, 2016 Posted February 1, 2016 It's an extremely common occurrence, although I consider using the first row, or column, as headers to be specific usage as opposed to general usage. I am writing some functions at the moment that would swallow the code I wrote above in a single mouthful. I hope to have them finished soon. operator64 ArrayWorkshop
Malkey Posted February 1, 2016 Posted February 1, 2016 And another method. #include <Array.au3> ; Modified from ; https://www.autoitscript.com/forum/topic/180314-how-to-detect-x2-common-numbers-in-a-list/?do=findComment&comment=1294363 Local $ret Global $aLV_Array[][] = [["Kent, Clark", 3, 8, 8, 7, 3], _ ["Wayne, Bruce", 3, 1, 3, 3, 3], _ ["Parker, Peter", 0, 3, 4, 5, 0], _ ["Duck, Donald", 0, 5, 4, 4, 0], _ ["Mouse, Mickey", 0, 5, 5, 5, 8]] Local $txt = _ArrayToString($aLV_Array, @LF) Local $uniq = StringRegExp($txt, "(?s)\b(\d+)\b(?!.*\b\1\b)", 3) ; Unique digits only in this array. _ArraySort($uniq) ;_ArrayDisplay($uniq) For $i = 0 To UBound($uniq) - 1 StringRegExpReplace($txt, "\b" & $uniq[$i] & "\b", "") $ret &= $uniq[$i] & ":" & @extended & @LF Next ConsoleWrite(StringTrimRight($ret, 1) & @LF) ; Remove trailing @LF #cs ; Returns:- 0:5 1:1 3:7 4:3 5:5 7:1 8:3 #ce
error471 Posted February 3, 2016 Author Posted February 3, 2016 Things get complicated. The values are no numbers anymore, but decimals. The correct solution would be: 0,5: 1 1: 2 1,5: 4 2: 3 2,5: 2 3,5: 3 4: 1 5: 1 5,5:1 9: 1 9,1: 3 9,3: 1 11: 2 I hope this is a common problem and someone already has a solution.
kylomas Posted February 3, 2016 Posted February 3, 2016 Error471, Did you try any of the previous solutions on your new data? Kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
error471 Posted February 3, 2016 Author Posted February 3, 2016 Indeed, czardas' and JLogan3o13s examples do the job. Excuse me.
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