Sign in to follow this  
Followers 0
Knight

Trim Down Array and return Ubound Problems

10 posts in this topic

#1 ·  Posted (edited)

Alright I made a function that will search an array over and over again for "None" and then delete it and when there is no more "None"'s it will return UBound of that array.

#include <Array.au3>
Func _TrimDownArray($ArrayToUse)
    Do
        $Found = _ArraySearch($ArrayToUse, "None")
        MsgBox(0, "", $Found)
        If Not $Found = " " Then _ArrayDelete($ArrayToUse, $Found)
    Until $Found = ""
    return UBound($ArrayToUse, 1)
EndFunc

The problem I have is, is that if the array only contains "None" then it UBound returns as many elements in the array as there is.

Use this for example.. It returns 3 as it should..

#include <Array.au3>

$Array = _ArrayCreate("Fish", "Goose", "Chicken", "None", "None")
$Trim2UBound = _TrimDownArray($Array)
MsgBox(4100, "Array", $Trim2UBound)
Exit

Func _TrimDownArray($ArrayToUse)
    Do
        $Found = _ArraySearch($ArrayToUse, "None")
        If Not $Found = "" Then _ArrayDelete($ArrayToUse, $Found)
    Until $Found = ""
    return UBound($ArrayToUse, 1)
EndFunc

now, if I replace all the animals with "None", then it will return 5 rather then 0.

#include <Array.au3>

$Array = _ArrayCreate("None", "None", "None", "None", "None")
$Trim2UBound = _TrimDownArray($Array)
MsgBox(4100, "Array", $Trim2UBound)
Exit

Func _TrimDownArray($ArrayToUse)
    Do
        $Found = _ArraySearch($ArrayToUse, "None")
        If Not $Found = "" Then _ArrayDelete($ArrayToUse, $Found)
    Until $Found = ""
    return UBound($ArrayToUse, 1)
EndFunc

Anyone have any ideas to solve this? I have tried numerous things to no avail.

Thanks,

JKnight

Edited by Knight

Share this post


Link to post
Share on other sites



Alright I have done alot of testing and have yet to solve the problem, but I realize why it doesn't work. You can't delete every element of the array, so I have to find a way to be able to do that.

Share this post


Link to post
Share on other sites

After spending hours of trying to do this, I resorted to a different approach.

Any help doing it the old way would be GREATLY APPRECIATED!

Func _FuckThisShit($ArrayToUse)
    Local $TotalElements = UBound($ArrayToUse, 1)
    Local $Element = 0
    Local $UseElements = 0
    While 1
        If Not $ArrayToUse[$Element] = "" Then $UseElements += 1
        $Element += 1
        If $Element = $TotalElements Then ExitLoop
    WEnd
    return $UseElements
EndFunc

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

#include <Array.au3>

$Array = _ArrayCreate("None", "None", "None", "None", "None")
$Trim2UBound = _TrimDownArray($Array)
MsgBox(4100, "Array", $Trim2UBound)

Func _TrimDownArray($ArrayToUse)
   For $I = 0 To UBound($ArrayToUse) - 1
      If $ArrayToUse[$I] <> "None" Then
         Do
           $Found = _ArraySearch($ArrayToUse, "None")
           If Not $Found = "" Then _ArrayDelete($ArrayToUse, $Found)
         Until $Found = ""
         Return UBound($ArrayToUse)
      EndIf
   Next
EndFunc

;)

Edited by Burrup

[u]Old Projects:[/u]A3MORGB2Hex[u]Old Functions:[/u]_TimeAdd/_TimeSub_AddComma_BubbleSort _RippleSort "He who does not understand your silence will probably not understand your words." - Elbert Hubbard.

Share this post


Link to post
Share on other sites

Here's what I came up with:

#include <Array.au3>
  
$Array = _ArrayCreate("Fish", "Goose", "Chicken", "None", "None")
  $Array = _ArrayCreate("None", "None", "None", "None", "None")
  $Trim2UBound = _TrimDownArray($Array)
  MsgBox(4100, "Array", $Trim2UBound)
  Exit
  
  Func _TrimDownArray($ArrayToUse)
      Local $StringArray
      $StringArray = _ArrayToString($ArrayToUse, "|") & "|"
      $StringArray = StringReplace($StringArray, "None|", "")
      $StringArray = StringLeft($StringArray, StringLen($StringArray) - 1)
      $ArrayToUse = StringSplit($StringArray, "|")
      _ArrayDelete($ArrayToUse, 0)
      If $StringArray = "" Then
          Return 0
      Else
          Return UBound($ArrayToUse, 1)
      EndIf
  EndFunc  ;==>_TrimDownArray
Note that $Array will not be altered unless it's passed by Reference.

Func _TrimDownArray(ByRef $ArrayToUse)


[font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

@Burrup

Thanks! It works perfect with one exception..

$Array = _ArrayCreate("None", "Fish", "Goose", "Mongoose", "None")

The above will return 5 with your function.

I don't plan on having that exception happen because the array should never appear like that anyway.

Thanks again,

@Skruge

Oops just noticed yours, Ill test it in a sec..

-JKnight

Edited by Knight

Share this post


Link to post
Share on other sites

#include <Array.au3>

$Array = _ArrayCreate("Fish", "Goose", "Chicken", "None", "None")
$Trim2UBound = _TrimDownArray($Array)
MsgBox(4100, "Array", $Trim2UBound)

Func _TrimDownArray($ArrayToUse)
   Local $Temp
   For $I = 0 To UBound($ArrayToUse) - 1
      If $ArrayToUse[$I] = "None" Then
            $Temp = $Temp & $I & ","
      EndIf
   Next
   $Split = StringSplit(StringTrimRight ($Temp, 1), ",")
   If $Split[0] = UBound($ArrayToUse) Then 
      $ArrayToUse = 0
   Else
      For $I = 1 To $Split[0]
         _ArrayDelete($ArrayToUse, $Split[$I])
      Next
   EndIf
   Return UBound($ArrayToUse)
EndFunc

Fixed ;). When all the index's don't equal "None" it returns 4 because that is the UBound. When they all equal "None" it sets the array to 0 thus deleting it.


[u]Old Projects:[/u]A3MORGB2Hex[u]Old Functions:[/u]_TimeAdd/_TimeSub_AddComma_BubbleSort _RippleSort "He who does not understand your silence will probably not understand your words." - Elbert Hubbard.

Share this post


Link to post
Share on other sites

Perfect Burrup!

Thanks man..

-JKnight

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