# How to compare values of an array?

## Recommended Posts

Hi,

let`s say we have an array like :

`Local \$array[5]=  ["1","2","2","3","1"]`

and I need to display how many times each element occured in the array like :

1 --> 2 times

2 --> 2 times

3 --> 1 time

How can I do this ?

Thank you

##### Share on other sites

Use _ArrayFindAll() function. Check out helpfile for parameters.

When the words fail... music speaks

##### Share on other sites

If you have a large list, you'll find the _Array UDF functions painfully slow.

You could play around with something liek this:

```Local \$array[14]=  [1, 3, 1, 0, 1, "0","2","2","3", 0, "1", "", "Arf!", "Arf!"]

For \$x = 0 to UBound(\$array) - 1 ; count 'em up
\$y = "__" & \$array[\$x]
If IsString(\$array[\$x]) Then \$y &= "_s"
If IsDeclared(\$y) Then
Assign(\$y, Eval(\$y) + 1)
\$array[\$x] = Chr(0)
Else
Assign(\$y, 1)
EndIf
Next

For \$x = 0 to UBound(\$array) - 1 ; display results
If \$array[\$x] == Chr(0) Then ContinueLoop
If IsString(\$array[\$x]) Then
\$suffix = "_s"
\$desc = "str"
Else
\$suffix = ""
\$desc = "num"
EndIf
ConsoleWrite("Value: " & \$array[\$x] & " (" & \$desc & ")    first found in element: " & \$x & "       occurs: " & Eval("__" & \$array[\$x] & \$suffix) & @CRLF)
Next```

You could almost cut that code in half if you didn't care about distinguishing between 1 and "1", or 0 and "0" and "".

• 1

##### Share on other sites

If you have a large list, you'll find the _Array UDF functions painfully slow.

You could play around with something liek this:

```Local \$array[14]= [1, 3, 1, 0, 1, "0","2","2","3", 0, "1", "", "Arf!", "Arf!"]

For \$x = 0 to UBound(\$array) - 1 ; count 'em up
\$y = "__" & \$array[\$x]
If IsString(\$array[\$x]) Then \$y &= "_s"
If IsDeclared(\$y) Then
Assign(\$y, Eval(\$y) + 1)
\$array[\$x] = Chr(0)
Else
Assign(\$y, 1)
EndIf
Next

For \$x = 0 to UBound(\$array) - 1 ; display results
If \$array[\$x] == Chr(0) Then ContinueLoop
If IsString(\$array[\$x]) Then
\$suffix = "_s"
\$desc = "str"
Else
\$suffix = ""
\$desc = "num"
EndIf
ConsoleWrite("Value: " & \$array[\$x] & " (" & \$desc & ") first found in element: " & \$x & "   occurs: " & Eval("__" & \$array[\$x] & \$suffix) & @CRLF)
Next```

You could almost cut that code in half if you didn't care about distinguishing between 1 and "1", or 0 and "0" and "".

Thank you so much, it worked after pasting it.

I will look into the code and try to understand it all in all and hope if I get stuck that you may help me in the future :-)

Thanks again

## Create an account

Register a new account