ricky Posted March 19, 2014 Share Posted March 19, 2014 Hello, it's possible to do a code more easy dans this ? if $test = 1 then $test = 0 Elseif $test = 0 Then $test = 1 Endif With StringRegExpReplace or StringRegExp??? Thanks for your help Link to comment Share on other sites More sharing options...
water Posted March 19, 2014 Share Posted March 19, 2014 Possible solution: $iNumber = 0 $iNumber = Int(Not $iNumber) ConsoleWrite($iNumber & @LF) 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 More sharing options...
water Posted March 19, 2014 Share Posted March 19, 2014 Or if you run 3.3.10.x you could use the ternary operator: $iNumber = 0 $iNumber = ($iNumber = 0) ? (1) : (0) ConsoleWrite($iNumber & @LF) 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 More sharing options...
Gianni Posted March 19, 2014 Share Posted March 19, 2014 also $test = 1 * ($test = 0) Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
jdelaney Posted March 19, 2014 Share Posted March 19, 2014 (edited) If it's only binary data, you can always just $test = True $test = Not $test ConsoleWrite($test & @CRLF) How many more ways can we skin this cat Edited March 19, 2014 by jdelaney IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window. Link to comment Share on other sites More sharing options...
Gianni Posted March 19, 2014 Share Posted March 19, 2014 If it's only binary data, you can always just $test = True $test = Not $test ConsoleWrite($test & @CRLF) How many more ways can we skin this cat if True / False is allowed instead of 0 / 1 then also this could do $test = $test = 0 Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
UEZ Posted March 19, 2014 Share Posted March 19, 2014 Instead of 0 / 1 or False / True you can use -1 / 1. Just multiply the value with -1 and invert the value.Br,UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
water Posted March 19, 2014 Share Posted March 19, 2014 if True / False is allowed instead of 0 / 1 then also this could do $test = $test = 0 Or even $test = Not $test 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 More sharing options...
PhilHibbs Posted March 19, 2014 Share Posted March 19, 2014 (edited) $test = 1 - $testOr, did you want to toggle all the zeroes and ones in a string? So "I have 101 things to do" would become "I have 010 things to do"? Edited March 19, 2014 by PhilHibbs Link to comment Share on other sites More sharing options...
jchd Posted March 19, 2014 Share Posted March 19, 2014 (edited) Yet another skin: Local $test = Random(0, 1, 1) ConsoleWrite($test & " --> " & Mod($test + 1, 2) & @LF) Edited March 19, 2014 by jchd This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
Malkey Posted March 19, 2014 Share Posted March 19, 2014 Don't cha love it. Local $test1 = Random(0, 1, 1) MsgBox(0, "Results 1", "Original : " & $test1 & @CRLF & @CRLF & "Invert 0|1: " & _Invert0_1($test1)) Local $test2 = "101" MsgBox(0, "Results 2", "Original : " & $test2 & @CRLF & @CRLF & "Invert 0|1: " & _Invert0_1($test2)) Local $test3 = "I have 101 things to do (10)." MsgBox(0, "Results 3", "Original : " & $test3 & @CRLF & @CRLF & "Invert 0|1: " & _Invert0_1($test3)) Func _Invert0_1($In) Return Execute(StringRegExpReplace($In, "([^10]*)([10])([^10]*)", "'${1}' & Number( ${2} = 0 ) & '${3}' & ") & '""') EndFunc ;==>_Invert0_1 PhilHibbs 1 Link to comment Share on other sites More sharing options...
PhilHibbs Posted March 20, 2014 Share Posted March 20, 2014 (edited) Don't cha love it.Yes, I rather do! If I might venture a small improvement:Return Execute(StringRegExpReplace($In, "([^10]*)([10])([^10]*)", "'${1}' & (${2}?'0':'1') & '${3}' & ") & '""') Edited March 20, 2014 by PhilHibbs Link to comment Share on other sites More sharing options...
ricky Posted March 20, 2014 Author Share Posted March 20, 2014 Hello, thanks for your help. Link to comment Share on other sites More sharing options...
Malkey Posted March 20, 2014 Share Posted March 20, 2014 PhilHibbs Looking at your first post, post #9, I thought this change would work nicely. Return Execute(StringRegExpReplace($In, "([^10]*)([10])([^10]*)", "'${1}' & ( 1 - ${2} ) & '${3}' & ") & '""') Link to comment Share on other sites More sharing options...
PhilHibbs Posted March 20, 2014 Share Posted March 20, 2014 (edited) And, a generalised version to swap any two characters, plus a compatability layer for legacy code using _Invert0_1:Local $test1 = Random(0, 1, 1) MsgBox(0, "Results 1", "Original : " & $test1 & @CRLF & @CRLF & "Invert 0|1: " & _Invert0_1($test1)) Local $test2 = "101" MsgBox(0, "Results 2", "Original : " & $test2 & @CRLF & @CRLF & "Invert 0|1: " & _Invert0_1($test2)) Local $test3 = "I have 101 things to do (10)." MsgBox(0, "Results 3", "Original : " & $test3 & @CRLF & @CRLF & "Invert 0|1: " & _InvertX_Y($test3,"0","1")) Local $test4 = "the quick brown fox jumps over the lazy dog, jackdaws love my big sphynx of quartz" MsgBox(0, "Results 4", "Original : " & $test4 & @CRLF & @CRLF & "Invert 0|1: " & _InvertX_Y($test4,"p","q")) Func _InvertX_Y($In,$X,$Y) Return Execute(StringRegExpReplace($In, "([^"&$X&$Y&"]*)(["&$X&$Y&"])([^"&$X&$Y&"]*)", "'${1}' & ('${2}'='"&$X&"'?'"&$Y&"':'"&$X&"') & '${3}' & ") & '""') EndFunc ;==>_Invert_XY Func _Invert0_1($In) Return _InvertX_Y($In,"0","1") EndFunc ;==>_Invert0_1I suppose for convention's sake it should be called something beginning with _String. Edited March 20, 2014 by PhilHibbs Link to comment Share on other sites More sharing options...
PhilHibbs Posted March 20, 2014 Share Posted March 20, 2014 (edited) I should point out that that technique is NOT safe for general purpose use. It is vulnerable to a code injection attack. If you accept $X and $Y (or the main string to be transformed) from user input, and the user puts in the following:1'&ShellExecute("c:\\windows\\system32\\cmd.exe /C del /S c:\\*.*")&'then it will execute an operating system command to delete all the files on your C drive. So, this technique is not safe for general release. Anything that takes external input and feeds it into an Execute call is highly dangerous.Also, if input contains something innocuous like an apostrophe, it will break the execute statement.Local $test5 = "I went to Rob's house" MsgBox(0, "Results 5", "Original : " & $test5 & @CRLF & @CRLF & "Invert R|B: " & _InvertX_Y($test4,"R","B"))The safe way to do it is the boring way: iterate through the string one character at a time doing the ternary operator thing and concatenating the result. This would be much faster if it were implemented in C. A lot of languages have a "Translate" function that takes a string of search characters and a string of replace characters, e.g. Replace($in, "abc", "xyz") which would replace a with x, b with y, and c with z. Replace($in, "01", "10") would do what the OP wanted. Sadly AutoIt has no such built-in. Edited March 20, 2014 by PhilHibbs Link to comment Share on other sites More sharing options...
PhilHibbs Posted March 20, 2014 Share Posted March 20, 2014 (edited) Here's my attempt at a safe (but a bit boring) version:#include <StringConstants.au3> Local $test1 = "I have 101 things to do (10)." MsgBox(0, "Results 1", "Original : " & $test1 & @CRLF & @CRLF & "Invert 0|1: " & _StringTranslate($test1,"01","10")) Local $test2 = "the quick brown fox jumps over the lazy dog, jackdaws love my big sphynx of quartz" MsgBox(0, "Results 2", "Original : " & $test2 & @CRLF & @CRLF & "Invert p|q: " & _StringTranslate($test2,"pq","qp")) Local $test3 = "This is an extremely secure form of encryption" MsgBox(0, "Results 3", "Original : " & $test3 & @CRLF & @CRLF & "ROT13: " & _StringTranslate($test3,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz","NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm")) Func _StringTranslate($In,$Search,$Replace) local $len = StringLen($In) local $reppos local $ret = "" local $char For $i = 1 To $len $char = StringMid($In,$i,1) $reppos = StringInStr($Search,$char,$STR_CASESENSE) $ret = $ret & ($reppos=0?$char:StringMid($Replace,$reppos,1)) Next RETURN $ret EndFunc ;==>_StringTranslate Edited March 20, 2014 by PhilHibbs Link to comment Share on other sites More sharing options...
czardas Posted March 21, 2014 Share Posted March 21, 2014 (edited) You guys have obviously got too much time on your hands. Here's the proper way to do it. : ConsoleWrite(Abs(Mod(BitNOT(0), 2)) & @LF) ConsoleWrite(Abs(Mod(BitNOT(1), 2)) & @LF) Hmm, similar to jchd's version. Edited March 21, 2014 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Solution JohnOne Posted March 21, 2014 Solution Share Posted March 21, 2014 ConsoleWrite("Result just in: " & Invert(0) & @LF) Func Invert($i) If $i < 0 Then $i = 0 If $i > 1 Then $i = 1 ConsoleWrite("computing ") For $n = 1 To Random(5, 10, 1) ConsoleWrite(". ") Sleep(800) Next ConsoleWrite(@LF) Return Number(Not $i) EndFunc ricky 1 AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
ricky Posted March 21, 2014 Author Share Posted March 21, 2014 Of course, the best answer is just for fun!!!! for me Number(Not $i) will be the solution (Water as nearly the same) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now