Jump to content

_ArraySearch - Return value on 2d array


Recommended Posts

Hi,

i have a 2d array and i am using _ArraySearch to find the index of an array element. I store the result in a variable.

When i show  the variable, i see just a digit like "1", instead of "[1][3]" or something like that.

Can someone tell me how to get the number of row and column of the searched element?

Thanks in advance

Link to comment
Share on other sites

There may be better ways but if you look at the _ArraySearch function in the help file there is a 2D example.  However, it asks you for a column to search.  If you don't want to do it that way, you could modify that example and return a 2D array of row and column like this:

#include <Array.au3>
#include <MsgBoxConstants.au3>

Local $avArray[6][2] = [ _
        ["String0", "SubString0"], _
        ["String1", "SubString1"], _
        ["String2", "SubString2"], _
        ["String3", "SubString3"], _
        ["String4", "SubString4"], _
        ["String5", "SubString5"]]

_ArrayDisplay($avArray, "$avArray")

For $a=0 to UBound($avArray)-1
    $result=_ArraySearch($avArray, "String3", 0, 0, 0, 1, 1, $a) ; in this case looking for "String3"
    if $result <> 0 then
        Global $2Danswer[2]=[$result,$a] ; create 2d array with global scope to hold answer
        ExitLoop ;stop looping to preserve column number - global scope array populated with answer
    EndIf
Next

_ArrayDisplay($2Danswer)

 

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to comment
Share on other sites

A similar way. Maybe a bit faster because _ArraySearch is used only once
This is for the concept, error checkings to be added  :)

#include <Array.au3>

Local $avArray[6][2] = [ _
        ["String0", "SubString0"], _
        ["String1", "SubString1"], _
        ["String2", "SubString2"], _
        ["String3", "SubString3"], _
        ["String4", "SubString4"], _
        ["String5", "SubString5"]]
_ArrayDisplay($avArray, "$avArray")

$search = "SubString3"
Local $iIndex = _ArraySearch($avArray, $search) 
For $i = 0 to UBound($avArray, 2)-1
   If $avArray[$iIndex][$i] = $search Then $iSubIndex = $i
Next
MsgBox(0, "Found", $search & " was found in the array at position [" & $iIndex & "][" & $iSubIndex & "]")

 

Link to comment
Share on other sites

If you know its always going to be in the towards the last columns, maybe Binary Search with column parameter working backwards?

#include <Array.au3>

Local $avArray[6][2] = [ _
        ["String0", "SubString0"], _
        ["String1", "SubString1"], _
        ["String2", "SubString2"], _
        ["String3", "SubString3"], _
        ["String4", "SubString4"], _
        ["String5", "SubString5"]]

$search = "SubString3"

For $i = ubound($avArray , 2) - 1 to 0 step -1
$iIndex = _ArrayBinarySearch($avArray, $search , 0 , 0, $i)
If $iIndex = -1 Then
    ContinueLoop
Else
    $iIndex = "[" & $i & "]" & "[" & $iIndex & "]"
    exitloop
EndIf

Next

msgbox(0, '' , $iIndex = -1 ?  "Not Found" : $iIndex)

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

WTAF, W.T.A.F.  ?/?

#include <Array.au3>

Local $avArray[6][2] = [ _
        ["String0", "SubString0"], _
        ["String1", "SubString1"], _
        ["String2", "SubString2"], _
        ["String3", "SubString3"], _
        ["String4", "SubString4"], _
        ["String5", "SubString5"]]


$search = "SubString3"
$str = _ArrayToString($avArray , ";" , -1 , -1 , ",")
$arr = stringregexp($str , ".*" & $search , 3)
_ArrayDisplay($arr)
msgbox(0, '' , $arr[0])

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

I'm just going to keep talking to myself, heres one with no loop but it is confined to quite a singular purpose  :)

#include <Array.au3>

Local $avArray[6][2] = [ _
        ["String0", "SubString0"], _
        ["String1", "SubString1"], _
        ["String2", "SubString2"], _
        ["String3", "SubString3"], _
        ["String4", "SubString4"], _
        ["String5", "SubString5"]]


$search = "SubString2"

$str = _ArrayToString($avArray  , " ; " , -1 , -1 , " , ")
$sub = stringleft($str , stringinstr($str , $search) + stringlen($search))
msgbox(0, '' , "[" & stringlen(StringRegExpReplace($sub , "[^,]" , "")) & "]" & "[" & (stringregexp($sub , "\;\s" & $search  , 1) = 0 ? "0" : "1") & "]")

 

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

iamtheky,    fou.gif

#include <Array.au3>

Local $avArray[6][2] = [ _
        ["String0", "SubString0"], _
        ["String1", "SubString1"], _
        ["String2", "SubString2"], _
        ["String3", "SubString3"], _
        ["String4", "SubString4"], _
        ["String5", "SubString5"]]

$search = "SubString3"

StringRegExpReplace(_ArrayToString($avArray , ",," , 0 , 0 , ",,"), ',+(?=.*?,' & $search & '(?=,,|$))', "")
Local $r[3] = [@extended, Floor($r[0]/UBound($avArray, 2)), $r[0]-$r[1]*UBound($avArray, 2)]
Msgbox(0,"", "[" & $r[1] & "][" & $r[2] & "]")

 

Link to comment
Share on other sites

Very nice, you are crazy good at those,

I'd like to see that put to the test against the loops, i think you may have one to dominate the races.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
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
 Share

×
×
  • Create New...