Jump to content

Invert a number


 Share

Go to solution Solved by JohnOne,

Recommended Posts

Possible solution:

$iNumber = 0
$iNumber = Int(Not $iNumber)
ConsoleWrite($iNumber & @LF)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

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 2022-02-19 - Version 1.6.1.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 (NEW 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

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 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

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

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 2022-02-19 - Version 1.6.1.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 (NEW 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

Yet another skin:

Local $test = Random(0, 1, 1)
ConsoleWrite($test & " --> " & Mod($test + 1, 2) & @LF)
Edited 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 here
RegExp tutorial: enough to get started
PCRE 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

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
Link to comment
Share on other sites

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_1
I suppose for convention's sake it should be called something beginning with _String. Edited by PhilHibbs
Link to comment
Share on other sites

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 by PhilHibbs
Link to comment
Share on other sites

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 by PhilHibbs
Link to comment
Share on other sites

  • Solution

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

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

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
 Share

  • Recently Browsing   0 members

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