Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 01/23/2013 in all areas

  1. Have you ever wanted to print an array to the console using ConsoleWrite instead of _ArrayDisplay? I needed a function like this yesterday for my _PathSplitEx thread, so I created this. It's similar to _FileWriteFromArray. Any suggestions for improvement(s) are very welcome. Function: ; #FUNCTION# ==================================================================================================================== ; Name ..........: _PrintFromArray ; Description ...: Print an array to the console. ; Syntax ........: _PrintFromArray(Const Byref $aArray[, $iBase = Default[, $iUBound = Default[, $sDelimeter = "|"]]]) ; Parameters ....: $aArray - [in/out and const] The array to be written to the file. ; $iBase - [optional] Start array index to read, normally set to 0 or 1. Default is 0. ; $iUBound - [optional] Set to the last record you want to write to the File. Default is whole array. ; $sDelimeter - [optional] Delimiter character(s) for 2-dimension arrays. Default is "|". ; Return values .: Success - 1 ; Failure - 0 and sets @error to non-zero ; |@error: ; |1 - Input is not an array. ; |2 - Array dimension is greater than 2. ; |3 - Start index is greater than the size of the array. ; Author ........: guinness ; Modified ......: ; Remarks .......: ; Related .......: _FileWriteFromArray ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _PrintFromArray(ByRef Const $aArray, $iBase = Default, $iUBound = Default, $sDelimeter = "|") ; Check if we have a valid array as input If Not IsArray($aArray) Then Return SetError(1, 0, 0) ; Check the number of dimensions Local $iDims = UBound($aArray, 0) If $iDims > 2 Then Return SetError(2, 0, 0) ; Determine last entry of the array Local $iLast = UBound($aArray) - 1 If $iUBound = Default Or $iUBound > $iLast Then $iUBound = $iLast If $iBase < 0 Or $iBase = Default Then $iBase = 0 If $iBase > $iUBound Then Return SetError(3, 0, 0) If $sDelimeter = Default Then $sDelimeter = "|" ; Write array data to the console Switch $iDims Case 1 For $i = $iBase To $iUBound ConsoleWrite("[" & $i - $iBase & "] " & $aArray[$i] & @CRLF) Next Case 2 Local $sTemp = "" Local $iCols = UBound($aArray, 2) For $i = $iBase To $iUBound $sTemp = $aArray[$i][0] For $j = 1 To $iCols - 1 $sTemp &= $sDelimeter & $aArray[$i][$j] Next ConsoleWrite("[" & $i - $iBase & "] " & $sTemp & @CRLF) Next EndSwitch Return 1 EndFunc ;==>_PrintFromArray Example use of Function: #include <File.au3> Example() Func Example() Local $sDrive = '', $sDir = '', $sFileName = '', $sExtension = '' ConsoleWrite('_PrintFromArray Example 1:' & @CRLF) Local $aArray = _PathSplit('\\Server01\user\docs\Letter.txt', $sDrive, $sDir, $sFileName, $sExtension) _PrintFromArray($aArray) ConsoleWrite(@CRLF) $aArray = _ArrayFill(2) ; Fill a 2d array. _PrintFromArray($aArray) EndFunc ;==>Example ; #FUNCTION# ==================================================================================================================== ; Name ..........: _ArrayFill ; Description ...: Fill a dummy 1d or 2d array. ; Syntax ........: _ArrayFill($iType[, $fIsIndexCount = Default]) ; Parameters ....: $iType - 1 or 2 for the dimension of the array. ; $fIsIndexCount - [optional] Return the array count in the 0th index. Default is True. ; Return values .: Array ; Author ........: guinness ; Example .......: Yes ; =============================================================================================================================== Func _ArrayFill($iType, $fIsIndexCount = Default) Local $iRows = Random(2, 100, 1) If $fIsIndexCount = Default Then $fIsIndexCount = True EndIf Switch $iType Case 2 Local $iCols = Random(2, 10, 1) Local $aReturn[$iRows][$iCols] For $i = 0 To $iRows - 1 For $j = 0 To $iCols - 1 $aReturn[$i][$j] = 'Row ' & $i & ': Col ' & $j Next Next If $fIsIndexCount Then For $i = 0 To $iCols - 1 $aReturn[0][$i] = '' Next $aReturn[0][0] = $iRows - 1 EndIf Case Else Local $aReturn[$iRows] For $i = 0 To $iRows - 1 $aReturn[$i] = 'Row ' & $i Next If $fIsIndexCount Then $aReturn[0] = $iRows - 1 EndIf EndSwitch Return $aReturn EndFunc ;==>_ArrayFill
    1 point
  2. Here another similar way: Global $aFilter = StringRegExp(FileRead(@ScriptDir & "\File.txt"), "\d+\h+\w+\h+\d+\h+\w+\h+\w+\h+\w+\h+\d+\h+\d+.*", 3) If @error Then Exit Global $i, $a, $sLines For $i = 0 To UBound($aFilter) - 1 $a = StringRegExp($aFilter[$i], "\d+\h+\w+\h+\d+\h+\w+\h+\w+\h+\w+\h+\d+\h+(\d+).*", 3) If @error Then ContinueLoop If $a[0] > 3600 Then $sLines &= $aFilter[$i] & @LF Next MsgBox(0, "Test", $sLines) Br, UEZ
    1 point
  3. suprebosu, My most abject apologies - I was looking at the PID column! Just change $aRet[1]) to $aRet[3]). M23
    1 point
  4. czardas

    Sorting an array

    I'm not quite sure what the problem is here. #include <Array.au3> Global $aArray[7] = ["1","11","100","2","20","3","30"] ; Array Contains strings _ArraySort($aArray) ; Sort strings _ArrayDisplay($aArray, "Alphanumeric") For $i = 0 To 6 ; Convert all string values to numbers $aArray[$i] = Number($aArray[$i]) Next _ArraySort($aArray) ; Sort Numbers _ArrayDisplay($aArray, "Numeric Sorting")
    1 point
  5. guinness

    GUI design concepts.

    Please use [autoit][/autoit] tags rather than the generic (code)(/code) tags. Thanks
    1 point
  6. superbosu, This will write the lines with the "Run" value > 3600 to the SciTE console - up to you to get them printed: #include <File.au3> Global $aLines _FileReadToArray("file.txt", $aLines) For $i = 6 To $aLines[0] $aRet = StringRegExp($aLines[$i], "(\d+)", 3) If Number($aRet[1]) > 3600 Then ConsoleWrite($aLines[$i] & @CRLF) EndIf Next Please ask if you have any questions. M23
    1 point
  7. Example 2: #include <String.au3> Example() Func Example() Local Const $ISPALINDROME_DEFAULT = 0, $ISPALINDROME_CASEINSENSITIVE = 1, $ISPALINDROME_STRIPNONALPHA = 2, $ISPALINDROME_STRIPNUMBERS = 4 ConsoleWrite('Racecar: ' & _IsPalindrome('Racecar', $ISPALINDROME_CASEINSENSITIVE) & @CRLF) ConsoleWrite('String: ' & _IsPalindrome('String', $ISPALINDROME_DEFAULT) & @CRLF) ConsoleWrite('A man, a plan, a canal, Panama: ' & _IsPalindrome('A man, a plan, a canal, Panama!', $ISPALINDROME_DEFAULT) & @CRLF) ConsoleWrite('Palindrome: ' & _IsPalindrome('Palindrome', BitOR($ISPALINDROME_CASEINSENSITIVE, $ISPALINDROME_STRIPNONALPHA, $ISPALINDROME_STRIPNUMBERS)) & @CRLF) ConsoleWrite('Was it a car or a cat I saw?: ' & _IsPalindrome('Was it a car or a cat I saw?', $ISPALINDROME_STRIPNONALPHA) & @CRLF) ConsoleWrite('Elk rap song? No sparkle: ' & _IsPalindrome('Elk rap song? No sparkle', BitOR($ISPALINDROME_STRIPNONALPHA, $ISPALINDROME_STRIPNUMBERS)) & @CRLF) ConsoleWrite('I saw desserts; I’d no lemons, alas no melon! Distressed was I: ' & _IsPalindrome('I saw desserts; I’d no lemons, alas no melon! Distressed was I', BitOR($ISPALINDROME_CASEINSENSITIVE, $ISPALINDROME_STRIPNONALPHA, $ISPALINDROME_STRIPNUMBERS)) & @CRLF) EndFunc ;==>Example ; #FUNCTION# ==================================================================================================================== ; Name ..........: _IsPalindrome ; Description ...: Check if a string is a palindrome. ; Syntax ........: _IsPalindrome($sString) ; Parameters ....: $sString - A string value containg the palindrome. ; $iFlag - [optional] An integer value. Default is $ISPALINDROME_DEFAULT (0). ; |$ISPALINDROME_DEFAULT = 0 - Use the string without any modification. ; |$ISPALINDROME_CASEINSENSITIVE = 1 - Convert the string to lowercase. ; |$ISPALINDROME_STRIPNONALPHA = 2 - Strip non-alphanumeric characters from the string. ; |$ISPALINDROME_STRIPNUMBERS = 4 - Strip numbers from the string. ; Return values .: Success - True ; Failure - False ; Author ........: guinness ; Remarks .......: String.au3 must be included. ; Example .......: Yes ; =============================================================================================================================== Func _IsPalindrome($sString, $iFlag = Default) Local Const $ISPALINDROME_DEFAULT = 0, $ISPALINDROME_CASEINSENSITIVE = 1, $ISPALINDROME_STRIPNONALPHA = 2, $ISPALINDROME_STRIPNUMBERS = 4 If $iFlag = Default Then $iFlag = $ISPALINDROME_DEFAULT EndIf If BitAND($iFlag, $ISPALINDROME_CASEINSENSITIVE) Then $sString = StringLower($sString) EndIf If BitAND($iFlag, $ISPALINDROME_STRIPNONALPHA) Then $sString = StringRegExpReplace($sString, '(?i)[^a-z0-9]', '') EndIf If BitAND($iFlag, $ISPALINDROME_STRIPNUMBERS) Then $sString = StringRegExpReplace(StringLower($sString), '[0-9]', '') EndIf Return $sString == _StringReverse($sString) EndFunc ;==>_IsPalindrome
    1 point
  8. water

    Loop not looping ?

    Move the Ping line after the While.
    1 point
  9. GUI design guidelines on MSDN. Covers basic things like correct terminology. But it's things like that which speed up design time (particularly when working with other people). IT goes as far as to cover icon design (and how to do it in photoshop). Even how to structure the text and title in a message box is covered. Since reading it I now get annoyed when an application presents me with an un-professional message box. It's a bit like this I guess. Edit: I also like these guidelines because they are so definite. Like "Don't show a progress bar if the activity will be completed in less than 5 seconds." and they then say what you should use instead.
    1 point
  10. Ah good. Glad Melba got that sorted. I (and another member also) pointed out you cannot have a function in the middle of your While loop. You then asked what a function was and got two responses, I gave a very strong hint that it was a Func tion, and another user also linked you to the helpfile page for functions. Given that information, you should be able to identify the function in your code, whilst keeping the actual code of the function intact. I even gave you the clue that only three lines needed to be removed. Given that two of them will have "Func" in them, and the other one is calling your function, there really is nothing to it. And I'm not even going to summarise the help you got trying to include LockFile. Short of giving you the code, there isn't much more we can do. And we aren't going to give you the code, because that's not how we roll. I haven't seen the result of your attempts yet. It's a bit like learning to ride a bike. We could ride the bike while you sit on our shoulders. But we'd break our backs while you get to your destination but don't learn anything.
    1 point
  11. DRY and KISS, fail fast, SoC and single responsibility... Most of those are common sense and apply not just to programming. This on the other hand is a bit like the bible. You aren't expected to always follow it literally, stoning is generally frowned upon by modern society. Single entry and exit (SESE). Usually overlooked with good reason but can be important, a GUI loop that looks like this is not good: While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd This would be better: While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd Usually there are multiple places in the loop that should end the GUI (a cancel button, an action completing etc). If you were to add resources that need freeing, the second design means that code can go after the loop and be guaranteed to execute. Same applies for functions. Bad: Func Foo($i) If $i < 12 Then Return "Foo" Return "Bar" EndFunc Better: Func Foo($i) Local $ret = "Bar" If $i < 12 Then $ret = "Foo" Return $ret EndFunc You can now add code that will always be executed immediately before returning from the function (like freeing resources). I'm not saying go for it and rewrite all your scripts like this. Trying to can result in larger and more unreadable code. But bear it in mind when you write your code.
    1 point
  12. Welcome to AutoIt! I can see from your script that you've been reading the help files included with AutoIt. I have to applaud you for that It looks to me like you're trying to combine COM usage along with the built in UDF's of AutoIt. Run/tweak this in a script session all of it's own, it should help you figure out what you're trying to do... Because you're just using defaults, you don't need to delare them or indicate them. #include <ie.au3> $o_object = _IECreate("googlewebsite") _IELoadWait($o_object) Feel free to post questions. You're doing good, trust me EDIT: Due to the weirdness of this website, I had to take out the google website in my code snippit. Replace googlewebsite with the FQDN for google's homepage.
    1 point
×
×
  • Create New...