Jump to content

[Solved] Analyse an array ?


hcI
 Share

Recommended Posts

Hello everyone !

I have a little problem

I would like to do a little program who analyse an array and return the 1st line of the array who is empty

For example :

Global $aArray[5] = "abc","def","ghi","","mno"
; And Then, (maybe a For loop who) return in a variable "4"
; because the 4th case of the array is empty

I've took a look in helpfile but found nothing intrusting..

If someone have already done this or have idea how to proceed..

Hci

Edited by hcI
Problem resolved
Link to comment
Share on other sites

Sure... Something like this:

Global $aArray[5] = ["abc", "def", "ghi", "", "mno"]

MsgBox(64, "first empty index", getFirstEmptyIndex($aArray))

Func getFirstEmptyIndex($ar)
    For $index = 0 to UBound($ar) - 1
        If $ar[$index] = "" Then Return $index
    Next
    Return -1
EndFunc   ;==>getFirstEmptyIndex

Note that it will return a 3 as arrays are 0-based, instead of the 4 you expected. If you need it to return 4, you can easily fix that in this snippet.

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

SadBunny's example will work just fine for what OP wants but one of the problems with autoit and strings is the implicit conversion to 0 when a string is empty.

Global $aArray[5] = ["abc", "def", "ghi", 0, "mno"]

MsgBox(64, "first empty index", getFirstEmptyIndex($aArray))

Func getFirstEmptyIndex($ar)
    For $index = 0 to UBound($ar) - 1
        If $ar[$index] = "" Then Return $index
    Next
    Return -1
EndFunc   ;==>getFirstEmptyIndex

0 is a valid value and that index is not empty (just the int representation of zero). This is really just for future reference but when looking for empty strings, I like to use StringLen instead when checking for a null string

Global $aArray[5] = ["abc", "def", "ghi", 0, "mno"]

MsgBox(64, "first empty index", getFirstEmptyIndex($aArray))

Func getFirstEmptyIndex($ar)
    For $index = 0 to UBound($ar) - 1
        If (StringLen($ar[$index]) = 0) Then Return $index
    Next
    Return -1
EndFunc   ;==>getFirstEmptyIndex

 

Link to comment
Share on other sites

One needs to define "empty".  If the use case is only finding an empty string, then the only sure way is to test for both datatype and string length.

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

×
×
  • Create New...