Sign in to follow this  
Followers 0
momo1984

How to compare values of an array?

4 posts in this topic

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 this post


Link to post
Share on other sites



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


When the words fail... music speaks

Share this post


Link to post
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 person likes this

Share this post


Link to post
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

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
Sign in to follow this  
Followers 0