xcaliber13 Posted December 9, 2020 Share Posted December 9, 2020 (edited) 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 December 9, 2020 by xcaliber13 test file Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted December 9, 2020 Share Posted December 9, 2020 @xcaliber13 Could you please post the sample file you're using, so we can do some tests Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
xcaliber13 Posted December 9, 2020 Author Share Posted December 9, 2020 FrancescoDiMuro just a quick simple text file. The script finds the correct line for $sSearch but does not find the correct line for $bSearch test2.txt Link to comment Share on other sites More sharing options...
Musashi Posted December 9, 2020 Share Posted December 9, 2020 (edited) Use : Local $jIndex = _ArraySearch($avArray, $bSearch, 0, $iIndex, 0, 0, 0) instead of : Local $jIndex = _ArraySearch($avArray, $bSearch, $iIndex, 0, 0, 0, 0) Edited December 9, 2020 by Musashi TheXman and FrancescoDiMuro 2 "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 comment Share on other sites More sharing options...
TheXman Posted December 9, 2020 Share Posted December 9, 2020 (edited) 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 December 10, 2020 by TheXman Corrected mistakes pointed out by pseakins :) FrancescoDiMuro and Musashi 2 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
Musashi Posted December 9, 2020 Share Posted December 9, 2020 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 . "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 comment Share on other sites More sharing options...
xcaliber13 Posted December 9, 2020 Author Share Posted December 9, 2020 Thank you everyone, TheXman thank you for the explanation. Read the help file many times but just could not see that. Again Thank you Link to comment Share on other sites More sharing options...
TheXman Posted December 9, 2020 Share Posted December 9, 2020 (edited) 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 December 9, 2020 by TheXman CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
pseakins Posted December 10, 2020 Share Posted December 10, 2020 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). TheXman 1 Phil Seakins Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now