Jump to content

Check if array exist


Recommended Posts

hello every1, i have these codes to plus all the element [0] from all the array

$a=+$Files[0]

the script run fine as long as exist the element [0], unless (there is not anny array), the script crash ...

so ... is there any way to check if the element[0] exit ?

P/S : the element[0] show the total total numer in the array, so if there's no array, there's no element[0]

Thankx

[quote]Don't expect for a perfect life ... Expect a least troubles ones[/quote]Contact me : ass@kiss.toWhat I Have Done :Favorites Manager Mangage your favorite's folder, that's coolPC Waker For those who want to save stickersWebScipts Supporter For those who've just started with Web and WebScriptsTemporary Looker Simple but powerful to manage your Temporary folder, you know what you downloaded[UDF] _NumberFormat() Better performance on number display[UDF] _DirGet() What a folder contain [how many (hidden,normal,...) files], with one line of code[UDF] _IsPressEs() Just like _IsPress() but for a group of keys

Link to comment
Share on other sites

hello every1, i have these codes to plus all the element [0] from all the array

$a=+$Files[0]

the script run fine as long as exist the element [0], unless (there is not anny array), the script crash ...

so ... is there any way to check if the element[0] exit ?

P/S : the element[0] show the total total numer in the array, so if there's no array, there's no element[0]

Thankx

UBound or IsArray()

:)

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

Hello, i'm sorry but these fixed code was trouble me once more ... as you can see, if exist an array, mean the [0] has a value, the script works fine, otherwise, the script crashes, i put a ELSE too check but it's just the same.

for $j=1 to UBound($Files)-1
if IsNumber(UBound($Files[0])-1) > 0 Then
;MsgBox(64,"","some thing")
$a=+$Files[0]
EndIf
Next
MsgBox(64,"",$a) ;CRASH IF DON'T HAVE ELEMENT[0]

[quote]Don't expect for a perfect life ... Expect a least troubles ones[/quote]Contact me : ass@kiss.toWhat I Have Done :Favorites Manager Mangage your favorite's folder, that's coolPC Waker For those who want to save stickersWebScipts Supporter For those who've just started with Web and WebScriptsTemporary Looker Simple but powerful to manage your Temporary folder, you know what you downloaded[UDF] _NumberFormat() Better performance on number display[UDF] _DirGet() What a folder contain [how many (hidden,normal,...) files], with one line of code[UDF] _IsPressEs() Just like _IsPress() but for a group of keys

Link to comment
Share on other sites

i read and did, but it just sit right there ... :)

[quote]Don't expect for a perfect life ... Expect a least troubles ones[/quote]Contact me : ass@kiss.toWhat I Have Done :Favorites Manager Mangage your favorite's folder, that's coolPC Waker For those who want to save stickersWebScipts Supporter For those who've just started with Web and WebScriptsTemporary Looker Simple but powerful to manage your Temporary folder, you know what you downloaded[UDF] _NumberFormat() Better performance on number display[UDF] _DirGet() What a folder contain [how many (hidden,normal,...) files], with one line of code[UDF] _IsPressEs() Just like _IsPress() but for a group of keys

Link to comment
Share on other sites

No, i don't want to increase the variable , this's what i am doing :

the Files[0] stand for the the number of total element in the array, if you use _ArrayDisplay(), you can see the [0] always show the total ...

And i want to count all of them, i have four Array, meant four Files[0]

[quote]Don't expect for a perfect life ... Expect a least troubles ones[/quote]Contact me : ass@kiss.toWhat I Have Done :Favorites Manager Mangage your favorite's folder, that's coolPC Waker For those who want to save stickersWebScipts Supporter For those who've just started with Web and WebScriptsTemporary Looker Simple but powerful to manage your Temporary folder, you know what you downloaded[UDF] _NumberFormat() Better performance on number display[UDF] _DirGet() What a folder contain [how many (hidden,normal,...) files], with one line of code[UDF] _IsPressEs() Just like _IsPress() but for a group of keys

Link to comment
Share on other sites

hello every1, i have these codes to plus all the element [0] from all the array

$a=+$Files[0]

the script run fine as long as exist the element [0], unless (there is not anny array), the script crash ...

so ... is there any way to check if the element[0] exit ?

P/S : the element[0] show the total total numer in the array, so if there's no array, there's no element[0]

Thankx

Instead of

$a=+$Files[0]

try

If IsArray($Files) then $a += $Files[0]
Link to comment
Share on other sites

Thanks anyway but maybe i'll find a different method

[quote]Don't expect for a perfect life ... Expect a least troubles ones[/quote]Contact me : ass@kiss.toWhat I Have Done :Favorites Manager Mangage your favorite's folder, that's coolPC Waker For those who want to save stickersWebScipts Supporter For those who've just started with Web and WebScriptsTemporary Looker Simple but powerful to manage your Temporary folder, you know what you downloaded[UDF] _NumberFormat() Better performance on number display[UDF] _DirGet() What a folder contain [how many (hidden,normal,...) files], with one line of code[UDF] _IsPressEs() Just like _IsPress() but for a group of keys

Link to comment
Share on other sites

Hello, i'm sorry but these fixed code was trouble me once more ... as you can see, if exist an array, mean the [0] has a value, the script works fine, otherwise, the script crashes, i put a ELSE too check but it's just the same.

for $j=1 to UBound($Files)-1
if IsNumber(UBound($Files[0])-1) > 0 Then
;MsgBox(64,"","some thing")
$a=+$Files[0]
EndIf
Next
MsgBox(64,"",$a) ;CRASH IF DON'T HAVE ELEMENT[0]oÝ÷ Ûú®¢×v÷öصìZrÙr«{*.ëméî·«¡÷2rW¬b¦z{b¢|¨º
-áªëk+8)±ø¥{E¡z|¨¹×w­éh¢aÌË*.)¢÷«¶§¶ßW¬{ayø«²ÛÚ®&í7éø¥zÄáxZ+5ìm)ºÇ¬jÞuçâçoj¸nW*.v÷öÙ'£ay©¨º{h}Ìë2zܨ¹Æ§ºÇ')h­¥¡WzwN¶«Â+¶¡×°Yp¢¹"*.z0¶§ºfÞ®íg¬jëh×6for $j=1 to 4; the number of arrays you need to add
    if IsNumber($Files[0]) Then
    ;MsgBox(64,"","some thing")
        $a=+$Files[0]
    EndIf
; Statement to give the array $files new values, e.g.
    $Files = _FileReadToArray($Dirname) 
Next
MsgBox(64,"",$a)
Edited by DMEE

In the beginning there was nothing and then even that exploded - anonymous

Link to comment
Share on other sites

Im not sure if this is what you want, but im bored so ... sue me :)

*Sits back and waits for the Eval() Bashing comments :)*

dim $array1[4] = [3 , "test1" , "test2" ,"test3"]
dim $array2[4] = [3 , "test1" , "test2" ,"test3"]
dim $array3[4] = [3 , "test1" , "test2" ,"test3"]
dim $array4[4] = [3 , "test1" , "test2" ,"test3"]
dim $total = 0

;The neat way
IF IsArray($array1) Then
    $total += $array1[0]
Else
    ;array is not an Array do error handeling
EndIf
IF IsArray($array2) Then
    $total += $array2[0]
Else
    ;array is not an Array do error handeling
EndIf
IF IsArray($array3) Then
    $total += $array3[0]
Else
    ;array is not an Array do error handeling
EndIf
IF IsArray($array4) Then
    $total += $array4[0]
Else
    ;array is not an Array do error handeling
EndIf
MsgBox(0 , "output", "The total elements in all array's is: " & $total)


; Different way using eval() although this is probably not adviced for more complex operations.
$total = 0
For $i=1 To 4 ; total number of arrays
    $temp = Eval("array" & $i)
    IF IsArray( $temp ) Then
        $total += $temp[0]
    Else
        ;array is not an Array do error handeling
    EndIf
Next
MsgBox(0 , "output", "The total elements in all array's is: " & $total)
+==================================================================+| The Definition of Madness: Creating a GUI, with GUI automation scripts |+==================================================================+
Link to comment
Share on other sites

Ok, thanks you both DMEE and Prophet ...

@DMEE : your code is ok at first, when the element[0] exist, but how about if it not ?

i can't specific the element in the array, so it's great if i there are some ways to prevent the script crash when the element doesn't exist

This is the full code

func _MakeLog()
;||----------

$ContentDir = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Cache\Paths", "Directory")
$InContent=_FileListToArray($ContentDir,"*.*",2);show content
;----------||


;||----------
$Delindex=_ArraySearch($InContent,"index.dat");delete index.dat file from the array
If Not @error Then
_ArrayDelete($InContent,$Delindex)
EndIf
;----------||


;||----------
$Deldesktop=_ArraySearch($InContent,"desktop.ini");delete desktop.ini file from the array
If Not @error Then
_ArrayDelete($InContent,$Deldesktop)
EndIf
;----------||


;||----------
$Element=0
_ArrayDelete($InContent,$Element);delete the [0]
;----------||



for $i=0 to UBound($InContent)-1
$Dir=$ContentDir & "\" & $InContent[$i];dir for each folder inside Content.IE5

$Files=_FileListToArray($Dir,$SpeEx)

for $j=1 to UBound($Files)-1

FileWriteLine(GUICtrlRead($SaveDir) & "\[Temporary Looker Log]_" &"["&$time & ".txt",$Dir & "\" & $Files[$j])
If IsNumber($Files[0]) then 
$a =+ $Files[0]
Else
MsgBox(64,"","No item")
EndIf
Next
Next
MsgBox(64,"",$a) ;CRASH IF DONT HAVE ELEMENT


EndFunc
;----------------------------------------
;END OF '_MakeLog FUNCTION'
;----------------------------------------

[quote]Don't expect for a perfect life ... Expect a least troubles ones[/quote]Contact me : ass@kiss.toWhat I Have Done :Favorites Manager Mangage your favorite's folder, that's coolPC Waker For those who want to save stickersWebScipts Supporter For those who've just started with Web and WebScriptsTemporary Looker Simple but powerful to manage your Temporary folder, you know what you downloaded[UDF] _NumberFormat() Better performance on number display[UDF] _DirGet() What a folder contain [how many (hidden,normal,...) files], with one line of code[UDF] _IsPressEs() Just like _IsPress() but for a group of keys

Link to comment
Share on other sites

Check if $a has value before activating the MSGbox

If $a Then
    MsgBox(64,""," The value of $a is: " & $a)
Else
    MsgBox(64,"error","Error $a contains no value")
EndIf

[edit]

removed typo

[/edit]

Edited by Prophet
+==================================================================+| The Definition of Madness: Creating a GUI, with GUI automation scripts |+==================================================================+
Link to comment
Share on other sites

hello every1, i have these codes to plus all the element [0] from all the array

$a=+$Files[0]

the script run fine as long as exist the element [0], unless (there is not anny array), the script crash ...

so ... is there any way to check if the element[0] exit ?

P/S : the element[0] show the total total numer in the array, so if there's no array, there's no element[0]

Thankx

Have a look at these examples of how to check if an array is valid and contains data.

Dim $Files[100]
    $Files[0] = 4
Global $a = 0

;Example 1 ;$Files[0] contains a valid count
If IsArray($Files) Then
    If $Files[0] > 0 Then
        $a += $Files[0]
    EndIf
EndIf
MsgBox(64,"",$a) ;CRASH IF DON'T HAVE ELEMENT[0]

;Example 2
$Files[0] = -1 ;$Files[0] contains an invalid count for number of array elements
$a = 0
If IsArray($Files) Then
    If $Files[0] > 0 Then
        $a += $Files[0]
    EndIf
EndIf
MsgBox(64,"",$a) ;CRASH IF DON'T HAVE ELEMENT[0]

;Example 3 
$Files = 0 ;$Files is no longer an array
$a = 0
If IsArray($Files) Then
    If $Files[0] > 0 Then
        $a += $Files[0]
    EndIf
EndIf
MsgBox(64,"",$a) ;CRASH IF DON'T HAVE ELEMENT[0]


;Example 4 
$Files = 0 ;$Files is no longer an array
$a = 0
If IsArray($Files) Then
    For $i = 1 To $Files[0] > 0 Then
        $a += $Files[0]
    EndIf
EndIf
MsgBox(64,"",$a) ;CRASH IF DON'T HAVE ELEMENT[0]

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

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