Sign in to follow this  
Followers 0
programit

Basic Array Management Issues

6 posts in this topic

I am trying to keep an array up to date containing 2 fields: Item, Quantity.

I originally tried to build an array like:

$PackedItems[x] = Item

$PackedItems[x][0] = Quatnity

But the _ArraySearch & _ArrayAdd didn't seem to work with the multidimensional array.

So I moved on to trying this method:

$PackedItems[x] = Item & "^" & Quantity

So basically I would search to see if the item exists in the array. If not just _ArrayAdd it. And if it did exist, set quantity = quantity + amount added. And put it back in the array.

BUT the problem I'm running into with this method is that if I do an _ArraySearch on the array for "Basketball" and the string in the array is "Basketball^12" it wont find it. So, does anyone have a solution for this scenario on how to manage an array with Items & Quantity on the fly.

Thanks,

Programit.


What I'm using AutoIt for: I was hired as QA to write automated tests for our software. So I macro any possible situation/process a user could get themselves into, and then check to see that the outcome is what I expect.

Share this post


Link to post
Share on other sites



Did I not explain this well? It seems like a VERY basic operation that I'm trying to perform.


What I'm using AutoIt for: I was hired as QA to write automated tests for our software. So I macro any possible situation/process a user could get themselves into, and then check to see that the outcome is what I expect.

Share this post


Link to post
Share on other sites

Just create 2 arrays: one for the Item and one for the Quantity.

Search the Item array and when found use the returned index for adding the value to the Quantity array.

When not found add the Item and Quantity to the end of both arrays.

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

If you insist on using the 2d array I guess you could do something like this:

#include <Array.au3>

; $PackedItems[Item][Quantity]
Global $PackedItems[3][2]

$PackedItems[0][0] = "Basketball^12"
$PackedItems[0][1] = 12
$PackedItems[1][0] = "Baseball&@##!"
$PackedItems[1][1] = 22
$PackedItems[2][0] = "Football^12112"
$PackedItems[2][1] = 43

While 1
    $sItemName = InputBox("Enter Item", "Enter an item name to search for!")
; Do a partial search on PackedItems array
    $iSearchIndex = _ArraySearch($PackedItems, $sItemName, 0, UBound($PackedItems), 0, 1)
    If $iSearchIndex = -1 Then
        MsgBox(0, "Not Found", $sItemName & " was not found in $PackedItems.  Adding it.")
        $iItemQuantity = InputBox("Enter Quantity", "Enter an item quantity")
        ReDim $PackedItems[UBound($PackedItems) + 1][2]
        $PackedItems[UBound($PackedItems) - 1][0] = $sItemName
        $PackedItems[UBound($PackedItems) - 1][1] = $iItemQuantity
        MsgBox(0, "Item added", "Item: " & $sItemName & " Qty: " & $iItemQuantity & " has been added!")
    Else
        MsgBox(0, "Found!", $sItemName & " was found in $PackedItems at [" & $iSearchIndex & "][0]"&@CRLF&"Item Name:"&$PackedItems[$iSearchIndex][0]&@CRLF&"Quantity:" &$PackedItems[$iSearchIndex][1])
    EndIf
WEnd

Try searching for "Basketball", you'll get a hit on "Basketball^12". If it doesn't get a hit on what you search it will ask you for a quantity, redim the array and add it.

It should be noted if you have a large number of elements redimm'ing the array is a computationally expensive process so it could be slow. Similarly, your searching might be slow and you could speed it up considerably using a Binary Search if you sorted your data.

As far as your search not returning "Basketball^12" when you search for "Basketball", you need to use a Partial search.

Edited by Simucal

AutoIt Scripts:Aimbot: Proof of Concept - PixelSearching Aimbot with several search/autoshoot/lock-on techniques.Sliding Toolbar - Add a nice Sliding Toolbar to your next script. Click the link to see an animation of it in action!FontInfo UDF - Get list of system fonts, or search to see if a particular font is installed.Get Extended Property UDF - Retrieve a files extended properties (e.g., video/image dimensions, file version, bitrate of song/video, etc)

Share this post


Link to post
Share on other sites

Ok, it looks like the partial is just what I needed.

Here is my code to accomplish what I needed:

Local $avArray[6]

$avArray[0] = "JPM^10"
$avArray[1] = "Holger^10"
$avArray[2] = "Jon^10"
$avArray[3] = "Larry^10"
$avArray[4] = "Jeremy^10"
$avArray[5] = "Valik^10"
;~ $avArray[6] = "Cyberslug"
;~ $avArray[7] = "Nutster"
;~ $avArray[8] = "JdeB"
;~ $avArray[9] = "Tylo"
$name = "Valik"
$quantity = "50"
_ArrayDisplay($avArray, "$avArray BEFORE _ArrayAdd()")
$index = _ArraySearch($avArray, $name,"","","",1);Search through array for name
If $index == -1 Then
    MsgBox(1, "", "Item does not exist. Adding it.")
    _ArrayAdd($avArray, $name & "^" & $quantity)
Else
    MsgBox(1, "", "Item exists, adding quantity.")
    $string = $avArray[$index]
    $Array = StringSplit($string, "^")
    $name = $Array[1]
    $old_quantity = $Array[2]
    MsgBox(1, "", "Name = '" & $name & " and old quantity = '" & $old_quantity & "'")
    $old_quantity = $old_quantity + $quantity
    $avArray[$index] = $name & "^" & $old_quantity
EndIf
_ArrayDisplay($avArray, "$avArray AFTER _ArrayAdd()")

I greatly appreciate the help Guys/Gals.

Thanks,

Programit


What I'm using AutoIt for: I was hired as QA to write automated tests for our software. So I macro any possible situation/process a user could get themselves into, and then check to see that the outcome is what I expect.

Share this post


Link to post
Share on other sites

This is the method I use for sequentially adding info to a 2 dimensional array.

#include <Array.au3>


Global $Array[200][2] = [["0",""]]

_UpdateArrayData("BlahBlahBlah","1337")
_UpdateArrayData("BlaaaaHHHH","13")
_UpdateArrayData("BlahBlah","37")

_ArrayDisplay($Array)

Func _UpdateArrayData($Value1,$Value2)
    Redim $Array[$Array[0][0]+2][2]
    $Array[0][0] = $Array[0][0]+1
    $Array[$Array[0][0]][0] = $Value1 
    $Array[$Array[0][0]][1] = $Value2       
EndFunc

There is always a butthead in the crowd, no matter how hard one tries to keep them out.......Volly

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