Jump to content

_ArraySearch Help(Solved)


Recommended Posts

I just seem to be missing something with _ArraySearch.  I modified the help file just to test what I am trying to do.  I am searching an array for "End of Record".  From that I want to search backwards and find the first " Begining of Record".   But my second search keeps starting at the end of the file.   

Local $avArray
_FileReadToArray("X:\Temp\test2.txt", $avArray, $FRTA_NOCOUNT)
;_ArrayDisplay($avArray, "$avArray")

Local $sSearch = "End of Record"
Local $bSearch = "Begining of Record"
Local $iIndex = _ArraySearch($avArray, $sSearch, 0, 0, 0, 1, 1)
MsgBox($MB_SYSTEMMODAL, "iIndex", $iIndex)
Local $jIndex = _ArraySearch($avArray, $bSearch, $iIndex, 0, 0, 0, 0)
If @error Then
    MsgBox($MB_SYSTEMMODAL, "Not Found", '"' & $sSearch & '" was not found' & '.')
Else
    MsgBox($MB_SYSTEMMODAL, "Found", '"' & $sSearch & '" was found in the array at position ' & $iIndex & '.'&@CRLF& "encouter was found at "& $jIndex)
EndIf

What am I doing wrong?   Shouldn't $jIndex start the search where $iIndex was found?

Thank you any help you can give me. 

Edited by xcaliber13
test file
Link to post
Share on other sites

Use :

Local $jIndex = _ArraySearch($avArray, $bSearch, 0, $iIndex, 0, 0, 0)

instead of :

Local $jIndex = _ArraySearch($avArray, $bSearch, $iIndex, 0, 0, 0, 0)

 

Edited by Musashi

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to post
Share on other sites

Your starting and ending indexes, on the second array search need to be reversed.  It should be:

$jIndex = _ArraySearch($avArray, $bSearch, 0, $iIndex, 0, 1, 0)

If you look at the _ArraySearch function's logic, in the array.au3 UDF, you'll see that if $iForward is true false, it will reverse the indexes.  Therefore, you should set the starting and ending index parameters as you normally would, from top to bottom.  By setting the $iForward flag to true false, it will correctly search the defined range in reverse order.

Edit:

I didn't see that @Musashi had already replied.  😃

Edited by TheXman
Corrected mistakes pointed out by pseakins :)
Link to post
Share on other sites
  • xcaliber13 changed the title to _ArraySearch Help(Solved)
4 minutes ago, TheXman said:

I didn't see that @Musashi had already replied.  😃

Yes, but you also provided the appropriate explanation. This will certainly make it easier for the OP to understand the principle :).

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to post
Share on other sites
5 minutes ago, xcaliber13 said:

Read the help file many times but just could not see that.

You're welcome.  If the help file doesn't provide answers, sometimes it helps to look at the UDF function's code to see how and why it is working the way it does.  That's what I did in this case.  :)  I, like you, initially assumed that the indexes should be reversed too.  :)

Edited by TheXman
Link to post
Share on other sites
8 hours ago, TheXman said:

By setting the $iForward flag to true, it will correctly search the defined range in reverse order.

A little clarification. When the flag $iForward is true (1), which is the default value, it searches forward. To make it search backward the flag should be set to false (0).

 

Phil Seakins

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...