Jump to content
Sign in to follow this  
kcvinu

Range function to create instant integer arrays

Recommended Posts

kcvinu

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

My Contributions

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)

 

Share this post


Link to post
Share on other sites
kcvinu

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


My Contributions

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)

 

Share this post


Link to post
Share on other sites
jaberwacky

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
  • Like 1

Share this post


Link to post
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
Sign in to follow this  

  • Similar Content

    • TrashBoat
      By TrashBoat
      So Im trying to make a simple 2d game and make some sort of collision detection so why not to make a 2 dimensional array but i have no clue how  to write it in multiple lines
      Global $map[5,5] = [0,0,0,0,0 _ [0,0,0,0,0 _ [0,0,0,0,0 _ [0,0,0,0,0 _ [0,0,0,0,0] something like this but it doesn't work
    • Zein
      By Zein
      #include "..\Include\Array.au3" #include "..\Include\File.au3" #include "..\Include\AutoItConstants.au3" Local $aRetArray Local $sFilePath = "n.csv" _FileReadToArray($sFilePath, $aRetArray, ",") ; _FileReadToArray($sFilePath, $aRetArray, $FRTA_COUNT, ",") _ArrayDisplay($aRetArray, "Original", Default, 8) The above code shows two versions of _FileReadToArray and both don't work as expected.
      The first one doesn't use the comma as a delimiter. (so I get a single column array)  I tried adding "Default" between $aRetArray and "," then it told me it had an incorrect number of parameters. 

      I looked again at the documentation:
       
      #include <File.au3> _FileReadToArray ( $sFilePath, ByRef $vReturn [, $iFlags = $FRTA_COUNT [, $sDelimiter = ""]] )
      And I with or without the flags params I should be getting a 2D array due to my file being a csv. 
      I then tried a regular flag, $FRTA_COUNT, and it tells me that I'm using a variable $FRTA_COUNT while it's not declared. Tried putting in 1 instead and it told me again, incorrect number of params. 

       
    • ternal
      By ternal
      Hi,
      Recently I have had the need to do a sort and then do a second sort while the item of the first sort stays the same ( double sorting , first on column x then while column x is the same sort column y).
      I did not put much efffort into error checking but so far I did not need it.
      For my applications so far it works perfectly however if someone is willing I want to test this extensivly.
      If anyone has big lists of random stuff to sort could you try this out please?
      #include <Array.au3> ; #FUNCTION# ==================================================================================================================== ; Name ..........: _ArraySort_Double ; Description ...: ; Syntax ........: _ArraySort_Double (Byref $array[, $first_index = Default[, $second_index = Default[, $ascending = Default]]]) ; Parameters ....: $array - 2d array to sort. ; $first_index - [optional] first column to sort. Default is 0. ; $second_index - [optional] second column to sort. Default is 1. ; $ascending - [optional] ascending/descending. Default is 1. ; Return values .: 1 if no errors occured , -1 if errors occured ; Author ........: Ternal ; Remarks .......: Needs excessive testing. ; Related .......: _arraysort() ; =============================================================================================================================== Func _ArraySort_Double (byref $array, $first_index = Default, $second_index = Default, $ascending = Default) Local $temp_value Local $counter = 1 If UBound($array, $UBOUND_DIMENSIONS) <> 2 Then MsgBox(0, "error", "error") return -1 EndIf If $first_index = Default Then $first_index = 0 If $second_index = Default Then $second_index = 1 If $ascending = Default Then $ascending = 1 _ArraySort($array, $ascending, 0, 0, $first_index); you can alter settings of primary sort here If @error Then MsgBox(0, "error", @error) return -1 EndIf $temp_value = $array[0][$first_index] For $x = 1 to UBound($array, 1) - 1 If Mod( $x, 10000) = 0 Then ConsoleWrite("at " & $x & " of a total : " & UBound($array, 1) & @CRLF) If $array[$x][$first_index] = $temp_value Then $counter+= 1 If $x = UBound($array, 1) - 1 Then; do last line here(if last line is not a new item) _ArraySort($array, $ascending, $x - $counter, $x, $second_index);you can alter settings of secondary sort here(don't forget to place line 34 the exact same) If @error Then MsgBox(0, "error", @error) return -1 EndIf EndIf Else If $counter > 0 Then ;at least 2 of the same _ArraySort($array, $ascending, $x - $counter, $x - 1, $second_index);you can alter settings of secondary sort here(don't forget to place line 29 the exact same) If @error Then MsgBox(0, "error", @error) return -1 EndIf $counter = 1 EndIf EndIf $temp_value = $array[$x][$first_index] Next Return 1 EndFunc Kind regards, Ternal
    • TrashBoat
      By TrashBoat
      So I've made this script that detects how long i have held down my left mouse button for and stores the information in an array and then sorts its using _ArraySort but the output is half sorted half broken.
      Here's my script:
      HotKeySet("{F1}","_exit") #include <Misc.au3> #include <Timers.au3> #include <Array.au3> Local $dll = DllOpen("user32.dll") $on = False Global $array[0] While(1) If _IsPressed(01,$dll) Then $timer = _Timer_Init() While _IsPressed(01,$dll) Sleep(1) WEnd $time = _Timer_Diff($timer) _ArrayAdd($array,"Time: " & Floor($time) & " ms") ;~ ConsoleWrite("Time: " & Floor($time) & " ms" & @CRLF) EndIf Sleep(50) WEnd Func _exit() _ArraySort($array) _ArrayDisplay($array) Exit EndFunc And the output:

      See how its not sorted?  What is the problem here?
    • MrCheese
      By MrCheese
      Hi guys,
      See attached for an array example.
      to simplify what i want to achieve,  I want to split this array into 9 different csv files.
      the first file would contain the list of "key" and the corresponding "ID1", the second would have "key" and the "ID2", the third would have "key" and "ID3"
      However, I want to remove all the rows that don't have an ID recorded in the respective ID2, ID3 4...5...6 etc, so the file only contains row items with a key and the ID.
      Would be the best way to loop through the rows and delete the row if the array field is blank - would I then need to repeat that row ID to check that the row that its replaced is also empty (ie the one after the one I just deleted)? I see this getting messy.
      or _arraySort, and delete everything below the last filled row? <-- this might be best?
      Or should I use the excel UDF, apply a filter (not selecting the blanks), then create/export to the array->csv?
       
      Super keen to hear your thoughts.
      thanks!
       
       
       
       
      IDArray.csv
×