Jump to content

Need help with the _arraysearch() on a 2D array - (Moved)


Recommended Posts

I am trying to use the _ArraySearch() function to trigger a condition when the 3rd element of $arrayTwo is not found in the $arrayOne. 

 

#include <Array.au3>

local $arrayOne[3][2]
$arrayOne[0][0] = UBound($arrayOne, 1)-1
$arrayOne[1][0] = "Notepad.exe"
$arrayOne[1][1] = 1234
$arrayOne[2][0] = "Notepad.exe"
$arrayOne[2][1] = 5678
_ArrayDisplay($arrayOne, "$arrayOne")


local $arrayTwo[4][2]
$arrayTwo[0][0] = UBound($arrayTwo, 1)-1
$arrayTwo[1][0] = "Notepad.exe"
$arrayTwo[1][1] = 1234
$arrayTwo[2][0] = "Notepad.exe"
$arrayTwo[2][1] = 5678
$arrayTwo[3][0] = "Notepad.exe"
$arrayTwo[3][1] = 9101
_ArrayDisplay($arrayTwo, "$arrayTwo")


for $x=1 to $arrayTwo[0][0]
    If Not _ArraySearch($arrayOne, $arrayTwo[$x][1], 0, 0, 0, 0, 1, 1) Then
        MsgBox(0, "", "Not Found element "&$arrayTwo[$x][1]&" in $arrayOne")
    EndIf
next

 

The above script does not trigger the MsgBox instruction. Please help me what am I doing wrong. 

 

Regards,

 

Edited by naturelaws
Link to comment
Share on other sites

  • naturelaws changed the title to Need help with the _arraysearch() on a 2D array

As per help file of _ArraySearch

Quote
Success: the index that $vValue was found at.
Failure: -1 and sets the @error flag to non-zero.

You need to check @error because result of the function is always converted to True

Link to comment
Share on other sites

7 hours ago, naturelaws said:
For $x=1 to $arrayTwo[0][0]
    If _ArraySearch($arrayOne, $arrayTwo[$x][1], 0, 0, 0, 0, 1, 1) < 0 Then
        MsgBox(0, "", "Not Found element "&$arrayTwo[$x][1]&" in $arrayOne")
    EndIf
Next

 

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

  • Moderators

Moved to the appropriate AutoIt General Help and Support forum, as the Developer General Discussion forum very clearly states:

Quote

General development and scripting discussions.


Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums.

Moderation Team

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

@Deye you got the comparison wrong: if a match is found, the index of the found array element is returned, which is >= 0, else no match returns -1, which as you know is < 0.

EDIT: sorry I looked too fast, focussing on the > 0 only. My bad.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Hello everybody :)

There are a couple of things I don't understand in this thread, could you please explain, thanks.

On 10/1/2024 at 11:42 AM, Nine said:

You need to check @error because result of the function is always converted to True

@Nine What does it mean exactly ?
Return of the function is -1 when it fails, so what is always converted to True ?

@jchd I always script it a bit like you (in your 1st post) i.e. check first the result of _ArraySearch ( >= 0 means found)
If it's not >= 0, then only I'd check @error to display the @error value in a MsgBox etc...

7 hours ago, jchd said:

 Deye, you got the comparison wrong [...]

Why is that ?
It seems to me that Deye's way returns correct results (see script below) but he could make it easier.

@Deye why the complication of "* -1 > 0"  Isn't it simpler to check it with "<0" when it fails (which is equivalent to the "-1" indicated in the help file) ?

Test script :

#include <Array.au3>

Local $aArray[5] = [2, 50, -4, 0, 10]

For $i = 0 To 2
    $iIndex = _ArraySearch($aArray, $i) ; 0+ if found, -1 if not found
    If $iIndex = -1 Then
        ConsoleWrite("Element " & $i & " not found in array" & @crlf)
    Else
        ConsoleWrite("Element " & $i & " found in array (index = " & $iIndex & ")" & @crlf)
    EndIf
Next

ConsoleWrite("-----" & @crlf)

For $i = 0 To 2
    If _ArraySearch($aArray, $i) * -1 > 0 Then
        ConsoleWrite("Deye's way : Not Found element " & $i & " in array" & @crlf)
    EndIf
Next

Guys, please don't get mad at me if I wrote something wrong, in case I missed something that is obvious to you.
Thanks for reading.

Edited by pixelsearch
typo
Link to comment
Share on other sites

1 hour ago, pixelsearch said:

Why is that ?
It seems to me that Deye's way returns correct results (see script below) but he could make it easier.

Post edited: I did see the * -1 part (and didn't expect it), just the > 0 caught my (olding) eyes.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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