Jump to content

Search a multi-dimensional array?


bboysza
 Share

Recommended Posts

I can't for the life of me (and out of shear laziness) find a way to search a multi-dim array.

Say I have an array ($avArray[key][value]) -

I want to search the array for a specified VALUE (aka search criteria), and return the KEY.

I guess you can tell I've read an ini section into an array... B)

Any ideas?

Thanks,

Ben

Ben

Link to comment
Share on other sites

The way I will do it is:

For $x = 1 To $avArray[0][0] 
   If $avArray[$x][1] = "Search Value" Then
      $Key = $avArray[$x][0]
      ExitLoop
   EndIf
Next

MsgBox(0, 'Search Key', 'The Key is ==> ' & $Key)

Hope this will help you...

Edited by Danny35d
AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line
Link to comment
Share on other sites

The way I will do it is:

For $x = 1 To $avArray[0][0] 
   If $avArray[$x][1] = "Search Value" Then
      $Key = $avArray[$x][0]
      ExitLoop
   EndIf
Next

MsgBox(0, 'Search Key', 'The Key is ==> ' & $Key)

Hope this will help you...

Thank you - helps quite a bit, but I'm not exactly where I wanted to be.

In this example, I'm searching to see if [$x][1] = "Search Value". Which works, but it's not a true search of the entire array. I.E., only [1] is checked against the search criteria. I could always check each one individually ([$x][1], [$x][2], [$x][3], and etc..) but I imagine somebody can clean this up for us. I'm thinking, but am not terribly talented with multi-dim's.

Ben

Link to comment
Share on other sites

In this example, I'm searching to see if [$x][1] = "Search Value". Which works, but it's not a true search of the entire array. I.E., only [1] is checked against the search criteria. I could always check each one individually ([$x][1], [$x][2], [$x][3], and etc..)

Check AutoIt help for INIREADSECTION

Returns a 2 dimensional array where element[n][0] is the key and element[n][1] is the value.

Meaning [$x][1] will give you search of the entire array. If you try [$x][3] it will fail beacuse this 2 dimensional array only have 2 colums no 3 . This two dimensional array will hold all your data in [?][0] and [?][1].

May be if you post an example of the ini file and the search that you are trying, or replaced

this line

If $avArray[$x][1] = "Search Value" Then

for this one

If StringInStr($avArray[$x][1], "Search Value") Then

This will make it no case sensitive...

Edited by Danny35d
AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line
Link to comment
Share on other sites

Hi,

Is this what you mean?

For $Key = 0 To UBound($avArray)-1

For $Value = 0 To UBound($avArray,2)-1

If $avArray[$Key][$Value] = "X" Then

ExitLoop

EndIf

Next

Next

MsgBox(0, 'Search Key', 'The Index for Key is ==> ' & $Key&@CRLF&'The Index for Value is ==> ' & $Value&@CRLF&'The Cell is ==> [' & $Key-1&']['&$Value&']')

Best, Randall
Link to comment
Share on other sites

Hi,

Is this what you mean?

Best, Randall

Thanks Randall - just the direction I needed...

Ini sample:

[SectionName]
Key1 = Value1
Key2 = Value2

au3:

$avArray = IniReadSection($IniFile, "SectionName")

$Criteria = "Value1"
For $Key = 0 To UBound($avArray)-1
For $Value = 0 To UBound($avArray,2)-1
   If $avArray[$Key][$Value] = $Criteria Then
      msgbox(0, "Match Found", $Criteria &' is assigned to ' &$avArray[$Key][0])
      ExitLoop
   Else 
      msgbox(0, "No Match", 'Sorry, ' & $Criteria & ' was not found to be a value for any keys under queried sectioname')
   ExitLoop
   EndIf
Next
Next

Ben

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...