Jump to content

Ubound($array) vs $array[0]


Burgaud
 Share

Recommended Posts

Array variable has been giving me headaches the last few months...

an operation like $Array = stringsplit(string1, delimiter) could give me an empty $Array so much so if i assumed otherwise and do:

for $i = 1 to $Array[0]

I can get into trouble... I need to do an error checking first if IsArray($Array) then before looping.

$Array = stringsplit(string1, delimiter)
if IsArray($Array) then
    for $i = 1 to $Array[0]
        .
        .
        .
    next
endif

 

Reading through some scripts, I came upon an array function UBound.

Is it then safer to use it that all the error checkings and what not?

ie:

$Array = stringsplit(string1, delimiter)
for $i = 1 to UBound($Array) -1
    .
    .
    .
next

Is this a a better practice to use UBound than the previous one?

Or is the previous one the better "coding" practice?

 

Dan.

Link to comment
Share on other sites

5 hours ago, Burgaud said:

Is this a a better practice to use UBound than the previous one? Or is the previous one the better "coding" practice?

Using UBound($Array) or $Array[0] is not a question of "better coding practice".

It depends on how the array was created. Let's take a 1-Dim array as an example. In functions you can often specify the structure with a parameter, see $STR_NOCOUNT$STR_COUNT.

If the number of elements is specified in the [0] index, then use $Array[0]. If index [0] already contains the first data element then use UBound (simplified description).

5 hours ago, Subz said:

Just use @error after StringSplit to detect if it was successful or not.

@Burgaud : This is important, e.g. when applying StringSplit to an empty string. IsArray() indicates Success, but @error does not.

#include <Array.au3>
Global $sStr, $aArr, $sDelimiter, $iAtError
$sDelimiter = ","

; ---- string is empty ----
$sStr       = ""
$aArr       = StringSplit($sStr, $sDelimiter)
$iAtError   = @error
ConsoleWrite("> >> String  = " & $sStr & @CRLF)
ConsoleWrite("  >> @error  = " & $iAtError & " (0=No Error , 1=Error)" & @CRLF)
ConsoleWrite("  >> IsArray = " & IsArray($aArr) & " (1=Success , 0=Failure) "  & @CRLF)

; ---- string is not empty ----
$sStr       = "Have,a,nice,day"
$aArr       = StringSplit($sStr, $sDelimiter)
$iAtError   = @error
ConsoleWrite("> >> String  = " & $sStr & @CRLF)
ConsoleWrite("  >> @error  = " & $iAtError & " (0=No Error , 1=Error)" & @CRLF)
ConsoleWrite("  >> IsArray = " & IsArray($aArr) & " (1=Success , 0=Failure) "  & @CRLF)

 

Musashi-C64.png

"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

Sometimes it's nice to have the number of elements in [0], sometimes it's more convenient to have an array, that's holding just values.

 

e.g. the two functions FileReadToArray() and _FileReadToArray()

 

#include <Debug.au3>
#include <File.au3>



$File=@TempDir & "\Sample-Text.txt"
$h=FileOpen($File,2+8)
for $i = 1 to 10
    FileWriteLine($h,"This is line " & $i)
Next

FileClose($h)



$aOne= FileReadToArray($File) ; returns an array with first *ELEMENT* in [0]
_DebugArrayDisplay($aOne)

; Arrays with "fist-element-in-[0]" offer the very nice feature to *ENUMERATE* its element by using "for $Variable in $aArray"
for $line in $aOne
    MsgBox(0, '', $line)
Next

Dim $aTwo
_FileReadToArray($File,$aTwo)
_DebugArrayDisplay($aTwo)


for $i = 1 to $aTwo[0]
    if mod($i,2)=0 then ; even -> display
        MsgBox(0, '', $aTwo[$i])
    EndIf
Next

 

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

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