Jump to content
Sign in to follow this  
huldu

Array compared to another array

Recommended Posts

huldu

For $x = 1 To UBound($as_monstertype)
    $monster = $as_monstertype[$x]
    ToolTip("Looking for : " & $monster & " at line " & $a & " Overall progress: " & $x,0,0)
        For $a = 1 To UBound($as_monstertype)
            If $monster = $as_monstertype[$a] Then
                $as_monstertype[$a] = ""
                ToolTip("Looking for : " & $monster & " at line " & $a & " Overall progress: " & $x,0,0)
            EndIf
            ToolTip("Looking for : " & $monster & " at line " & $a & " Overall progress: " & $x,0,0)
        Next
Next

This is the problem line "If $monster = $as_monstertype[$a] Then"

I get an error running this, : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

What im trying to do is look if a type of "monster" (in this case) exists more then once in the array, if it was found then it will be removed. In the end you only have 1 type of monster left in the array. Its pretty rough atm i know :o. I just wonder why i get that error when i got thru the For loop just before i got to $a?

Edited by huldu

"I'm paper, rock is fine, nerf scissors!!!"

Share this post


Link to post
Share on other sites
Sokko

UBound returns the size of the array, the number of the array's last element is one less than that. If $as_monstertype contained 10 elements (0 through 9), UBound would return 10, and the final iteration of the loop would attempt to access $as_monstertype[10] which is a nonexistant location.

I assume you're not storing anything important at $as_monstertype[0] since your loop doesn't access it at all. If you are, you'd want to change that part of the code as well.

Edited by Sokko

Share this post


Link to post
Share on other sites
huldu

Oh... i see what you mean!

Think im gonna skip this part, its really really slow if the array is pretty big :|

Hm maybe would be alot faster if i saved all the "monsters" to a file instead, then checked thru that and removed as duplicates were found.

Edited by huldu

"I'm paper, rock is fine, nerf scissors!!!"

Share this post


Link to post
Share on other sites
seandisanti

Oh... i see what you mean!

Think im gonna skip this part, its really really slow if the array is pretty big :|

Hm maybe would be alot faster if i saved all the "monsters" to a file instead, then checked thru that and removed as duplicates were found.

the fastest way is probably to use ArrayToString to turn the monster array into a string, then use StrInStr() to determine if your monster is listed in that string...

Share this post


Link to post
Share on other sites
Stumpii

This should do what you want. I would not write it out to file as it will be quicker in memory. It does not return an array, but creates a new one instead.

This would be a nice little UDF that someone else could finish. :o

#Include<Array.au3>

Dim $SourceArray[10]
Dim $ResultArray

$SourceArray[0] = "a"
$SourceArray[1] = "b"
$SourceArray[2] = "a"
$SourceArray[3] = "c"
$SourceArray[4] = "a"
$SourceArray[5] = "a"
$SourceArray[6] = "c"
$SourceArray[7] = "a"
$SourceArray[8] = "b"
$SourceArray[9] = "a"

; Go through each source index
For $iSourceIndex = 0 To UBound($SourceArray) - 1
;See if it exists in the result Array
    If IsArray($ResultArray) Then
        If _ArraySearch($ResultArray, $SourceArray[$iSourceIndex]) = -1 Then
; Does not exist, so add it
            _ArrayAdd ( $ResultArray, $SourceArray[$iSourceIndex])
        EndIf
    Else
;Not an array, so create it
        $ResultArray = _ArrayCreate($SourceArray[$iSourceIndex])
    EndIf
Next

_ArrayDisplay ( $ResultArray, "Here are all the entries" )
Edited by Stumpii

“Give a man a script; you have helped him for today. Teach a man to script; and you will not have to hear him whine for help.”AutoIt4UE - Custom AutoIt toolbar and wordfile for UltraEdit/UEStudio users.AutoIt Graphical Debugger - A graphical debugger for AutoIt.SimMetrics COM Wrapper - Calculate string similarity.

Share this post


Link to post
Share on other sites
huldu

Im very impressed!

Your search didnt take more then a second, if that. I had 144 entries.

My array search (up there) took almost 5 minutes. In the end the array was empty too lol (forgot to add the monsters to an array so they didnt delete themself:) )

Edited by huldu

"I'm paper, rock is fine, nerf scissors!!!"

Share this post


Link to post
Share on other sites
huldu

I just read about _ArrayCreate, and it only supports upto 21 elements?. Thats gonna be very bad :o


"I'm paper, rock is fine, nerf scissors!!!"

Share this post


Link to post
Share on other sites
Stumpii

I just read about _ArrayCreate, and it only supports upto 21 elements?. Thats gonna be very bad :o

No. It is only used to create the very first entry. After that, the add function keeps adding, so there will be no limit.


“Give a man a script; you have helped him for today. Teach a man to script; and you will not have to hear him whine for help.”AutoIt4UE - Custom AutoIt toolbar and wordfile for UltraEdit/UEStudio users.AutoIt Graphical Debugger - A graphical debugger for AutoIt.SimMetrics COM Wrapper - Calculate string similarity.

Share this post


Link to post
Share on other sites
huldu

No. It is only used to create the very first entry. After that, the add function keeps adding, so there will be no limit.

Cool, got a bit worried for a second :o


"I'm paper, rock is fine, nerf scissors!!!"

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.