Jump to content

Range function to create instant integer arrays


kcvinu
 Share

Recommended Posts

Hi all,

Here is new UDF to create integer arrays instantly. 

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.12.0
 Author:         kcvinu

 Script Function:This function will give you an array of integer
                 If you give only Stop_Number, it will give you an array[stop_number]
                 Example => Range(9) = [0,1,2,3,4,5,6,7,8,9]
                 If you give both parameters, it will give you an array [start_number to stop_number]
                 Example => Range(9,3) = [3,4,5,6,7,8,9]
                 If you give step number, it will add the elements by step number
                 Example => Range(9,3,2) = [3,5,7,9]
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

#include <Array.au3>
Func Range($To_Number, $From_Number = 0, $Step_Number = 1)

    If $From_Number < 0 Then
        MsgBox(0, "Range Function", "From Number must be > 0")
    ElseIf $To_Number < $From_Number Then
        MsgBox(0, "Range Function", "To Number must be > From Number")
    ElseIf $Step_Number > $To_Number Then
        MsgBox(0, "Range Function", "Step Number must be < To Number")
    EndIf

    If $From_Number = 0 And $Step_Number = 1 Then
        Global $ReturnArray[$To_Number + 1]
        For $Element = 0 To $To_Number Step $Step_Number
            $ReturnArray[$Element] = $Element
        Next
        Return $ReturnArray
    ElseIf $From_Number = 0 And $Step_Number > 1 Then
        Local $index = 0
        Local $TempVar1 = 0
        While $TempVar1 <= $To_Number
            $TempVar1 += $Step_Number
            $index += 1
        WEnd
        Global $ReturnArray[$index]
        For $j = 0 To $index-1
            $ReturnArray[$j] = $From_Number
            $From_Number += $Step_Number
        Next
        Local $LastIndex = (UBound($ReturnArray)) -1
            if $ReturnArray[$LastIndex] > $To_Number Then
            _ArrayDelete($ReturnArray,$LastIndex)
            EndIf
        Return $ReturnArray

    ElseIf $From_Number > 0 Then
        If $Step_Number = 1 Then
            Local $Diff = ($To_Number - $From_Number) + 1

            Global $ReturnArray[$Diff]
            Local $index = 0
            For $Element = $From_Number To $To_Number
                $ReturnArray[$index] = $Element
                $index += 1
            Next
            Return $ReturnArray

        ElseIf $Step_Number > 1 Then
            Local $TempVar1 = $From_Number
            Local $index = 0
            While $TempVar1 < $To_Number
                $TempVar1 += $Step_Number
                $index += 1
            WEnd

            Global $ReturnArray[$index + 1]
            For $j = 0 To $index
                $ReturnArray[$j] = $From_Number
                $From_Number += $Step_Number
            Next
            Local $LastIndex = (UBound($ReturnArray)) -1
            if $ReturnArray[$LastIndex] > $To_Number Then
            _ArrayDelete($ReturnArray,$LastIndex)
            EndIf
            Return $ReturnArray

        EndIf
    EndIf

EndFunc   ;==>Range
 

If you want to create a list of integers, you can use this like Local $List = Range(12,2,2)

This will make an array like this => [2,4,6,8,10,12]

Range.au3

Edited by kcvinu
Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Link to comment
Share on other sites

Hi all,

Last day, i found some bugs in my Range.au3 script and i have debugged all. Now i have edited my post and updated the Range.au3 file. So I am requesting those who downloaded (I found 6 downloads) this file last day, please delete that file and use the updated Range.au3. Thank you all. :)

Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Link to comment
Share on other sites

Thanks for contributing!

Here is another version (not extensively tested):

#include <Array.au3>

Const $test1 = Range(9)
Const $test2 = Range(9, 3)
Const $test3 = Range(9, 3, 2)
Const $test4 = Range(9, -3)
Const $test5 = Range(9, -3, 2)

_ArrayDisplay($test1, "Range: 9")
_ArrayDisplay($test2, "Range: 3 - 9")
_ArrayDisplay($test3, "Range: 3 - 9 -- Step: 2")
_ArrayDisplay($test4, "Range: -3 - 9")
_ArrayDisplay($test5, "Range: -3 - 9 -- Step: 2")

Switch @error
  Case 1
    MsgBox(0, "Range Function", "To Number must be > From Number")
  
  Case 2
    MsgBox(0, "Range Function", "Step Number must be < To Number")
EndSwitch

Func Range(Const $To_Number, Const $From_Number = 0, Const $Step_Number = 1)
    Select
      Case $To_Number < $From_Number        
        Return SetError(1, 0, False)
      
      Case $Step_Number > $To_Number
        Return SetError(2, 0, False)
    EndSelect
    
    Local Const $upbound = _get_upbound($To_Number, $From_Number, $Step_Number) + 1
    
    Local $range[$upbound]
    
    Local $inc = $From_Number
    
    For $i = 0 To $upbound - 1
      $range[$i] = $inc
      
      $inc += $Step_Number 
    Next
    
    Return $range
EndFunc

Func _get_upbound(Const $To_Number, Const $From_Number, Const $Step_Number)
  Local $upbound
  
  Select        
      Case $From_Number < 0
        $upbound = (Abs($From_Number) + $To_Number)
        
      Case Else
        $upbound = ($To_Number - $From_Number)
  EndSelect
  
  Return ($Step_Number > 1) ? ($upbound / $Step_Number) : ($upbound)
EndFunc
Edited by jaberwacky
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...