Jump to content
Sign in to follow this  
ricky

Invert a number

Recommended Posts

ricky

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

Share this post


Link to post
Share on other sites
water

Possible solution:

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

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
water

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 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
Chimp

also

$test = 1 * ($test = 0)

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites
jdelaney

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.

Share this post


Link to post
Share on other sites
Chimp

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

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites
UEZ

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
water

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 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
PhilHibbs

$test = 1 - $test
Or, 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 by PhilHibbs

Share this post


Link to post
Share on other sites
jchd

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)

Share this post


Link to post
Share on other sites
Malkey

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
  • Like 1

Share this post


Link to post
Share on other sites
PhilHibbs

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

Share this post


Link to post
Share on other sites
PhilHibbs

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

Share this post


Link to post
Share on other sites
PhilHibbs

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

Share this post


Link to post
Share on other sites
PhilHibbs

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

Share this post


Link to post
Share on other sites
JohnOne

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

  • Like 1

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
ricky

Of course, the best answer is just for fun!!!!

for me Number(Not $i) will be the solution (Water as nearly the same)


Share this post


Link to post
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
Sign in to follow this  

×