Jump to content

_ArrayBinarySearch ()


Recommended Posts

When I first seen _ArrayBinarySearch () I was quite happy to know that I didnt have to search each array seperatly like...

For $I = 0 To $Array
   If $avArray[$I] = "blah" Then
   ;Blah Blah
   EndIf
Next

But it has just come to my attention that _ArrayBinarySearch () only matches the exact string, where as I thought it would be StringInStr

I have a script where I need to seach a whole array but not specifiy which elements. Except I'm not looking for an exact match, I'm looking for a StringInStr.

I need to some how make a fucntion or script that will allow me to use

_ArrayBinarySearch() and StringInStr() so I can search for a string in an element of the array without searching through each element one by one.

Edit: Ok, I think the Function will be pretty easy to make, only thing I'm having trouble is that I dont know the number of elements that will be inputed...

eg, all the _Array() Functions like _ArrayDisplay() already now how many elements are in the array. Is there someway I can find this out?

Edited by Burrup

qq

Link to comment
Share on other sites

  • Developers

Why not copy the _ArrayBinarySearch () and change it to your needs ?

By the way BinarySearch and StringInstr do not match!!!

BinarySearch is based on a Greater than - Less than logic ....

StringInstr() logic is "if the string exists anywhere in the value"

Or did you mean you want to check the beginning of each entry in the array?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I need it so search the value to see if it contains the string anywhere.

#include <Array.au3>
Dim $avArray[10]

$avArray[0] = "JPM"
$avArray[1] = "Holger"
$avArray[2] = "Jon"
$avArray[3] = "Larry"
$avArray[4] = "Jeremy"
$avArray[5] = "Valik"
$avArray[6] = "Cyberslug"
$avArray[7] = "Nutster"
$avArray[8] = "JdeB"
$avArray[9] = "This|Is|A|Test"

_ArraySort( $avArray)

_ArrayDisplay( $avArray, "sorted" )

$iKeyIndex = _ArrayBinarySearch ( $avArray, "A|Test")
If Not @error Then
   Msgbox(0,'Entry found',' Index:' & $iKeyIndex)
Else
   Msgbox(0,'Entry Not found',' Error:' & @error)
EndIf

See what I mean. It searches the array for that exact text, "A|Test" but not each value to see if it contains it. I sort of starting making a function and I got this so far.

Func _ArrayBinarySearch_SIS($avArray, $iKey)
   $Array = 10
   _ArrayBinarySearch ( $avArray, $iKey)
   If @Error = 3 Then
      For $I = 0 To $Array - 1
         If StringInStr($avArray[$I],$iKey) Then
            Return $I
         EndIf
      Next
      SetError(3)
   ElseIf @Error = 2 Then
      SetError(2)
   ElseIf @Error = 1 Then
      SetError(1)
   ElseIf @Error = 0 Then
      Return $I
   EndIf
EndFunc

I dont usually create UDF's so its probably all wrong :).

Edited by Burrup

qq

Link to comment
Share on other sites

  • Developers

See what I mean. It searches the array for that exact text, "A|Test" but not each value to see if it contains it. I sorted starting function and I got this so far.

<{POST_SNAPBACK}>

I understand what you mean but you clearly don't understand me .....

I know how the ArrayBinarySearch() UDF works because i wrote it... :)

Maybe you need to do some "Googling" on how a binary search works.. then you probably understand what i meant in my first reply.

anyway, with StringInstr() there's a chance that more than one entry in the array has a match .... how do you want to handle that ?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

In computer science, binary search is a search algorithm for searching a set of sorted data for a particular value. - Googled :)

* Is the number greater than 8? (Yes)

* Is the number greater than 12? (No)

* Is the number greater than 10? (Yes)

* Is the number greater than 11? (No)

Therefore, the number must be 11.

Why not copy the _ArrayBinarySearch () and change it to your needs ?

How do I "copy" _ArrayBinarySearch (), you mean get the source and change it to my needs or use the algorithm and create a UDF?

Nice job on _ArrayBinarySearch () btw :D.

Edited by Burrup

qq

Link to comment
Share on other sites

  • Developers

In computer science, binary search is a search algorithm for searching a set of sorted data for a particular value. - Googled :)

    * Is the number greater than 8? (Yes)

    * Is the number greater than 12? (No)

    * Is the number greater than 10? (Yes)

    * Is the number greater than 11? (No)

Therefore, the number must be 11.

<{POST_SNAPBACK}>

Close, but with arrays its a bit different.

You sort the values of the array.

Then compare to the value halve-way the array.

When its smaller you compare to the value halveway of the top halve of the array.

etc, so essentially cutting the array in halve with every test till you found the value.

Thats why you cannot do StringStr() test with this algoritme... that chance you miss the correct entry is BIG...

The only option i see is a sequential approach....

How do I "copy" _ArrayBinarySearch (), you mean get the source and change it to my needs or use the algorithm and create a UDF?

<{POST_SNAPBACK}>

Its part of the Array.au3 in the Include subdirectory....... Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I cut to the bit where I tell you what I'm actually doing, and see if you can tell me another way to do this.

I'm creating a 'Media Player', how cliche lol. And its still only the being and I've been creating a List bow that has the following columns.

Title|Artist|Album|Length|Year|Genre|Track #|Bit Rate

I use a FileOpen Dialog, I then use LazyCats UDF's to retrieve the Tag Info. So now it displayes a song name , length etc. When you click on the "Title" column it sorts it by Alphabetical order with this/

If GuiCtrlGetState($Audio_List) = 0 Then
   For $I = 0 To $Array - 1
      $Split_File = StringSplit($Audio_Files[$I],"<>")
      If $I = 0 Then
         $Audio_Info = _ArrayCreate ($Split_File[3])
      Else
         _ArrayAdd ($Audio_Info, $Split_File[3])
      EndIf
   Next
   If $Title_Sort = 0 Then
      _ArraySort($Audio_Info)
      $Title_Sort = 1
   ElseIf $Title_Sort = 1 Then
      _ArraySort($Audio_Info)
      _ArrayReverse ($Audio_Info)
      $Title_Sort = 0
   EndIf
   GuiCtrlSendMsg($Audio_List, 0x1009,0,0)
   For $I = 0 To $Array - 1
      $Split_Title = StringSplit($Audio_Info[$I],"|")
      GUICtrlCreateListViewItem($Split_Title[1] & "|"& $Split_Title[2] & "|" & $Split_Title[3] & "|" & $Split_Title[4] & "|" & $Split_Title[5] & "|" & $Split_Title[6] & "|" & $Split_Title[7] &  "|" & $Split_Title[8],$Audio_List)
   Next
EndIf

When it gets the file form FileOpen Dialog it saves it to the array $Audio_Files. So an eg. of this array once you selected a song would look like this

C:\myfile.mp3<>Title|Artist|Album|Length|Year|Genre|Track #|Bit Rate

I then split it so I just have

Title|Artist|Album|Length|Year|Genre|Track #|Bit Rate

Because I'm sorting it by Title, its already at the front so I can directly use _ArraySort($Audio_Info), but when i get to Artist its not up the front, which makes it alot harder.

What I am currenty to is splitting it again so now I have.

Artist|Album|Length|Year|Genre|Track #|Bit Rate

I then sort that. Then I was this going to use _ArrayBinarySearch () to search Title|Artist|Album|Length|Year|Genre|Track #|Bit Rate for Artist|Album|Length|Year|Genre|Track #|Bit Rate inside it. But because as I have previoulsy found out _ArrayBinarySearch () doesnt search like that.

As you can see all I want to do is sort the files but artist name but it becomes difficult because I have to seperated the name, search for it and put it back together.

If you bothered to read al the above lol :), I am open to other suggestions so I dont have to it that way we are talking about.

Edit: Dont worry :D, I just figured out away, very simple to lol, I use _ArraySwap() to swap the title and the artist, so the artist is now up the front, sort it, and then just before setting the Listview I swap it back.

Edited by Burrup

qq

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...