Jump to content

A simple problem about _ArraySearch()


Recommended Posts

Hi,

I am trying to find all instances of a value in an array. _arraysearch returns the first occurance. So I tried to use the following code to get all the occurances. It's not working properly.

#include <Array.au3>
Dim $search_pos[6];array to hold positions
Dim $hold[6];array to search in
$hold[0] = "10"
$hold[1] = "9"
$hold[2] = "8"
$hold[3] = "10"
$hold[4] = "3"
$hold[5] = "10"
$val_to_search = "10";the value to search
$i = 0
$search_pos[$i] = _ArraySearch($hold, $val_to_search, 0);first instance of search
While ($search_pos[$i] <> "")
    $search_pos[$i] = _ArraySearch($hold, $val_to_search, ($search_pos[$i]+1))
    $i = $i + 1
WEnd

For $j = 0 to UBound($search_pos)-1
    MsgBox(0,'',$search_pos[$j])
    $j = $j+1
Next

Ne suggestions?

I believe we should all pay our tax with a smile. I tried - but they wanted cash!

Link to comment
Share on other sites

  • Moderators

#include <Array.au3>
Dim $search_pos[6];array to hold positions
Dim $hold[6];array to search in
$hold[0] = "10"
$hold[1] = "9"
$hold[2] = "8"
$hold[3] = "10"
$hold[4] = "3"
$hold[5] = "10"
$val_to_search = "10";the value to search

For $x = 0 To UBound($hold) - 1
    If StringInStr($hold[$x], $val_to_search) Then MsgBox(0, 'Value Found', 'Value was found in array $hold[' & $x & ']')
Next

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Wow, that was quick!

Thanks SmOke_N :o

But I still don't understand where am I lacking logic in my original script?

Edit: Actually I need to hold all the positions, i.e. $x's in your script in an array. How do I do that?

Edited by kclteam

I believe we should all pay our tax with a smile. I tried - but they wanted cash!

Link to comment
Share on other sites

maybe

#include <Array.au3>
Dim $search_pos[6];array to hold positions
Dim $hold[6];array to search in
$hold[0] = "10"
$hold[1] = "9"
$hold[2] = "8"
$hold[3] = "10"
$hold[4] = "3"
$hold[5] = "10"
$val_to_search = "10";the value to search

for $i = 0 to 5
    $search_pos[$i] = _ArraySearch($hold, $val_to_search, $i)
Next

For $j = 0 to UBound($search_pos)-1
    MsgBox(0,'',$search_pos[$j])
    $j = $j+1
Next

Dah.... way to slow this time...

8)

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

  • Moderators

My logic was flawed anyway... StringInString() is not the right option there!! Because if you were looking for let's say '1' and there was a '10' only, it would still say it was there... Vals works... here's another option:

#include <Array.au3>
Dim $hold[6];array to search in
Dim $search_pos = ''
Dim $StoreArray = ''
$hold[0] = "10"
$hold[1] = "9"
$hold[2] = "8"
$hold[3] = "10"
$hold[4] = "3"
$hold[5] = "10"
$val_to_search = "10";the value to search

For $x = 0 To UBound($hold) - 1
    If $hold[$x] == $val_to_search Then $search_pos = $search_pos & $x & Chr(01)
Next
$StoreArray = StringSplit(StringTrimRight($search_pos, 1), Chr(01)); this is the stored values
If IsArray($StoreArray) Then 
    For $i = 1 To UBound($StoreArray) - 1
        MsgBox(0, 'Value Found', 'Value was found in array $hold[' & $StoreArray[$i] & ']')
    Next
Else
    MsgBox(0, 'Error', 'There were no like values found')
EndIf

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Hi,

I don´t whether it is a good solution, but it seems to work. :o

#include <Array.au3>
Dim $search_pos = _ArrayCreate("")
Dim $hold = _ArrayCreate("10","9", "8", "10", "3", "10"); array to search in
$val_to_search = "10"; the value to search

for $i = 0 to (UBound($hold)-1)
    If StringInStr($hold[$i], $val_to_search) Then 
    _ArrayAdd($search_pos, _ArraySearch($hold, $val_to_search, $i))
    EndIf
Next
_ArrayDisplay($search_pos, "Found " & $val_to_search & " @")

Edit: Okay, SmOke_N you are right. *g*

So long,

Mega

Edited by th.meger

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Ohh...thanx for bringing that fact into my notice..I will also take care about it.

But I have yet another problem...I adapted Valuater's idea into a part of my script, but it says Array variable subscript badly formatted

The code is:

For $j = 0 to UBound($ip)-1
    $search_pos[$j] = _ArraySearch($ip, $val_to_search, $j)
Next

_GUICtrlListViewDeleteAllItems($list_no1)
For $k = 0 to UBound($search_pos)-1
$field = $id[$search_pos[$k]] & " * " & $name[$search_pos[$k]] & "*" & $ip[$search_pos[$k]] & "*" & $email[$search_pos[$k]] & "*" & $host[$search_pos[$k]] & "*" & $hostip[$search_pos[$k]]
GuiCtrlCreateListViewItem($field, $list_no1)
$k = $k+1
Next

The error comes at $id[$search_pos[$k]]

And thank you mega for helping too :o

Edited by kclteam

I believe we should all pay our tax with a smile. I tried - but they wanted cash!

Link to comment
Share on other sites

  • Moderators

Ohh...thanx for bringing that fact into my notice..I will also take care about it.

But I have yet another problem...I adapted Valuater's idea into a part of my script, but it says Array variable subscript badly formatted

The code is:

For $j = 0 to UBound($ip)-1
    $search_pos[$j] = _ArraySearch($ip, $val_to_search, $j)
Next

_GUICtrlListViewDeleteAllItems($list_no1)
For $k = 0 to UBound($search_pos)-1
$field = $id[$search_pos[$k]] & " * " & $name[$search_pos[$k]] & "*" & $ip[$search_pos[$k]] & "*" & $email[$search_pos[$k]] & "*" & $host[$search_pos[$k]] & "*" & $hostip[$search_pos[$k]]
GuiCtrlCreateListViewItem($field, $list_no1)
$k = $k+1
Next

The error comes at $id[$search_pos[$k]]

And thank you mega for helping too :o

Did you declare $id[] / $name[] / $ip[] / $email[] / $host[] / $hostip[] all as array variables?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

Yups...they are all arrays and have values fetched from a MySQL Database

So they all have valuse like:

Dim $Id[Number]
$Id[1] = Something
Etc...
? If so... we could play the guessing game here, or you could provide a working example :o

Edit:

If you adapted the last one I wrote... like this... what happens?

#include <Array.au3>
Dim $hold[6];array to search in
Dim $search_pos = ''
Dim $StoreArray = ''
$hold[0] = "10"
$hold[1] = "9"
$hold[2] = "8"
$hold[3] = "10"
$hold[4] = "3"
$hold[5] = "10"
$val_to_search = "10";the value to search

For $x = 0 To UBound($hold) - 1
    If $hold[$x] == $val_to_search Then $search_pos = $search_pos & $x & Chr(01)
Next
$StoreArray = StringSplit(StringTrimRight($search_pos, 1), Chr(01)); this is the stored values
If IsArray($StoreArray) Then 
    _GUICtrlListViewDeleteAllItems($list_no1)
    For $i = 1 To UBound($StoreArray) - 1
        $field = $id[$StoreArray[$i]] & " * " _ 
        & $name[$StoreArray[$i]] & "*" _ 
        & $ip[$StoreArray[$i]] & "*" _ 
        & $email[$StoreArray[$i]] & "*" _ 
        & $host[$StoreArray[$i]] & "*" _ 
        & $hostip[$StoreArray[$i]]
        GuiCtrlCreateListViewItem($field, $list_no1); << Your resetting $list_no1 over and over... you know that right? (At least 3 times with this example)
    Next
Else
    MsgBox(0, 'Error', 'There were no like values found')
EndIf
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

CODE

Func User_search($ip_to_search, $list_no1)

Dim $search_pos[500]

$sql = _MySQLConnect($username, $password, $database1, $server)

$column0 = "user_active"

$condition = "1"

$number_users_active = _CountRecords($sql, $table, $column0, $condition)

If ($number_users_active == 0) Then

Return

EndIf

$search_cmd = "SELECT * FROM " & $table & " WHERE 1"

$search_ips = _Query($sql, $search_cmd)

If ($search_ips == 0) Then

MsgBox(0, $title, "Error searching the user")

Else

$i = 0

With $search_ips

While ($i < $number_users_active)

$csid[$i] = .Fields("user_id").value

$csname[$i] = .Fields("username").value

$csip[$i] = .Fields("user_csip").value

$csemail[$i] = .Fields("user_email").value

$cshost[$i] = .Fields("user_reg_host").value

$cshost[$i] = .Fields("user_reg_ip").value

.MoveNext

$i = $i + 1

WEnd

EndWith

EndIf

For $x = 0 To UBound($csip) - 1

If $csip[$x] == $ip_to_search Then $search_pos = $search_pos & $x & Chr(01)

Next

$StoreArray = StringSplit(StringTrimRight($search_pos, 1), Chr(01))

If IsArray($StoreArray) Then

_GUICtrlListViewDeleteAllItems($list_no1)

For $i = 1 To UBound($StoreArray) - 1

$field = $csid[$StoreArray[$i]] & " * " & $csname[$StoreArray[$i]] & "*" & $csip[$StoreArray[$i]] & "*" & $csemail[$StoreArray[$i]] & "*" & $cshost[$StoreArray[$i]] & "*" & $cshostip[$StoreArray[$i]]

GuiCtrlCreateListViewItem($field, $list_no1)

Next

Else

MsgBox(0, 'Error', 'There were no like values found')

EndIf

_MySQLEnd($sql)

EndFunc

Here is the whole function. I have already applied your search method...but it shows me just the first entry in the $csip array even when it's value in no way related to the one I am searching. :o

I believe we should all pay our tax with a smile. I tried - but they wanted cash!

Link to comment
Share on other sites

  • Moderators

Take a look at this, you don't have all the info there, so I couldn't check for errors:

Func User_search ($ip_to_search, $list_no1)
    Local $search_pos = '', $csid[500], $csname[500], $csip[500], $csemail[500], $cshost[500], $chostip[500]; You didn't have them declared like I asked and you had $search_pos[500], when it isn't an array with my example, it's a storage bin is all
    $sql = _MySQLConnect ($username, $password, $database1, $server)
    $column0 = "user_active"
    $condition = "1"
    $number_users_active = _CountRecords ($sql, $table, $column0, $condition)
    If ($number_users_active == 0) Then
        Return
    EndIf
    $search_cmd = "SELECT * FROM " & $table & " WHERE 1"
    $search_ips = _Query ($sql, $search_cmd)
    If ($search_ips == 0) Then
        MsgBox(0, $title, "Error searching the user")
    Else
        $i = 0
        With $search_ips
            While ($i < $number_users_active)
                $csid[$i] = .Fields ("user_id").value
                $csname[$i] = .Fields ("username").value
                $csip[$i] = .Fields ("user_csip").value
                $csemail[$i] = .Fields ("user_email").value
                $cshost[$i] = .Fields ("user_reg_host").value
                $cshostip[$i] = .Fields ("user_reg_ip").value; << you had this as $cshost not $cshostip
                .MoveNext
                $i = $i + 1
            WEnd
        EndWith
    EndIf
    For $x = 0 To UBound($csip) - 1
        If $csip[$x] == $ip_to_search Then $search_pos = $search_pos & $x & Chr(01)
    Next
    $StoreArray = StringSplit(StringTrimRight($search_pos, 1), Chr(01))
    If IsArray($StoreArray) Then
        _GUICtrlListViewDeleteAllItems ($list_no1)
        For $i = 1 To UBound($StoreArray) - 1
            $field = $csid[$StoreArray[$i]] & " * " & $csname[$StoreArray[$i]] & "*" & $csip[$StoreArray[$i]] & "*" & $csemail[$StoreArray[$i]] & "*" & $cshost[$StoreArray[$i]] & "*" & $cshostip[$StoreArray[$i]]
            GUICtrlCreateListViewItem($field, $list_no1); Still reseting this every loop around, is $list_no1 an array... should it have $list_no1[$i] in it?
        Next
    Else
        MsgBox(0, 'Error', 'There were no like values found')
    EndIf
    _MySQLEnd ($sql)
EndFunc  ;==>User_search
I made a few comments

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Sorry SmOke_N about not posting all my script...but my script is way too long :geek: Haven't yet learned to organize my code properly.

And Thanx for all the help...it's working fine now :o

And yeah, thanx for pointing out the mistake in using $cshost twice...and as far as that loop is concerned, $list_no1 is the handle for GUICtrlCreateListView.

But I will look into my script to see if that redrawing is necessary or not.

Thank you once again, you have been a real help and taught me many new things.

I believe we should all pay our tax with a smile. I tried - but they wanted cash!

Link to comment
Share on other sites

  • Moderators

Sorry SmOke_N about not posting all my script...but my script is way too long :geek: Haven't yet learned to organize my code properly.

And Thanx for all the help...it's working fine now :o

And yeah, thanx for pointing out the mistake in using $cshost twice...and as far as that loop is concerned, $list_no1 is the handle for GUICtrlCreateListView.

But I will look into my script to see if that redrawing is necessary or not.

Thank you once again, you have been a real help and taught me many new things.

Hey you learned them... that's the cool thing here... took me a hell of alot longer than most here to get things down (and still don't have but maybe 20% of it)!

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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