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

    • lattey
      By lattey
      hi,
      i have checkboxes and each checkbox that checked, i put in array. 
      now, im stuck on how to loop the checked array and store in in one variable. what i can do now, is only write the result into a text file. 
      below is the code:
      #include <GUIConstantsEx.au3> ;~ #include <MsgBoxConstants.au3> #include <ButtonConstants.au3> #include <Array.au3> Global $Count = 3 Global $CheckBoxP[$Count] Global $step[$Count] global $array1[1] Global $ExitResult $hGUI = GUICreate("Summary Steps", 500, 400) GUISetFont(12, 400, "Tahoma") GUICtrlCreateLabel( "Please Select the Summary Steps for Script Check", 70, 20) GUISetFont(10, 400, "Tahoma") Global $array_Pstep[3] = ["fix2","fix1","fix3"] global $step[3] = ["2","3","4"] $Spacing = 50 For $i = 0 To UBound($array_Pstep) - 1 $CheckBoxP[$i] = GUICtrlCreateCheckbox($array_Pstep[$i], 80, $Spacing + (20 * $i), 65, 17) Next $submit = GUICtrlCreateButton("Submit",180, 280, 80, 30) $exit = GUICtrlCreateButton("Exit",180, 320, 80, 30) GUISetState() While 1 $Msg = GUIGetMsg() Select case $Msg=$submit For $i = 0 To $Count - 1 If GUICtrlRead($CheckBoxP[$i]) = $GUI_CHECKED Then _ArrayAdd($array1, $step[$i]) EndIf Next Global $logfilerray = @WorkingDir & "\checkedlist.txt" FileDelete ($logfilerray) Global $readlogfile = FileOpen($logfilerray,1) for $a = 1 to UBound($array1) - 1 ;~ $var=$array1[$a] FileWriteLine($readlogfile,$array1[$a]) Next FileClose($readlogfile) Exit case $Msg=$exit $ExitResult = MsgBox(1,"Summary Step", "Continue to Exit ?") if $ExitResult = 1 Then ;ok Exit EndIf Exit EndSelect WEnd  
    • omicron
      By omicron
      How do you perform a nested loop function with a multidimensional array from 2 lists.
      for i in list1
      (open file) extract variable
          while open for i in list 2
          (open file2) extract variable
       
      var1 + var2 = (search term)

      The list sizes will more than likely consist of different lengths.
       
      What is the best approach to accomplishing this method?
             
    • omicron
      By omicron
      Hello!

      I am working on a function that I am just getting lost on. The goal is a multiple nested loop.

      Here are the steps:
      Contents of file1.txt::
      [topic] var1=Name var2=OtherName var3=SomeotheName Contents of file2.txt::
      [subTopic] top=sub1 top2=sub2 top3=sub3 The Shell I am working from::
      #include <file.au3> $file = "c:\yourfile.txt" FileOpen($file, 0) For $i = 1 to _FileCountLines($file) $line = FileReadLine($file, $i) msgbox(0,'','the line ' & $i & ' is ' & $line) Next FileClose($file) Understanding however that the "msgbox" needs to then become a variable. in example the following::
      $file = "c:\yourfile.txt" FileOpen($file, 0) While true( prog.exe is running && "WinName" is open) do For $i = 1 to _FileCountLines($file) $line = FileReadLine($file, $i) ;Open File to log "current location of file 1" FileWriteLine ("filename", $i & ' is ' & $line) var = $line Next $file2 = "c:\yourfile.txt" FileOpen($file, 0) For $i = 1 to _FileCountLines($file) $line = FileReadLine($file, $i) ; OpenFile to log "Current location of file 2" FileWriteLine ("filename", $i & ' is ' & $line) Next FileClose($file2) FileClose($file) The goal in written form is the following ::

      While in "OpenWindow"
          read from file 1 starting at line 1 until end of file.
         file 1 is a list of names to be searched.
         With $line selected, add this element to the element in file 2.
       
      The search of a variables in list 1 and list 2 differ on the amount of posts that day. (This is not a web based platform, it is a game) I need to search 2 names and take a screenshot of the out put. The sizes of the names list depend on the activity of names at the time of search.
      This loop continues until all the names from both lists have been searched. Mostly in the format of::
      File1= item
      File2= Vendor
       
      Item + Vendor  ( Capture screen, scroll) -- Not sure how to detect if I need to scroll)
       
      Thank you for your help and support!
    • Skeletor
      By Skeletor
      Hi Virtual People,
      My array works perfectly fine. However, what is the best practice if the line in the array doesn't have the correct amount of columns and if I can add a placeholder?

       
      For $count = 1 To _FileCountLines($FileRead1) Step 1 $string = FileReadLine($FileRead1, $count) $input = StringSplit($string, ",", 1) $value1 = $input[1] $value2 = $input[2] $value3 = $input[3] _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "A1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "B1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1") Next  
    • MrCheese
      By MrCheese
      hi all,
      reviewing the forum, this thread is applicable: 
       
       
      I wanted to know if there is now a better way to do this?
      In essence, I load a tab delimited txt file into an array (works well). I used tab, as some fields in the original csv contains commas.
      However, I needed autoit to manipulate this array, and output it as a csv.
      IF my array contains items with a comma, without double quotes around the field, then how best do I get a csv out of this?
      My current workaround is to filewritefromarray tab delimited, then open it in excel and save as a csv. I will need to check this to see how the address fields behave that contain a comma.
       
      Any thoughts would be appreciated.
       
×