Jump to content

Handy Functions


Bartokv
 Share

Recommended Posts

Below are a few custom functions that I've created, and use fairly regularly in my scripts. I don't know if anybody else would find them as useful as I, but I thought that I'd post them just in case. Please keep in mind that I'm new to AU3, so if you have any recommendations for code improvement please let me know.

...Enjoy! :whistle:

; ****************************************************************************
;  Returns the Variable type (STRing, NUMber, FLoaT, ARraY, INTeger, ERRor)
; ****************************************************************************
  Func TypeOf($TypeOfVariableName)
    $TypeOfReturnValue = "ERR"        ; This value should never be returned
    If IsString($TypeOfVariableName) Then $TypeOfReturnValue = "STR"
    If IsNumber($TypeOfVariableName) Then $TypeOfReturnValue = "NUM"
    If IsFloat($TypeOfVariableName) Then $TypeOfReturnValue = "FLT"
    If IsArray($TypeOfVariableName) Then $TypeOfReturnValue = "ARY"
    If IsInt($TypeOfVariableName) Then $TypeOfReturnValue = "INT"
    Return $TypeOfReturnValue
  EndFunc

; ****************************************************************************
;  Returns a String Conisting of the Specified Character and Length
; ****************************************************************************
  Func Pad($PadChar, $PadLen)
    If $PadChar == "" Then $PadChar = " "   ; Use space as default padding
    If $PadLen > 500 Then $PadLen = 500     ; Try to keep limit reasonable
    If $PadLen < 0 Then $PadLen = 0         ; Idiot-proofing - just in case
    $PadOutString = ""

    For $PadCount = 1 To $PadLen
      $PadOutString = $PadOutString & $PadChar
    Next
    Return $PadOutString
  EndFunc

; ****************************************************************************
;  Centers Text within the Specified Width
; ****************************************************************************
  Func Center($CenterString, $CenterWidth, $CenterSpacer)
    $CenterStringLen = StringLen($CenterString)
    If $CenterWidth < $CenterStringLen Then $CenterWidth = $CenterStringLen
    If $CenterSpacer = "" Then $CenterSpacer = " "
    $CenterOutString = ""

    $CenterPadding = ($CenterWidth - $CenterStringLen) / 2
    If INT($CenterPadding) <> $CenterPadding Then
      $CenterLeftPadding = INT($CenterPadding) + 1
      $CenterRightPadding = INT($CenterPadding)
    Else
      $CenterLeftPadding = $CenterPadding
      $CenterRightPadding = $CenterPadding
    EndIf

    $CenterOutString = Pad($CenterSpacer, $CenterLeftPadding) & $CenterString & Pad($CenterSpacer, $CenterRightPadding)
    Return $CenterOutString
  EndFunc

; ****************************************************************************
;  Resizes the Specified Array to the Desired Depth
; ****************************************************************************
  Func ResizeArray(ByRef $ArrayName, $ArraySize)
    If TypeOf($ArrayName) <> "ARY" Then Return SetError(1)
    If TypeOf($ArraySize) <> "INT" Then Return SetError(2)
    If $ArraySize < 0 Then $ArraySize = 0   ; Idiot-proofing - just in case

    Dim $ArrayTempValues[$ArraySize]        ; Create swap array with new size
    $ArrayOldMax = Ubound($ArrayName) - 1   ; Get size of original array

    If $ArraySize < $ArrayOldMax Then       ; Try to avoid needless bloat
      $ArrayCountMax = $ArraySize - 1
    Else
      $ArrayCountMax = $ArrayOldMax
    Endif

    For $ArrayCounter = 0 to $ArrayCountMax ; Populate swap array with data
      $ArrayTempValues[$ArrayCounter] = $ArrayName[$ArrayCounter]
    Next

    $ArrayName = $ArrayTempValues           ; Replace original array with swap
    $ArrayTempValues = ""                   ; Dump swap array to free memory
  EndFunc

; ****************************************************************************
;  Retreives Header Block Names from the Specified INI File
; ****************************************************************************
  Func Get_INI_headers($INI_file, ByRef $INI_ArrayName)
    $INI_stream = FileOpen($INI_file, 0)
    If TypeOf($INI_ArrayName) <> "ARY" Then Return SetError(1)

    $INI_count = 1

   ; Verify file is open for reading
    If $INI_stream == -1 Then
      MsgBox(1, "Error", "Unable to open configuration file.")
      Return SetError(1)
    EndIf

    Dim $INI_head[1]

   ; Read in lines of text until the EOF is reached
    While 1
      Sleep(10)                             ; Release some ticks to CPU
      $line = FileReadLine($INI_stream)     ; Get next line in file
      If @error == -1 Then ExitLoop

      If StringLeft($line, 1) == "[" Then   ; Header block marker found...
        $HeadName = StringSplit($line, "[]")
        If $HeadName[0] > 2 Then            ; Verify INI Header name is valid
          ResizeArray($INI_ArrayName, $INI_count)
          $INI_ArrayName[$INI_count - 1] = $HeadName[2]
          $INI_count = $INI_count + 1
        EndIf
      Endif
    Wend

    FileClose($INI_stream)
  EndFunc
Link to comment
Share on other sites

  • 9 years later...

That´s cool.

Here are the one´s that I paste to the bottom of pretty much every code.

I find

cr(@error) 

 much faster than 

ConsoleWrite(@error&@CRLF)

and since it returns itself I can debug a line "If @error = 1 Then Exit" by the small change of "If cr(@error) = 1 Then Exit", without any effect on the operation of the script :-D

 

When I use functions like StringRegExp that return arrays, and I (very often) just want the first result, I find

$sString = StringStripWS(na(StringRegExp($sSource, '(E\d{2}-\d{4}-\d{2}) / MARKET', 1)))

once again quicker than

$aResults  = StringRegExp($sSource, 'somepa(tt)ern', 1)
$sString = StringStripWS($aResults[0],7)

(I use "na" as short for "NoArray")

#Region Basic Script Functions (Exit, CR, NA)
Func _Exit()
    Exit
EndFunc   ;==>_ExitLoop
Func cr($v= "") ;Print to console, Returns itself.
    ConsoleWrite($v & @CR)
    Return $v
EndFunc   ;==>cr
Func na($aArray)
    If IsArray($aArray) Then
        Return $aArray[0]
    ElseIf IsString($aArray) Then
        Return $aArray
    Else
        SetError(1)
        Return ""
    EndIf
EndFunc   ;==>na
#EndRegion Basic Script Functions (Exit, CR, NA)

P.S. maybe a mod can shift this post to the Examples section?

Link to comment
Share on other sites

Wow, necroing a 10 years old thread!

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...