Jump to content

Counting in Multidimensional Arrays


Recommended Posts

I'm working on a script at the moment for practice but i think I've taken a wrong turn,

the intended result is to keep adding names to this array and if two or more names match it'll just increase a count next to their name.

Heres what i have so far:

Global $Names[3] =  ["Harry Potter","Harry Potter","Hermione Granger"]
Global $NameCount[1][2] ; [Index][Name, Count]
$NameCount[0][0] = 0
 
For $a = 0 to UBound($Names)-1 step 1
$Input = $Names[$a]
 
$iSize = UBound($NameCount, 1)
ReDim $NameCount[$iSize + 1][2]
$NameCount[0][0] = $iSize
$NameCount[$iSize][0] = $Input
 
for $i = 1 to $NameCount[0][0]
if $Input = $NameCount[$i][0] Then
$NameCount[$i][1] += 1
;ReDim $NameCount[$iSize+1][2]
EndIf
Next
 
For $i = 1 to $NameCount[0][0]
ConsoleWrite($NameCount[$i][0] & ": " & $NameCount[$i][1] & @CRLF)
Next
ConsoleWrite(@CRLF)
next

Expected Output:

 

Harry Potter: 2

Hermione Granger: 1

 

Current Output:

Harry Potter: 2
Harry Potter: 1
Hermione Granger: 1

 

Edited by Dead_Man
Link to comment
Share on other sites

you were adding it to the array before checking if is there, sorry I like to write in my manner :)

#include <Array.au3>
Global $Names[3] = ["Harry Potter", "Harry Potter", "Hermione Granger"], $found=0
Global $NameCount[1][2] = [[0,0]] ; [Index][Name, Count]

For $a = 0 To UBound($Names) - 1
    $Input = $Names[$a]

    If $a = 0 Then
        ReDim $NameCount[2][2]
        $NameCount[1][0]=$Input
        $NameCount[1][1]=1
    Else
        ;searching for the name
        For $x = 1 To UBound($NameCount)-1
            If $NameCount[$x][0] = $Input Then
                $found=$x
                ExitLoop
            EndIf
        Next
        If $found Then
            $NameCount[$found][1]+=1
        Else
            ReDim $NameCount[UBound($NameCount)+1][2]
            $NameCount[UBound($NameCount)-1][0]=$Input
            $NameCount[UBound($NameCount)-1][1]=1
        EndIf
        $found=0
    EndIf       
Next
_ArrayDisplay($NameCount)
Exit 

You're using $NameCount[0][0] and $NameCount[0][1] for?

Edited by Kyan

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Link to comment
Share on other sites

Its pretty much a score for another project of mine, every time their name is listed add a point, so I'm just making it a tree for how many people are there for the first [0], and the second is their name and how many times they've been listed.

Also this is my first time working with multidimensional arrays so i just wanted to get some practice using them even if there was another way.

Thank you for all your help, i really appreciate it! :)

Edit: NM just got what you meant, bad programming i guess. thanks for pointing that out.

Edited by Dead_Man
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

×
×
  • Create New...