-
Posts
2,005 -
Joined
-
Last visited
-
Days Won
8
Content Type
Forums
Downloads
Forum Articles
Events
Everything posted by Malkey
-
StringRegExp - Matching
Malkey replied to HurleyShanabarger's topic in AutoIt General Help and Support
Try this. #include <Debug.au3> _Main() Func _Main() Local $sgData $sgData &= " aaa := bbb OR" & @CRLF $sgData &= " ccc;" & @CRLF $sgData &= " ddd := eee OR" & @CRLF $sgData &= " fff;" & @CRLF $sgData &= " ggg := hhh OR" & @CRLF $sgData &= " iii;" & @CRLF $sgData &= " jjj := kkk OR" & @CRLF $sgData &= " ccc;" & @CRLF $sSearch = "ccc" ; "fff" ; "iii" ; Local $sMatch = StringRegExpReplace($sgData, "\s+([a-z]{3}).+\s+" & $sSearch & ";(\R)|\R*.*", "\1\2") ConsoleWrite(StringStripWS($sMatch, 2) & @CRLF) ; $STR_STRIPTRAILING (2) = strip trailing white space ; Or Local $aMatch = StringRegExp($sgData, "([a-z]{3}).+\s+" & $sSearch, 3) _DebugArrayDisplay($aMatch) EndFunc ;==>_Main -
Here are two one-liners that will return StringLeft() and StringRight() of all items in a 1D or 2D array. #include <array.au3> ; 1D Aarray Local $aArray[] = ["02", 1234, 23456789, "3456733333333333333333333"] ; Or, 2D Array ;Local $aArray[][] = [["02", 1234, 23456789, "3456733333333333333333333"],["ab", "cdefg","hijklmnop"]] _ArrayDisplay($aArray) $aArray1 = _ArrayFromString(StringRegExpReplace(_ArrayToString($aArray, "!"), "([^!\v]{3}).[^!\v]*", "\1"), "!") ; _ArrayAllItemsStringLeft _ArrayDisplay($aArray1, "Left") $aArray2 = _ArrayFromString(StringRegExpReplace(_ArrayToString($aArray, "!"), "[^!\v]*([^!\v]{3})", "\1"), "!") ; _ArrayAllItemsStringRight _ArrayDisplay($aArray2, "Right")
-
Convert a Number with a comma to a point?
Malkey replied to RyeStick's topic in AutoIt General Help and Support
Using StringReplace() is fine to blanket convert all commas. In case there are any commas in the string that don't need converting, here is a conditional converting method. Local $sOrigString = 'When a comma is between digits, replace with a point e.g. StringReplace("1,62918,", ",", ".")' Local $sString = StringRegExpReplace($sOrigString, "(?<=\d),(?=\d)", ".") Local $iReplacements = @extended MsgBox(4096, "", $iReplacements & " replacement" & _ ; $MB_SYSTEMMODAL is 4096 ($iReplacements = 1 ? " was" : "s were") & _ ; Allow for plural or singular grammatically. " made and the new string is:" & _ @CRLF & @CRLF & $sString) ; The RE Pattern:- ; "(?<=\d)," - (Lookbehind assertions) Match an occurrence of a comma, ",", that is preceded by a digit, "\d", but does not include the digit in the match; and, ; ",(?=\d)" - (Lookahead assertions) Matches a comma followed by a digit, "\d", but does not include the digit in the match. ; So, to match a particular comma to convert, there must be a digit on either side of the comma. ; Returns :- ; 1 replacement was made and the new string is: ; ; When a comma is between digits, replace with a point e.g. StringReplace("1.62918,", ",", ".") ; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^ : Replacement : Comma converted to point. -
This method has more accuracy than a 30 days a month method. #include <Date.au3> Local $sStartDate = "8/4/2021" ; Format is "D/M/YYYY", not US date format. Local $sEndDate = "9/6/2021" ; Format is "D/M/YYYY", not US date format. Local $sStart = _DateConvert($sStartDate, 0) Local $sEnd = _DateConvert($sEndDate, 0) ConsoleWrite("$sStart = " & $sStart & ' in format "YYYY/MM/DD"' & @CRLF) ConsoleWrite("$sEnd = " & $sEnd & ' in format "YYYY/MM/DD"' & @CRLF) Local $iNumMnths = _DateDiff('M', $sStart, $sEnd) ; Number of months between dates. Local $sStartPlusMnths = _DateAdd("M", $iNumMnths, $sStart) ; $sStartDatePlusMnths = Start Date plus number of months between dates. Local $iRemDays = _DateDiff('D', $sStartPlusMnths, $sEnd) ; Number of remaining days. Local $iTotalDays = _DateDiff('D', $sStart, $sEnd) ; Total number of days between dates. ConsoleWrite("(" & $iNumMnths & " month" & ($iNumMnths = 1 ? " " : "s ") & _ $iRemDays & " day" & ($iRemDays = 1 ? ")" : "s)") & " or " & _ $iTotalDays & " days" & @CRLF) ; ============== _DateConvert ================= ; If $US = 1 then format of $Date is in "[M]M/[D]D/YYYY" otherwise, ; If $US = 0 then format of $Date is in "[D]D/[M]M/YYYY" (not US date format) ; The converted Date out has format "YYYY/MM/DD" (for use in _Date...() AutoIt UDFs) ; Func _DateConvert($Date, $US = 1) Local $aTemp = StringSplit($Date, "/") Return StringFormat("%4i/%02i/%02i", $aTemp[3], ($US ? $aTemp[1] : $aTemp[2]), ($US ? $aTemp[2] : $aTemp[1])) EndFunc ;==>_DateConvert #cs ; Returns:- $sStart = 2021/04/08 in format "YYYY/MM/DD" $sEnd = 2021/06/09 in format "YYYY/MM/DD" (2 months 1 day) or 62 days #ce ; Returns:-
-
GUICtrlCreateDate using every quarter hour only
Malkey replied to zuladabef's topic in AutoIt General Help and Support
This example function, _TimeRound(), will round up or down the time to the nearest specified minute in an hour, or, to a fraction of a minute. The default rounding number is 15 minute. #include <Date.au3> ; https://www.autoitscript.com/forum/topic/205759-guictrlcreatedate-using-every-quarter-hour-only/ ConsoleWrite("_TimeRound(""12:07"") equals " & _TimeRound("12:07") & " ; Note: There are no 'return' seconds present." & @CRLF) ConsoleWrite("_TimeRound(""12:8"") equals " & _TimeRound("12:8") & @CRLF) For $i = 0 To 68 * 60 Local $sTimeIncrement = StringRegExpReplace(_DateAdd('s', $i, "2021/04/02 12:00:00"), "(^\H+)\h", "") ConsoleWrite("_TimeRound(""" & $sTimeIncrement & """) equals " & _TimeRound($sTimeIncrement) & @tab) ConsoleWrite("_TimeRound(""" & $sTimeIncrement & """, 15/60) equals " & _TimeRound($sTimeIncrement, 15/60) & @CRLF) Next ; Description - Rounds time (default nearest 15 minutes of the hour, or, closest 15mins to $sTime +/- 7.5 mins). ; Parameters - $sTime - A time string, format in HH:MM[:SS] ; $iRoundedInMins - Rounding $sTime in minutes (default 15mins). For rounding to 15 seconds, have $iRoundedInMins = 15/60 (minutes). ; Returns - Time to nearest 15 mins in format HH:(00|15|30|45)[:00] If there are no "input" seconds present, then there are no "return" seconds present. ; Func _TimeRound($sTime, $iRoundedInMins = 15) Local $sPreDateTime = "2021/01/01 ", $iDecimalplaces = Int(Log(60 * $iRoundedInMins) / Log(10)), $iFactor = (60 * $iRoundedInMins) / (10 ^ $iDecimalplaces) Local $sHrZero = StringRegExpReplace($sTime, "(?<=\:)(.+)(?=$)", "00:00") ; Create Hour:00:00, where 'Hour' is the same hour as in $sTime. Local $bSecsPresent = StringRegExp($sTime, "\d+:\d+:\d+$") ; If secs are present, then $bSecsPresent is positive (True). Local $sDateTime = _DateAdd('s', Round(_DateDiff('s', $sPreDateTime & $sHrZero, $sPreDateTime & $sTime) / $iFactor, -$iDecimalplaces) * $iFactor, $sPreDateTime & $sHrZero) ; Nearest 900secs (15mins). Return StringRegExpReplace($sDateTime, "^\H+\h" & ($bSecsPresent ? "" : "|\:\d+$"), "") EndFunc ;==>_TimeRound #cs ; Returns:- _TimeRound("12:07") equals 12:00 ; Note: There are no 'return' seconds present. _TimeRound("12:8") equals 12:15 ... _TimeRound("13:07:22") equals 13:00:00 _TimeRound("13:07:22", 15/60) equals 13:07:15 _TimeRound("13:07:23") equals 13:00:00 _TimeRound("13:07:23", 15/60) equals 13:07:30 <--- Note: Time increments to 30sec because time's seconds > 22.5secs (15+15/2) or (30-15/2) _TimeRound("13:07:24") equals 13:00:00 _TimeRound("13:07:24", 15/60) equals 13:07:30 _TimeRound("13:07:25") equals 13:00:00 _TimeRound("13:07:25", 15/60) equals 13:07:30 _TimeRound("13:07:26") equals 13:00:00 _TimeRound("13:07:26", 15/60) equals 13:07:30 _TimeRound("13:07:27") equals 13:00:00 _TimeRound("13:07:27", 15/60) equals 13:07:30 _TimeRound("13:07:28") equals 13:00:00 _TimeRound("13:07:28", 15/60) equals 13:07:30 _TimeRound("13:07:29") equals 13:00:00 _TimeRound("13:07:29", 15/60) equals 13:07:30 _TimeRound("13:07:30") equals 13:15:00 _TimeRound("13:07:30", 15/60) equals 13:07:30 <--- Note: Time increments to 15min 00sec because time's min & sec equals 7.5min (15/2) _TimeRound("13:07:31") equals 13:15:00 _TimeRound("13:07:31", 15/60) equals 13:07:30 ... #ce -
_GUICtrlMonthCal select different days
Malkey replied to Blois's topic in AutoIt General Help and Support
I have been playing with this example for about a week, off and on. May this example help someone. #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <Date.au3> #include <Array.au3> ; https://www.autoitscript.com/forum/topic/203556-_guictrlmonthcal-select-different-days/?do=findComment&comment=1462072 Global $g_idMemo Global $iFrmt = "other" ; Format MM/DD/YYYY ; or ; ;Global $iFrmt = "au" ; Format DD/MM/YYYY ; Example() Func Example() Local $idMonthCal GUICreate("Select Dates", 250, 340, -1, 20, BitOR($WS_SIZEBOX, $WS_MINIMIZEBOX, $WS_CAPTION, $WS_SYSMENU)) Local $idMonthCal = GUICtrlCreateMonthCal("", 10, 10, 230, 160, $WS_BORDER) GUICtrlSetResizing(-1, $GUI_DOCKALL) GUICtrlSetTip(-1, "Left mouse click on date to select") $g_idMemo = GUICtrlCreateEdit("", 10, 180, 230, 110) GUICtrlSetFont(-1, 9, 400, 0, "Courier New") GUICtrlSetResizing(-1, $GUI_DOCKTOP + $GUI_DOCKBOTTOM + $GUI_DOCKWIDTH + $GUI_DOCKLEFT) GUICtrlSetTip(-1, "Drag bottom edge of the GUI window is possible") Local $idButSort = GUICtrlCreateButton("Sort (Asc/Desc)", 10, 295, 100, 20) GUICtrlSetResizing(-1, $GUI_DOCKSIZE + $GUI_DOCKBOTTOM + $GUI_DOCKLEFT) GUICtrlSetTip(-1, 'Toggle ascending and descending date sort order.') Local $idButUniq = GUICtrlCreateButton("Unique", 120, 295, 50, 20) GUICtrlSetResizing(-1, $GUI_DOCKSIZE + $GUI_DOCKBOTTOM + $GUI_DOCKLEFT) GUICtrlSetTip(-1, 'Remove duplicate dates.') Local $idButParse = GUICtrlCreateButton("Parse", 180, 295, 50, 20) GUICtrlSetResizing(-1, $GUI_DOCKSIZE + $GUI_DOCKBOTTOM + $GUI_DOCKLEFT) GUICtrlSetTip(-1, "Change a range of dates (if present) to a list of dates" & @CRLF & _ 'eg. "12/08/202014/08/2020" or "12/08/2020 14/08/2020" or "14/08/2020:12/08/2020" or "12/08/2020 to 14/08/2020" will return:-' & @CRLF & _ '12/08/2020' & @CRLF & _ '13/08/2020' & @CRLF & _ '14/08/2020') Local $idButFormat = GUICtrlCreateButton("MM/DD/YYYY", 10, 318, 100, 20) GUICtrlSetResizing(-1, $GUI_DOCKSIZE + $GUI_DOCKBOTTOM + $GUI_DOCKLEFT) GUICtrlSetTip(-1, 'Toggle date format between "DD/MM/YYYY" and "MM/DD/YYYY"') GUISetState(@SW_SHOW) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $idMonthCal ;GUICtrlSetData($g_idMemo, GUICtrlRead($idMonthCal) & @CRLF, 1) GUICtrlSetData($g_idMemo, StringRegExpReplace(GUICtrlRead($idMonthCal), "(\d{4})/(\d{2})/(\d{2})", ($iFrmt = "au" ? "$3/$2/$1" : "$2/$3/$1") & @CRLF), 1) ; YYYY/MM/DD to (DD/MM/YYYY or MM/DD/YYYY) Case $idButSort _Parse() ; Default parameter is "1" to sort. And, will change a range of dates (if present) to a list of dates. Case $idButUniq _Unique() Case $idButParse _Parse(0) ; Parameter "0" will not sort, but will change a range of dates (if present) to a list of dates. Case $idButFormat GUICtrlSetData($idButFormat, ($iFrmt = "au" ? "MM/DD/YYYY" : "DD/MM/YYYY")) $iFrmt = ($iFrmt = "au" ? "other" : "au") GUICtrlSetData($g_idMemo, StringRegExpReplace(GUICtrlRead($g_idMemo), "(\d{2})/(\d{2})/(\d{4})", "$2/$1/$3"), "") EndSwitch WEnd GUIDelete() EndFunc ;==>Example ; Any two dates on the same line separated by any character(s) that are not a digit or a "/", ; will be replaced with a sequencial range of dates between the two dates. ; eg. "12/08/202014/08/2020" or "12/08/2020 15/08/2020" or "15/08/2020:12/08/2020" or "12/08/2020 to 15/08/2020" will return:- ; 12/08/2020 ; 13/08/2020 ; 14/08/2020 ; 15/08/2020 Func _Parse($iSort = 1) Local $Y, $M, $D, $iStart, $iEnd, $iStep = 1, $iIndex = 0 Local $aRet[1000][2] Local Static $iDesc = 1 If $iSort Then $iDesc = Not $iDesc ; Toggle ascending and descending date sort order. Local $a = StringRegExp(GUICtrlRead($g_idMemo), "[\d\V]+", 3) For $j = 0 To UBound($a) - 1 $aTemp = StringRegExp($a[$j], "[^\D]{2,4}", 3) ; Note: "[^\D]{2,4}" is faster than "\d{2,4}" If UBound($aTemp) = 6 Then ; Expand date range to date list. In 2D array, 1st column is sorting date,"y/m/d". 2nd column is formatted date, either "d/m/y" or "m/d/y"." $iStart = _DateToDayValue($aTemp[2], ($iFrmt = "au" ? $aTemp[1] : $aTemp[0]), ($iFrmt = "au" ? $aTemp[0] : $aTemp[1])) If @error Then ContinueLoop (Abs(MsgBox(16, "Error", "Error iInvalid range start date", 4))) ; Check valid date. $iEnd = _DateToDayValue($aTemp[2], ($iFrmt = "au" ? $aTemp[4] : $aTemp[3]), ($iFrmt = "au" ? $aTemp[3] : $aTemp[4])) If @error Then ContinueLoop (Abs(MsgBox(16, "Error", "Error invalid range end date", 4))) ; Check valid date. If $iStart > $iEnd Then $iStep = -1 For $i = $iStart To $iEnd Step $iStep _DayValueToDate($i, $Y, $M, $D) $aRet[$iIndex][0] = $Y & "/" & $M & "/" & $D $aRet[$iIndex][1] = ($iFrmt = "au" ? $D & "/" & $M : $M & "/" & $D) & "/" & $Y $iIndex += 1 Next Else If _DateIsValid($aTemp[2] & "/" & ($iFrmt = "au" ? $aTemp[1] & "/" & $aTemp[0] : $aTemp[0] & "/" & $aTemp[1])) = 0 Then _ ; Check valid date. ContinueLoop (Abs(MsgBox(16, "Error", "Invalid date: " & $a[$j], 4))) ; Check valid date. $a[$j] = StringRegExpReplace($a[$j], "([^\d/\v]+)", "") $aRet[$iIndex][0] = StringRegExpReplace($a[$j], "(\d{2})/(\d{2})/(\d{4})", ($iFrmt = "au" ? "$3/$2/$1" : "$3/$1/$2")) ; DD/MM/YYYY to YYYY/MM/DD for sorting on YYYY/MM/DD. $aRet[$iIndex][1] = $a[$j] $iIndex += 1 EndIf Next ReDim $aRet[$iIndex][2] If $iSort Then _ArraySort($aRet, $iDesc) _ArrayColDelete($aRet, 0) GUICtrlSetData($g_idMemo, _ArrayToString($aRet, @CRLF) & @CRLF) EndFunc ;==>_Parse Func _Unique() Local $a = StringRegExp(GUICtrlRead($g_idMemo), "[\d/]+", 3) $aU = _ArrayUnique($a) _ArrayDelete($aU, 0) GUICtrlSetData($g_idMemo, _ArrayToString($aU, @CRLF) & @CRLF) EndFunc ;==>_Unique -
delete sections not from end of ini then sort ?
Malkey replied to AlienStar's topic in AutoIt General Help and Support
This script sequentially renames all remaining sections of an .ini file after deleting one of the sections. Local $sFileName = "TestIni.ini" ; --------- Create a test .ini file ---------- If FileExists($sFileName) Then FileDelete($sFileName) FileWrite($sFileName, StringRegExpReplace(FileRead(@ScriptFullPath), "(?s)(^.*#cs\R|\R#ce.*$)", "")) ; -------- End of Create .ini file ----------- _DeleteSect($sFileName, 2) Run('Notepad.exe "' & $sFileName & '"') Func _DeleteSect($sFilePath, $Sect) IniDelete($sFilePath, $Sect) $aArray = IniReadSectionNames($sFilePath) For $i = 1 To $aArray[0] IniRenameSection($sFilePath, $aArray[$i], $i) Next ; Format and save .ini file Local $sFileContents = FileRead($sFilePath) FileDelete($sFilePath) FileWrite($sFilePath, StringStripWS(StringRegExpReplace($sFileContents, "\s*(\[\d+\])", @CRLF & @CRLF & "$1"), 3) & @CRLF) ; (1) = strip leading white spaces, and, (2) = strip trailing white spaces EndFunc ;==>_DeleteSect #cs [1] s = 1 m = 4 [2] h = 2 j = 7 k = 9 l = 22 r = 99 [3] a = 0 l = 111 y = 88 w = 90 [4] d =0 #ce -
Detect sorting order in a folder
Malkey replied to spuuunit's topic in AutoIt General Help and Support
Please notice in the opening post above, the image has the "Storlek" column circled in red. And just above the "k" in "Storlek" in the column header is an up-arrow character. This "up-arrow" character means that this column is sorted in ascending order. Click on the column header again, and the down-arrow character means the column is sorted in descending order. -
[SOLVED] combine Arrays respectively
Malkey replied to Colduction's topic in AutoIt General Help and Support
Another example for your amusement. #include <Array.au3> Local $aFiles[][2] = [["1.txt"], ["2.txt"], ["3.txt"]] Local $iMNCols = 0 ; Maximum Number of Columns (MNC) Local $sStr = "" ; This string will be converted into 2D array. ; Files to abbreviated strings For $i = 0 To UBound($aFiles) - 1 $aFiles[$i][1] = StringRegExpReplace(FileRead($aFiles[$i][0]), '(?:=+\R)?(\w+)[^=]+(?:=+\R?)?', "\1|") ; De-construct file format. StringReplace($aFiles[$i][1], "|", "|") $iExt = @extended ; @extended contains number of replacements in StringReplace() function. $iMNCols = ($iExt > $iMNCols ? $iExt : $iMNCols) ; Get Maximum Number of Columns Next ; String to 2D array For $i = 0 To UBound($aFiles) - 1 $sStr &= StringTrimRight($aFiles[$i][1], 1) & @CRLF ; StringTrimRight() removes trailing "|" . Next Local $aData[0][$iMNCols] _ArrayAdd($aData, StringStripWS($sStr, 2)) ; Append the three rows into the $aData array with $iMNCols number of columns. _ArrayTranspose($aData) ; Now array has $iMNCols number of rows, with three columns. ;_ArrayDisplay($aData, "", "", 0, Default, "1.txt|2.txt|3.txt") ; 2D array to file re-constructed string with numbers Local $sRet = "==" & @CRLF Local $r = 1 ; Counter For $j = 0 To UBound($aData) - 1 For $i = 0 To UBound($aData, 2) - 1 If $aData[$j][$i] <> "" Then $sRet &= $r & $aData[$j][$i] & @CRLF & $r & $aData[$j][$i] & @CRLF & "==" & @CRLF EndIf Next $r += 1 ; Increment $r Next ConsoleWrite(StringStripWS($sRet, 2) & @CRLF) ; Parameter flag (2) = strip trailing white spaces (@CRLF). Used the following files with the resultant output. 1.txt: == aa aa == aa aa == 2.txt: == bb bb == bb bb == bb bb == bb bb == bb bb == bb bb == 3.txt: == cc cc == cc cc == cc cc == cc cc == OUTPUT.txt: == 1aa 1aa == 1bb 1bb == 1cc 1cc == 2aa 2aa == 2bb 2bb == 2cc 2cc == 3bb 3bb == 3cc 3cc == 4bb 4bb == 4cc 4cc == 5bb 5bb == 6bb 6bb == -
Help conver string from Uni to Character
Malkey replied to ngocthang26's topic in AutoIt General Help and Support
Another example. Local $sOrig = '?q=Kim+lo%E1%BA%A1i' MsgBox(0, "Conversion : ", "Original text: " & $sOrig & @CRLF & _ "Converted: " & BinaryToString(Binary(Execute('"' & StringRegExpReplace($sOrig, "(?i)%([0-9a-f]{2})", '" & Chr(0x${1})&"') & '"')), 4)) ; Returns:- ?q=Kim+loại -
Another example. #cs ; --------- Test Data ---------- Material B7E671143D244B ==================================== TEXT 2F3139D816C34D 1 TEXT B6A968EF2505A2 1 TEXT 35206697A04F91 1 TEXT EB485AF490D83D 1 TEXT 0DAB42294BD9B3 1 TEXT 3D6525BEE360E1 0 Material D6906B886B06E3 ==================================== TEXT 0CCECCCCFB62AE 1 TEXT 1E14CB29AB43F0 1 TEXT FB7F0DCE9B5950 1 But I have a new text file now the lines of which now are start with 0:, 1: and so on: sm_0 --------------- 0: dummy_gray 1: c_com_socksa_mt 2: c_com_socksa_tn 3: dummy_white 4: default_z 5: dummy_nmap 6: --- 7: --- sm_1 --------------- 0: c_com_prisoner_shoes_di 1: c_com_prisoner_shoes_mt 2: c_com_prisoner_shoes_tn 3: dummy_white 4: default_z 5: c_com_leatherb_rt 6: --- 7: --- #ce ; ----- End of Test Data ---------- #include <Array.au3> $_aName = StringRegExpReplace(FileRead(@ScriptFullPath), '(?is)(^.*#cs[^\R]*\R)|(\R#ce.*$)', "") ; Extract test data from script. ;ConsoleWrite( $_aName& @CRLF) $_aName = StringRegExp($_aName, '(?m)^(?:TEXT|\d+:\h)(.*$)', 3) ; Extract required text from test data. (Lines starting with "TEXT ", or, digits with a ": ". ConsoleWrite(_ArrayToString($_aName, @CRLF) & @CRLF) _ArrayDisplay($_aName)
-
Late last year I came across the same problem. These are the two methods I found that worked. #include <Array.au3> Local $Paths = 'C:\Users\' & @CRLF & _ 'C:\EEC (1)\someother file' & @CRLF & _ 'C:\MSfree\' & @CRLF & _ 'C:\EEC (1)\' Local $sSearch = "C:\EEC" ; --------------------- Method 1 ---------------------------------- ; Check if "\E" is in $sSearch. If "\E" is present, replace "\E" with "\E\\E\Q", because of the "\Q" & $sSearch & "\E" in RE pattern. Local $sSearchA = (StringInStr($sSearch, "\E") ? StringReplace($sSearch, "\E", "\E\\E\Q") : $sSearch) ;ConsoleWrite("\Q" & $sSearchA & "\E" & @CRLF) Local $a = StringRegExp($Paths, "(\Q" & $sSearchA & "\E.*)", 3) _ArraySort($a) ;_ArrayDisplay($a) If UBound($a) > 1 Then For $i = 1 To UBound($a) - 1 ; Keep $a1[0] $sSearchB = (StringInStr($a[$i], "\E") ? StringReplace($a[$i], "\E", "\E\\E\Q") : $a[$i]) ConsoleWrite("\Q" & $sSearchB & "\E" & @CRLF) Local $sOutput = StringRegExpReplace($Paths, "(\Q" & $sSearchB & "\E\R?)", "") Next Else Local $sOutput = StringRegExpReplace($Paths, "(\Q" & $sSearchA & "\E.*\R?)", "") EndIf MsgBox(0, "\Q...\E", $sOutput) ; Or ; --------------------- Method 2 ---------------------------------- ; Beware "(1)", where (n) tests whether the capturing group with absolute number n matched. $sSearch = StringRegExpReplace($sSearch, "([\\()\.^$|\[\]{}*+?#])", "\\$1") ;ConsoleWrite($sSearch & @CRLF) Local $a1 = StringRegExp($Paths, "(" & $sSearch & ".*)", 3) _ArraySort($a1) ;_ArrayDisplay($a1) If UBound($a1) > 1 Then For $i = 1 To UBound($a1) - 1 ; Keep $a1[0] $sSearchC = StringRegExpReplace($a1[$i], "([\\()\.^$|\[\]{}*+?#])", "\\$1") ConsoleWrite($sSearchC & @CRLF) Local $sOutput = StringRegExpReplace($Paths, "(" & $sSearchC & "\R?)", "") Next Else Local $sOutput = StringRegExpReplace($Paths, "(" & $sSearch & ".*\R?)", "") EndIf MsgBox(0, "\\,(,)", $sOutput) #cs ; Both methods return:- C:\Users\ C:\MSfree\ C:\EEC (1)\ #ce
-
Here is another method in this example. #include <Array.au3> Local $aData[][] = [ _ [""], _ [""], _ [1, "x", "w", "w", 5], _ [1, "x", "w", "w", 5], _ ["w", "x", 3, 4, "x"], _ [""]] Local $sAppendNewRow = "2|0|1|1" Local $sAppendNBRows = "2" ; String to append to Non-Blank rows. ReDim $aData[UBound($aData, 1) + 1][UBound($aData, 2) + 1] ; Re-dimension array with one extra row and one extra column. ; -------- Add last row -------- Local $aLastRow = StringSplit($sAppendNewRow, "|", 2) ; $STR_NOCOUNT (2) For $i = 0 To UBound($aLastRow) - 1 $aData[UBound($aData) - 1][$i] = $aLastRow[$i] Next ; ---Append $sAppendNBRows value to the end of each non-blank row. --- For $i = 0 To UBound($aData) - 1 For $j = UBound($aData, 2) - 2 To 0 Step -1 If $aData[$i][$j] <> "" Then $aData[$i][$j + 1] = $sAppendNBRows ; Add $sAppendNBRows to previous column, "$j + 1" in row, "$i". ExitLoop EndIf Next Next _ArrayDisplay($aData)
-
Your new question in this thread probably would have got more attention by creating a new thread. Here is one example of updating an array - the first method I thought of. There are surely other methods. #include <Array.au3> Local $aData[][] = [ _ [""], _ [""], _ [1, "x", "w", "w", 5], _ [1, "x", "w", "w", 5], _ ["w", "x", 3, 4, "x"], _ [""]] Local $sAppendNewRow = "2|0|1|1" Local $sAppendNBRows = "2" ; String to append to Non-Blank rows. ; --- Append new last row.--- _ArrayAdd($aData, $sAppendNewRow) ; --- Append new column ---- ReDim $aData[UBound($aData, 1)][UBound($aData, 2) + 1] ; Re-dimension Array with one extra column. ; ---Append $sAppendNBRows value to the end of each non-blank row. --- Local $sStrData = _ArrayToString($aData) ; Assign contents of array to string for later RE string manipulation. ;ConsoleWrite($sStrData & @CRLF) ; See workings. Local $aData[0][UBound($aData, 2)] ; Re-assign the array, $aData, with zero rows. _ArrayAdd($aData, StringRegExpReplace($sStrData, "(?m)([^\|\v]+\|)(\|{0," & (UBound($aData, 2) - 1) & "})$", "${1}" & $sAppendNBRows & "${2}")) ; Append a "2" to all non-blank rows. ;ConsoleWrite(StringRegExpReplace($sStrData, "(?m)([^\|\v]+\|)(\|{0," & (UBound($aData, 2) - 1) & "})$", "${1}2${2}") & @CRLF) ; See workings. _ArrayDisplay($aData)
-
And another example. #include <Array.au3> $g1 = "W|2|3|W|X" $g2 = "1|X|W|4|X" $g3 = "1|2|W|4|X" $g4 = "" $g5 = "W|2|3|4|X" $g6 = "" Local $arr[0][5] _ArrayAdd($arr, $g1 & @CRLF & $g2 & @CRLF & $g3 & @CRLF & $g4 & @CRLF & $g5 & @CRLF & $g6) _ArrayDisplay($arr)
-
Help with removing X line in text
Malkey replied to User751139's topic in AutoIt General Help and Support
Looking at the example in the opening post, conventionally, declaring a variable isn't normally placed inbetween the "#include" statements. Included are two more one-liners. Local $s = _ "Line 1" & @CRLF & _ "Line 2" & @CRLF & _ "Line 3" & @CRLF & _ "Line 4" & @CRLF & _ "Line 5 <-- need to remove 4th last line" & @CRLF & _ "Line 6" & @CRLF & _ "Line 7" & @CRLF & _ "Line 8 <-- removes last line" ConsoleWrite("--- jchd ---( Trailing @CRLF or clears last line)" & @CRLF) $s1 = StringRegExpReplace($s, "(?ms)(.*)(^\N+\R)((?:^\N+\R){2})(^\N+\Z)", "$1$3") ConsoleWrite($s1 & @LF) ConsoleWrite("--- mikell --- (Trailing @CRLF + Removes 4th line from beginning)" & @CRLF) $s2 = StringRegExpReplace($s, "(?m)(^\N+\R){3}\K(?1)|(^\N+\R?\Z)", "") ConsoleWrite($s2 & @LF) ConsoleWrite("------ RE ---------" & @CRLF) $s3 = StringRegExpReplace($s, "(\N+\R)((?1)\N+)(\R\N+\R*$)", "$2") ConsoleWrite($s3 & @LF) ConsoleWrite("--- StringMid ----" & @CRLF) $s4 = StringMid($s, 1, StringInStr($s, @CRLF, 0, -4) - 1) & StringMid($s, StringInStr($s, @CRLF, 0, -3), StringInStr($s, @CRLF, 0, -1) - StringInStr($s, @CRLF, 0, -3)) ConsoleWrite($s4 & @LF) #cs ; Returns:- --- jchd ---( Trailing @CRLF or clears last line) Line 1 Line 2 Line 3 Line 4 Line 6 Line 7 --- mikell --- (Trailing @CRLF + Removes 4th line from beginning) Line 1 Line 2 Line 3 Line 5 <-- need to remove 4th last line Line 6 Line 7 ------ RE --------- Line 1 Line 2 Line 3 Line 4 Line 6 Line 7 --- StringMid ---- Line 1 Line 2 Line 3 Line 4 Line 6 Line 7 #ce -
Random shuffle Loop help - (Moved)
Malkey replied to Roko's topic in AutoIt General Help and Support
Here is another example that may help. #include <Array.au3> HotKeySet("{ESC}", "Terminate") Local $iInc = 60, $iRandYMin = $iInc, $iRandYMax = $iRandYMin + $iInc Local $aY[9] = [8], $aX[UBound($aY)] = [UBound($aY) - 1] ; Instead of 2 - 1D arrays, could use 1 - 2D array to hold X and Y coordinates. While 1 Sleep(10) $iRandYMin = $iInc $iRandYMax = $iRandYMin + $iInc ConsoleWrite('"Y" Range:-' & @CRLF) For $i = 1 To 8 $aX[$i] = Random(930, 1050, 1) ; Random 8 "X" coordinate values. $aY[$i] = Random($iRandYMin, $iRandYMax, 1) ; Random 8 "Y" coordinate values. ConsoleWrite("Random(" & $iRandYMin & ", " & $iRandYMax & ", 1)" & @CRLF) $iRandYMin = $iRandYMax $iRandYMax = $iRandYMin + $iInc Next _ArrayShuffle($aY, 1) ; Randomize the random "Y" values of each area where the random mouse clicks will be made. ;_ArrayDisplay($aY) ConsoleWrite("MouseClick (X, Y):-" & @CRLF) For $i = 1 To 8 MouseClick("left", $aX[$i], $aY[$i], 1, 0) ConsoleWrite($aX[$i] & ", " & $aY[$i] & @CRLF) Next WEnd Func Terminate() Exit EndFunc ;==>Terminate #cs ; Output example:- "Y" Range:- Random(60, 120, 1) Random(120, 180, 1) Random(180, 240, 1) Random(240, 300, 1) Random(300, 360, 1) Random(360, 420, 1) Random(420, 480, 1) Random(480, 540, 1) MouseClick (X, Y):- 1020, 317 1013, 254 1010, 442 962, 71 963, 505 1005, 416 939, 140 1025, 209 #ce -
Copy text before empty line and set it into clipboard
Malkey replied to Fadi's topic in AutoIt General Help and Support
Maybe this example. ; https://books.google.com.au/books?id=IyhVAAAAYAAJ&dq=two+CRLF%27s+end+a+paragraph&focus=searchwithinvolume&q=two+CRLF Local $sExampleText = _ "Viscom1 has developed its X-ray systems to the point where X-ray inspection can be used for large, " & _ "heavy objects automatically and inline during production." & @CRLF & @CRLF & _ "This article was originally published in the March 2020 edition of U.S. Tech." ConsoleWrite($sExampleText & @CRLF & "-----------------------------------------------------------" & @CRLF) ClipPut(StringRegExpReplace($sExampleText, "\R{2,}.+$", "")) ConsoleWrite(ClipGet() & @CRLF) -
And logical operator not working as expected
Malkey replied to D3fr0s7's topic in AutoIt General Help and Support
This is supposed to show that (False = False = True) is True. ; https://www.autoitscript.com/forum/topic/202653-and-logical-operator-not-working-as-expected/?do=findComment&comment=1454621 ; Zero is false, not zero is true. ConsoleWrite( "Not 0 = " & Not 0 & @CRLF) ; -> Not 0 = True Local $AColor = 1 Local $BColor = 1 ConsoleWrite( ($AColor = $BColor = 2) & @CRLF) ; -> True, where, $AColor = $BColor is true, 2 is "not 0" is true. So, true = true is true. ConsoleWrite((($AColor = $BColor) = 3) & @CRLF) ; -> True ConsoleWrite( ($AColor = $BColor = True) & @CRLF) ; -> True Local $BColor = 2 ConsoleWrite( ($AColor = $BColor = 0) & @CRLF) ; -> True, where, $AColor = $BColor is false, 0 is false. So, (false = false) is true. * ConsoleWrite((($AColor = $BColor) = 0) & @CRLF) ; -> True ConsoleWrite( ($AColor = $BColor = False) & @CRLF) ; -> True Local $x = True ConsoleWrite( ($x = $x = $x) & @CRLF) ; -> True, where, $x = $x is true, $x is true. So, true = true is true. ConsoleWrite( (4 = 4 = 4) & @CRLF) ; -> True, where, 4 = 4 is true, 4 is "not 0" is true. So, true = true is true. ; * Show truth table where (False = False) is True. ConsoleWrite(@CRLF & "--------------- Truth Tables -----------------" & @CRLF) ConsoleWrite("A " & @TAB & "B " & @TAB & "A = B (Same Truth table as Ex-NOR)" & @CRLF) For $i = 1 To 4 ConsoleWrite((Mod($i, 2) = 1) & @TAB & ($i <= 2) & @TAB & (Mod($i, 2) = 1 = ($i <= 2)) & @CRLF) Next ; Ref: https://www.electronics-tutorials.ws/logic/logic_8.html ConsoleWrite(@CRLF & "A " & @TAB & "B " & @TAB & "A Ex-NOR B (Exclusive-NOR made from 5 - NAND's, Not BitAND's)" & @CRLF) For $i = 1 To 4 $a = (Mod($i, 2) = 1) $b = ($i <= 2) $ExNOR = (Not (BitAND(Not (BitAND($a, $b)), Not (BitAND(Not (BitAND($a, $a)), Not (BitAND($b, $b))))))) ConsoleWrite($a & @TAB & $b & @TAB & $ExNOR & @CRLF) Next ConsoleWrite(@CRLF & "A " & @TAB & "B " & @TAB & "A And B " & @CRLF) For $i = 1 To 4 ConsoleWrite((Mod($i, 2) = 1) & @TAB & ($i <= 2) & @TAB & (BitAND((Mod($i, 2) = 1), ($i <= 2)) = 1) & @CRLF) Next #cs ; Output:- --------------- Truth Tables ----------------- A B A = B (Same Truth table as Ex-NOR) True True True False True False True False False False False True A B A Ex-NOR B (Exclusive-NOR made from 5 - NAND's, Not BitAND's) True True True False True False True False False False False True A B A And B True True True False True False True False False False False False #ce -
A _FileReadToArray() method. #include <Array.au3> #include <File.au3> Local $sFileName = "TestData1.csv" _CreateTestCvsFile($sFileName) Local $sNewFileName = "TestData2.csv" Local $sColReplace = "Item 3", $sReplace = "Home" _SaveBaseSend($sFileName, $sNewFileName, $sColReplace, $sReplace) ShellExecute($sNewFileName) ;FileDelete($sNewFileName) ; Clean up ; The column named in $sColReplace will have its entries replaced with $sReplace in each row, if ; the column entries from 3rd column to the end of the row are all zero. ; The first and second columns may contain any data. ; Func _SaveBaseSend($sFile, $sNewFile, $sColReplace, $sReplace) Local $aArray[0][0] _FileReadToArray($sFile, $aArray, 0, ",") ;_ArrayDisplay($aArray) Local $iColReplaceNum, $bRow = 1 For $i = 0 To UBound($aArray) - 1 For $j = 0 To UBound($aArray, 2) - 1 If $i = 0 Then ; The first column header row. ; In column header names (row 0) find column number. If $aArray[0][$j] == $sColReplace Then $iColReplaceNum = $j ExitLoop 1 ; Once the header name is found, exit that first row. EndIf $bRow = 0 ;ConsoleWrite($i & " " & $j & " " & $aArray[$i][$j] & " " & $bRow & @CRLF) Else ; All rows except first row. If $j > 1 And $aArray[$i][$j] <> 0 Then ; "$j > 1" will only match columns 3 (or, array index 2, zero based) to the end of the row. $bRow = 0 ExitLoop 1 ; Once one non-zero entry appears in a row, the replacement will not happen, so exit row. EndIf EndIf ;ConsoleWrite($i & " > " & $j & " " & $aArray[$i][$j] & " " & $bRow & @CRLF) Next If $bRow Then $aArray[$i][$iColReplaceNum] = $sReplace $bRow = 1 Next ;_ArrayDisplay($aArray) _FileWriteFromArray($sNewFile, $aArray, Default, Default, ",") EndFunc ;==>_SaveBaseSend ; --------------- Create test CSV file --------------- Func _CreateTestCvsFile($sFileName) If FileExists($sFileName) Then FileDelete($sFileName) FileWrite($sFileName, _ "Item 1,Item 2,Item 3,Item 4,Item 5,Item 6,Item 7,Item 8,Item 9,Item 10" & @CRLF & _ "21,22,0,0,0,0,0,0,0,0" & @CRLF & _ ; Replacement will happen here "31,32,0,0,0,0,0,38,0,0" & @CRLF & _ "41,42,0,0,0,0,0,0,0,0" & @CRLF & _ ; Replacement will happen here "51,52,53,54,55,56,57,58,59,60" & @CRLF & _ "61,62,63,64,65,66,67,68,69,70" & @CRLF & _ "71,72,0,74,75,76,77,78,79,80" & @CRLF & _ "81,82,0,84,85,86,87,88,89,90" & @CRLF & _ "91,92,0,0,0,0,0,0,0,0" & @CRLF & _ ; Replacement will happen here "101,102,0,104,105,106,107,108,109,110") EndFunc ;==>_CreateTestCvsFile
-
How to find dissimilar dictionary?
Malkey replied to youtuber's topic in AutoIt General Help and Support
This appears to work. #include <file.au3> Local $findUniqueDictionary[5] = ['autoit', 'autoit', 'script', 'script', 'forum'] Local $sFileIn = "TestIn.txt", $sFileOut = "TestOut.txt" Local $aRecords, $sOut = "" If FileExists($sFileIn) Then FileDelete($sFileIn) _FileWriteFromArray($sFileIn, $findUniqueDictionary, Default, Default, @CRLF) ; https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/dictionary-object Local $oDict = ObjCreate('Scripting.Dictionary') $oDict.CompareMode = 1 ; 1 - Performs a textual comparison. If Not _FileReadToArray($sFileIn, $aRecords) Then MsgBox(4096, "Error", " Error reading file") Exit EndIf For $x = 1 To $aRecords[0] If $oDict.Exists($aRecords[$x]) Then $oDict.Item($aRecords[$x]) += 1 ; Increment record's item value. Else $oDict.Add($aRecords[$x], 1) ; Add new record EndIf Next ; $sOut only contains those records who's item is "1" only. Meaning the record in the file appears only once. For $x = 1 To $aRecords[0] If $oDict.item($aRecords[$x]) = 1 Then $sOut &= $aRecords[$x] & @CRLF Next If FileExists($sFileOut) Then FileDelete($sFileOut) FileWrite($sFileOut, $sOut) ShellExecute($sFileOut) Sleep(3000) ; Tidy files FileDelete($sFileIn) FileDelete($sFileOut) -
Modifying all values in an array
Malkey replied to mertly8's topic in AutoIt General Help and Support
And another example. #include <Array.au3> Local $i[2] = [800, 600] ;MouseGetPos() _AA($i, "+ 5") ; Add 4 to all of arrays elements. _ArrayDisplay($i) Local $a[2][2] = [[0, 1], [10, 20]] _AA($a, "*4") ; multiply all array's elements by 4 _ArrayDisplay($a) ; --------- All of Array's elements ----------- ; The array may be 1D or 2D. ;Any mathematical operator can be used in $sOp parameter. Func _AA(ByRef $aArray, $sOp) If IsArray($aArray) Then If UBound($aArray, 0) = 2 Then For $i = 0 To UBound($aArray) - 1 For $j = 0 To UBound($aArray, 2) - 1 $aArray[$i][$j] = Execute($aArray[$i][$j] & $sOp) Next Next Else For $j = 0 To UBound($aArray) - 1 $aArray[$j] = Execute($aArray[$j] & $sOp) Next EndIf EndIf EndFunc ;==>_AA -
Using Subz's test data, this method returns the same result as Subz's example. #include <array.au3> Local $a_inArr[][2] = [ _ ["2020-03-11", "122589268273"], _ ["2020-03-12", "6901211441"], _ ["2020-03-13", "6934371045"], _ ["2020-03-11", "6428227853"], _ ["2020-03-16", "7358355171"], _ ["2020-03-17", "7113079607"], _ ["2020-03-18", "6907515243"], _ ["2020-03-11", "122589268273"], _ ["2020-03-12", "6901211441"], _ ["2020-03-13", "6934371045"], _ ["2020-03-11", "6428227853"], _ ["2020-03-16", "7358355171"], _ ["2020-03-17", "7113079607"], _ ["2020-03-18", "6907515243"]] _ArraySort($a_inArr) For $i = UBound($a_inArr) - 1 To 1 Step -1 If $a_inArr[$i][0] == $a_inArr[$i - 1][0] Then $a_inArr[$i - 1][1] = $a_inArr[$i - 1][1] + $a_inArr[$i][1] _ArrayDelete($a_inArr, $i) Else $a_inArr[$i][1] = Round(($a_inArr[$i][1] / 2 ^ 30), 2) ; 1 GB = 2^30 bytes EndIf Next $a_inArr[0][1] = Round(($a_inArr[0][1] / 2 ^ 30), 2) ;ConsoleWrite(_ArrayToString($a_inArr) & @CRLF) _ArrayDisplay($a_inArr, "Outbound Array", Default, Default, Default, "Date|GB")