error471

Count number of same items within an array.

15 posts in this topic

#1 ·  Posted (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

example.png

Edited by error471

Share this post


Link to post
Share on other sites



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

 


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

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:

#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._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Thanks to both of you. I will test your scripts when I am home later.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Another (simple) method:
 

#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 by czardas

Share this post


Link to post
Share on other sites

I tried all methods. They all work very well. Thank you.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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. ;)

Share this post


Link to post
Share on other sites

error471,

Just for info, so does my code - as I knew that is what you wanted!

M23

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

That's why I implented your code. ;)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites

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.

example.png.cb9f256dd05e6e55c7f21eb1499081c1.png

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Indeed, czardas' and JLogan3o13s examples do the job. Excuse me.

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