Sign in to follow this  
Followers 0
phatzilla

Replace each digit in a string

11 posts in this topic

#1 ·  Posted (edited)

I have a string "abc123def432ghi678".  I'd like to replace each digit in this string with another DIFFERENT digit.  Here's what i have so far
 

$string="abc123def432ghi678"

if StringRegExp($string,"[0-9]") Then
     $newstring = StringRegExpReplace($string,"[0-9]",random(0,9,1))
EndIf

msgbox(4096,'',$newstring)

The problem with this is two fold

1) it replaces each digit with ONE digit that it chooses, so for example, the newstring will turn out to be:
 

abc111def111ghi111

or
 

abc555def555ghi555

etc etc

2) There's no guarentee that it wont replace the digit with a different one, since its just using a random function.

How would i go about replacing each digit in the given string, with a different new digit?
 

Edited by phatzilla

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

the reason it's only a specific digit that is placed instead of the original digits, is that the Random() expression is evaluated only once. and besides, what is the If good for? if no digits found, nothing would be replaced anyway.

i'd assume a RegExp solution exists, but i'd go about it the simple way:

$string="abc123def432ghi678"
$newstring=ReplaceDigits($string)
msgbox(4096,'',$newstring)

Func ReplaceDigits($sInput)
    Local $sOutput='', $i
    For $i=1 To StringLen($sInput)
        If StringIsInt(StringMid($sInput,$i,1)) Then
            $sOutput&=Random(0,9,1)
        Else
            $sOutput&=StringMid($sInput,$i,1)
        EndIf
    Next
    Return $sOutput
EndFunc

and as for your second requirement:

$string="abc123def432ghi678"
$newstring=ReplaceDigits($string)
msgbox(4096,'',$newstring)

Func ReplaceDigits($sInput)
    Local $sOutput='', $i, $sNewDigit
    For $i=1 To StringLen($sInput)
        If StringIsInt(StringMid($sInput,$i,1)) Then
            Do
                $sNewDigit=Random(0,9,1)
            Until $sNewDigit<>StringMid($sInput,$i,1)
            $sOutput&=$sNewDigit
        Else
            $sOutput&=StringMid($sInput,$i,1)
        EndIf
    Next
    Return $sOutput
EndFunc

and just for our curiosity, what is this for?  :huh2:

Edited by orbs

Share this post


Link to post
Share on other sites

Another example.

Local $string = "abc123d1ef432ghi12345678901234567890"
ConsoleWrite($string & @LF)

$string = Execute(StringRegExpReplace("'" & $string & "'", "(\d)", "' & _Random(\1) & '"))
ConsoleWrite($string & @LF)


Func _Random($iVal)
    Local $iRet = Random(0, 9, 1)
    While $iRet = $iVal
        $iRet = Random(0, 9, 1)
    WEnd
    Return $iRet
EndFunc   ;==>_Random

#cs An example return:-
abc123d1ef432ghi12345678901234567890
abc318d9ef606ghi06524323074712609517
#ce

Share this post


Link to post
Share on other sites

phatzilla,

Can I play too? :D

#include <Array.au3>

Local $sString = "abc123d1ef432ghi12345678901234567890"

ConsoleWrite($sString & @CRLF)

$sNewString = _ReplaceDigits($sString)

ConsoleWrite($sNewString & @LF)

Func _ReplaceDigits($sString)

    $aChars = StringSplit($sString, "")
    For $i = 1 To $aChars[0]
        $sChar = $aChars[$i]
        Switch $sChar
            Case "0" To "9"
                Do
                    $sNewChar = Chr(Random(48, 57, 1))
                Until $sNewChar <> $sChar
                $aChars[$i] = $sNewChar
        EndSwitch
    Next
    Return _ArrayToString($aChars, "", 1, Default)

EndFunc   ;==>_ReplaceDigits
And like water I would love to know why you want to do this. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Cool. But please explain how this works?

Local $string = "abc123"
$string = Execute(StringRegExpReplace("'" & $string & "'", "(\d)", "' & _test(\1) & '"))
ConsoleWrite($string & @LF)
Func _test($in)
    Return 'b'
EndFunc   ;==>_Random

Return as expected: abcbbb

But this:

Local $string = "abc123"
$string = Execute(StringRegExpReplace("'" & $string & "'", "(\D)", "' & _test(\1) & '"))
ConsoleWrite($string & @LF)

Func _test($in)
    Return 'b'
EndFunc   ;==>_Random

return nothing..

D return nothing

w return nothing

. return nothing

only works for d

Why?

Thank you.

Edited by Inververs

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Another skin for the same cat, no (visible) loop:

#include <Array.au3>

Local $string = "abc123d1ef432ghi12345678901234567890"
ConsoleWrite($string & @LF)

$string = Execute(StringRegExpReplace("'" & $string & "'", "(\d)", "' & _NotMe(\1) & '"))
ConsoleWrite($string & @LF)

Func _NotMe($iVal)
    Local $a = [0,1,2,3,4,5,6,7,8,9]
    _ArrayDelete($a, $iVal)
    Return $a[Random(0, 8, 1)]
EndFunc   ;==>_NotMe
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

jc, Inververs' question is very interesting

Local $string = "abc123"
;$string = Execute(StringRegExpReplace("'" & $string & "'", "([[:alpha:]])", "' & _test(\1) & '"))
$string = Execute(StringRegExpReplace("'" & $string & "'", "([[:alpha:]])", "' & _test('\1') & '"))
msgbox(0,"", $string)

Func _test($in)
    Return StringUpper($in)
EndFunc

This works with w but doesn't work with . or a negated class

Share this post


Link to post
Share on other sites

Local $string = "abc123"
$string = Execute(StringRegExpReplace("'" & $string & "'", "(\D)", "' & _test(\1) & '"))
ConsoleWrite($string & @LF)

Func _test($in)
    Return 'b'
EndFunc   ;==>_Random

return nothing..

D return nothing

w return nothing

. return nothing

only works for d

Why?

Thank you.

 

This works.

Local $string = "abc123"

$string = Execute("'" & StringRegExpReplace($string, "(\D)", "' & _test(""\1"") & '") & "'")
ConsoleWrite("'" & StringRegExpReplace($string, "(\D)", "' & _test(""\1"") & '") & "'" & @LF)
ConsoleWrite($string & @LF)

Func _test($in)
    Return 'b'
EndFunc   ;==>_test

#cs Returns:-
'' & _test("b") & '' & _test("b") & '' & _test("b") & '123'
bbb123
#ce

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Sorry, I read too fast the first topic.

Something similar to others :

$string = "abc123def432ghi678"

$string = Execute (  StringRegExpReplace("'" & $string & "'" , "(\d)", "' & StringMid( StringReplace('0123456789', '\1', ''), Random(1, 9, 1), 1) & '")  )
ConsoleWrite ($string)
Edited by jguinch

Share this post


Link to post
Share on other sites

 

This works.

Local $string = "abc123"

$string = Execute("'" & StringRegExpReplace($string, "(\D)", "' & _test(""\1"") & '") & "'")
ConsoleWrite("'" & StringRegExpReplace($string, "(\D)", "' & _test(""\1"") & '") & "'" & @LF)
ConsoleWrite($string & @LF)

Func _test($in)
    Return 'b'
EndFunc   ;==>_test

#cs Returns:-
'' & _test("b") & '' & _test("b") & '' & _test("b") & '123'
bbb123
#ce
 
now it is clear. Thanks!

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