Jump to content
Sign in to follow this  
marko001

Search within an array

Recommended Posts

marko001

Hi all,

i'm burning my brain trying to solve this.

I need to find and extract a value into a var from an array:

global $AR_Merge [9] [2] = [ _
[8,16], _
[14,8], _
[12,24], _
[6,8], _
[22,72], _
[18,74], _
[1,400], _
[25,340], _
[7,400]]

What I'd like to get is, for example, the value "74" given the value 18"

If i use _arraysearch($AR_Merge, 18) it returns, correctly, 5, that's the ROW where 18 is. But i need to extract the 74.

Is there a way to do it? (similar to the excel formula SEARCH.VERT or SEARCH.HORIZ)

Thanks everyone,

Marco

Edited by marko001

Share this post


Link to post
Share on other sites
funkey

#Include <Array.au3>

Local $AR_Merge [9] [2] = [ _
  [8,16], _
  [14,8], _
  [12,24], _
  [6,8], _
  [22,72], _
  [18,74], _
  [1,400], _
  [25,340], _
  [7,400]]
 
Local $Res = _ArraySearch($AR_Merge, 18)
ConsoleWrite($AR_Merge[$Res][1] & @cr)


Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites
FlyinRiz

$search = 18
$result = $AR_Merge[_ArraySearch($AR_Merge, $search)][1]

You should look into learning a little about 2D Arrays. It might help in understanding how to work with them.

-Aaron

Share this post


Link to post
Share on other sites
Schuss

#Include <Array.au3>

global $AR_Merge [9] [2] = [ _
[8,16], _
[14,8], _
[12,24], _
[6,8], _
[22,72], _
[18,74], _
[1,400], _
[25,340], _
[7,400]]


MsgBox(0,"Result",$AR_Merge [_arraysearch($AR_Merge, InputBox("Search Array", "Enter your lookup value.", "18"))] [1])

Share this post


Link to post
Share on other sites
saywell

You should look into learning a little about 2D Arrays. It might help in understanding how to work with them.

-Aaron

Yes, the Autoit help is rather scanty in this area - all the examples seem to be 1D. I've been puzzling over the syntax of 2D arrays without much success, so thanks for that link which sheds some light - though some more autoit- specific info would be useful. Eg really basic stuff like ho to specify a bit of data in a given location - I've tried various combinations of paired brackets without success! Looking at the link, it seems to be a pair of numbers, within a single pair of square brackets, separated by a comma.

I think it's one of those areas that trained/experienced programmers find so easy that they don't understand why mere mortals find it difficult!

So thanks from me, too!

William

Share this post


Link to post
Share on other sites
FlyinRiz

Yes, the Autoit help is rather scanty in this area - all the examples seem to be 1D. I've been puzzling over the syntax of 2D arrays without much success, so thanks for that link which sheds some light - though some more autoit- specific info would be useful. Eg really basic stuff like ho to specify a bit of data in a given location - I've tried various combinations of paired brackets without success! Looking at the link, it seems to be a pair of numbers, within a single pair of square brackets, separated by a comma.

I think it's one of those areas that trained/experienced programmers find so easy that they don't understand why mere mortals find it difficult!

So thanks from me, too!

William

The easiest way to explain it is as such (using books/Excel)...

Variable = Word/Cell($var)

1D Array = Line/Row ($array[2])

2D Array = Page/Worksheet ($array[2][5])

3D Array = Book/Workbook ($array[2][5][3])

4D Array = Shelf/Folder of Workbooks ($array[2][5][3][7])

etc...

The only other big thing to worry about is whether the reference starts at 0 (C++, AutoIt) or 1 (VB)...

So in the above example using an AutoIt array,

$array[2][4][1]

would refer to the 3nd word of the 5th line in the 2nd book. Make sense?

-Aaron

Share this post


Link to post
Share on other sites
AdmiralAlkex

Yes, the Autoit help is rather scanty in this area - all the examples seem to be 1D. I've been puzzling over the syntax of 2D arrays without much success, so thanks for that link which sheds some light - though some more autoit- specific info would be useful. Eg really basic stuff like ho to specify a bit of data in a given location - I've tried various combinations of paired brackets without success! Looking at the link, it seems to be a pair of numbers, within a single pair of square brackets, separated by a comma.

Did you try the tutorial at the Wiki? You do know we have a Wiki, right? The big link at the top of the page?

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  

×