Jump to content

[Solved] Count array without empty entries


Recommended Posts

I'm having a little trouble finding the right function to count my array.

I have an array that can have a random number of entries, but when I use Ubound to count the array, it also counts the entries that are empty. I can get up to 365 entries total in the array

Global $value[10] = [100, 132, 138, 117, 122, 108, 130]

Can I somehow make an array where I dont have a preset number ($value[] = [100, 132, 138, 117, 122, 108, 130]) or can I somehow find a result of 7 entries with my array example?

I don't want to loop through the array and check for empty entries if there are other ways to do it.

Edited by Hest
Software:Model Train Calculator (Screen)Autoit3 beginner!
Link to comment
Share on other sites

I'm having a little trouble finding the right function to count my array.

I have an array that can have a random number of entries, but when I use Ubound to count the array, it also counts the entries that are empty. I can get up to 365 entries total in the array

Global $value[10] = [100, 132, 138, 117, 122, 108, 130]

Can I somehow make an array where I dont have a preset number ($value[] = [100, 132, 138, 117, 122, 108, 130]) or can I somehow find a result of 7 entries with my array example?

I don't want to loop through the array and check for empty entries if there are other ways to do it.

Hi,

#include <array.au3>
Global $value[10] = [100, 132, 138, 117, 122, 108, 130]
$aresult = _ArrayFindAll ($value, "")
MsgBox (0,"", UBound ($value) - UBound ($aresult))

;-))

Stefan

Link to comment
Share on other sites

I don't want to loop through the array and check for empty entries if there are other ways to do it.

No there isn't, you must loop through the array to check the empty entries.

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

But it's better to generate the array in the way that allows you to use only non empty entries, for example:

#include <Array.au3>

Global $avArray[100]

For $i = 1 To 15
    $avArray[0] += 1
    
    $avArray[$i] = "Some data #" & $i
Next

_ArrayDisplay($avArray)

;Here we use the [0] to indicate where non empty entries ending
For $i = 1 To $avArray[0]
    ConsoleWrite($avArray[$i] & @CRLF)
    
    If $avArray[$i] = "" Then MsgBox(64, 'Title', 'Empty entry: ' & $i) ;This will never be shown, since we check only filed entries
Next

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Here are two examples but the" for loop" version is fastest.

#include 'array.au3'

Global $value[10] = [100, 132, 138, 117, 122, 108, 130]
Global $result = 0
Global $timer = 0, $dif = 0

$timer = TimerInit()
$result = _ArrayCount($value)
$dif = TimerDiff($timer)

ConsoleWrite("_ArrayCount = " & $dif & @CRLF)
ConsoleWrite("     result = " & $result & @CRLF)

$timer = TimerInit()
$result = _ArrayCountv2($value)
$dif = TimerDiff($timer)

ConsoleWrite("_ArrayCountv2 = " & $dif & @CRLF)
ConsoleWrite("       result = " & $result & @CRLF)

Func _ArrayCount(ByRef $Array)
    Local $Undefined = _ArrayFindAll($Array, "")
    Return UBound($Array) - UBound($undefined)
EndFunc

Func _ArrayCountv2(ByRef $Array)
    Local $i, $count = 0
    
    For $i = 0 To UBound($Array) - 1
        If $Array[$i] <> "" Then $count += 1
    Next
    
    Return $count
EndFunc

Results

  • _ArrayCount = 0.613206427073832 ms
  • _ArrayCountv2 = 0.0466539741782824 ms

--- TTFN

Link to comment
Share on other sites

How did you create the array? If it was with _FileReadToArray() then there is a faster method you could use with RegExp.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

How did you create the array? If it was with _FileReadToArray() then there is a faster method you could use with RegExp.

I'll have a look at that also. I'm going to read everything from a file and into an array.

Here are two examples but the" for loop" version is fastest.

Thx to you too. I tried the script Stefan wrote and thats alot slower than the for script ^_^

I have something to work with now.

Software:Model Train Calculator (Screen)Autoit3 beginner!
Link to comment
Share on other sites

$aRegExp = StringRegExp(FileRead("C:\some\path\myfile.txt", "(?i)\s*(\S\V*)\v?", 3)
$iLines = Ubound($aRegExp)

$aRegExp will not contain any blank Elements so $iLines will be the number of non-empty lines

Note This can also be used to read the file to an array. It also strips leading space from each line.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

$aRegExp = StringRegExp(FileRead("C:\some\path\myfile.txt", "(?i)\s*(\S\V*)\v?", 3)
$iLines = Ubound($aRegExp)

$aRegExp will not contain any blank Elements so $iLines will be the number of non-empty lines

Note This can also be used to read the file to an array. It also strips leading space from each line.

Thx, this I can use. Will make it alot faster. How can I store everything in an array?
Software:Model Train Calculator (Screen)Autoit3 beginner!
Link to comment
Share on other sites

Thx, this I can use. Will make it alot faster. How can I store everything in an array?

$aRegExp is an array. It just doesn't have leading spaces or empty elements. Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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