Jump to content

Recommended Posts

Posted (edited)

Im trying to remove some unwanted chars from a string

This is the string and it will be different each time

  Quote

 

3EDF35C6C10345A78F885679C86D701A

Now the numbers i want to build the remove sequence are these and they will be different each time as well

36379

Ive managed to generate these numbers after much messing about but this is the bit i cant work out how to do

I want to remove the corresponding char from the large number for each of the smaller one

The ones below demonstrate

3EDF35C6C10345A78F885679C86D701A
  3     6  3      7        9

So im looking to remove from the main string which correspond to the number positions counting from the left

Can someone point me in the right way?

Edited by Chimaera
Posted

Example()

Func Example()
    Local $sString = '3EDF35C6C10345A78F885679C86D701A'
    Local $iLength = StringLen($sString)
    Local $aInts[] = [3, 6, 3, 7, 9], _ ; The positions.
            $sSearchPattern = '', $sReplacePattern = ''
    Local $iGroup = 1
    For $i = 0 To UBound($aInts) - 1
        $iLength -= $aInts[$i]
        $sSearchPattern &= '([[:xdigit:]]{' & $aInts[$i] - 1 & '})[[:xdigit:]]' ; Search pattern.
        $sReplacePattern &= '${' & $iGroup & '}' ; Replacement pattern.
        $iGroup += 1 ; Increment the replacement group value.
    Next
    If $iLength > 0 Then ; If there is values still remaining create a capture all group.
        $sSearchPattern &= '([[:xdigit:]]{' & $iLength & '})'
        $sReplacePattern &= '${' & $iGroup & '}'
    EndIf
    ConsoleWrite($sSearchPattern & @CRLF)
    ConsoleWrite($sReplacePattern & @CRLF)
    ConsoleWrite(StringRegExpReplace($sString, $sSearchPattern, $sReplacePattern) & @CRLF)
EndFunc   ;==>Example

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted
a draft...
 
 
#include <array.au3>

$input = "3EDF35C6C10345A78F885679C86D701A"
$delete = "36379"

$split = StringSplit($input, "", 2)
$remove = StringSplit($delete, "", 2)
Local $pointer = 0
For $i = 0 To UBound($remove) - 1
    $pointer += $remove[$i]
    $split[$pointer] = "to be deleted"
Next

_ArrayDisplay($split)

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

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

Posted (edited)

2 = $STR_NOCOUNT

Edit: Also you're a little off with your calculation.

Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted (edited)

Call me cheese as I am on a roll today with code snippets!

Example()

Func Example()
    Local $sString = '3EDF35C6C10345A78F885679C86D701A'
    Local $aPos[] = [3, 6, 3, 7, 9] ; The positions.
    ConsoleWrite(_StringStringByPos($sString, $aPos) & @CRLF)

    $sString = 'ABCDE'
    $sString = _StringStringByPos($sString, $aPos)
    ConsoleWrite($sString & @CRLF) ; This will work as well, though 6,3,7,9 don't exist as the string is quite short.
EndFunc   ;==>Example

Func _StringStringByPos($sString, ByRef $aPos) ; $aPos must be a zero based array of integer values.
    Local Const $STRING_LEN = StringLen($sString)
    If Not UBound($aPos) Or Not $STRING_LEN Then Return SetError(1, 0, $sString)

    Local $iGroup = 1, $iLength = $STRING_LEN, $iTotalPos = 0, _
            $sReplacePattern = '', $sSearchPattern = ''
    For $i = 0 To UBound($aPos) - 1
        $aPos[$i] = Int($aPos[$i])
        $iLength -= $aPos[$i]
        $iTotalPos += $aPos[$i]
        If $iTotalPos > $STRING_LEN Then
            $iLength += $aPos[$i]
            ExitLoop
        EndIf
        $sSearchPattern &= '([[:xdigit:]]{' & $aPos[$i] - 1 & '})[[:xdigit:]]' ; Search pattern.
        $sReplacePattern &= '${' & $iGroup & '}' ; Replacement pattern.
        $iGroup += 1 ; Increment the replacement group value.
    Next
    If $iLength > 0 Then ; If there is values still remaining create a capture all group.
        $sSearchPattern &= '([[:xdigit:]]{' & $iLength - 1 & '})'
        $sReplacePattern &= '${' & $iGroup & '}'
    EndIf
    Return StringRegExpReplace($sString, $sSearchPattern, $sReplacePattern)
EndFunc   ;==>_StringStringByPos
Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted (edited)

Thanks guys the only comment i would reiterate is both numbers are random i cant use a fixed position like in guinness eg

and the positions for the removal come from the random number

Also i may have shot myself in the foot abecause if by some random chance all the numbers are 9 ie 99999 it will exceed the 32 limit of the main number

Edited by Chimaera
  • Moderators
Posted

guinness,

Hi "Cheese"

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted

  On 3/2/2014 at 3:14 PM, Chimaera said:

Thanks guys the only comment i would reiterate is both numbers are random i cant use a fixed position like in guinness eg

and the positions for the removal come from the random number

Also i may have shot myself in the foot abecause if by some random chance all the numbers are 9 ie 99999 it will exceed the 32 limit of the main number

Just use StringSplit('264568', '', $STR_NOCOUNT) to create the $aPos Array. I don't see the issue?!

What I gave you is working code. What's the problem?

  On 3/2/2014 at 3:14 PM, Melba23 said:

guinness,

Hi "Cheese"

M23

Classic.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted
  On 3/2/2014 at 3:22 PM, Chimaera said:

ok ill have a play thx

Is there something I have missed?

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted (edited)

#include <array.au3>

$input = "3EDF35C6C10345A78F885679C86D701A"
$delete = "36379"

ConsoleWrite($input & @CRLF & Example($input, $delete) & @CRLF)

Func Example($input, $delete)
    $split = StringSplit($input, "", 2)
    $remove = StringSplit($delete, "", 2)
    Local $pointer = 0
    For $i = 0 To UBound($remove) - 1
        $pointer += $remove[$i]
        If $pointer <= UBound($split) Then
            $split[$pointer - 1] = ""
        ;    $split[$pointer - 1] = " " ; <-- use this to see the "holes" in output string
        EndIf
    Next
    Return _ArrayToString($split, "")
EndFunc   ;==>Example

edit:

changed listing in "Func" form

Edited by PincoPanco

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

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

Posted

I mean i will try and get it working with my code.

Im running this but it doesnt seem to change the chars

Func Example()
    Local $sString = '3EDF35C6C10345A78F885679C86D701A'
    $stringsplit = StringSplit('2645', '', $STR_NOCOUNT)
    _ArrayDisplay($stringsplit)
    Local $aPos[] = [ $stringsplit] ; The positions.
    ConsoleWrite(_StringStringByPos($sString, $aPos) & @CRLF)

;~     $sString = 'ABCDE'
;~     $sString = _StringStringByPos($sString, $aPos)
;~     ConsoleWrite($sString & @CRLF) ; This will work as well, though 6,3,7,9 don't exist as the string is quite short.
EndFunc   ;==>Example

Ill keep looking at it

Posted

Ah, you misunderstood me.

Func Example()
    Local $sString = '3EDF35C6C10345A78F885679C86D701A'
    Local $aPos = StringSplit('2645', '', $STR_NOCOUNT); The positions.
    ConsoleWrite(_StringStringByPos($sString, $aPos) & @CRLF)
EndFunc   ;==>Example

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted
  On 3/2/2014 at 4:53 PM, mikell said:

 

My 2 cents

$input = "3EDF35C6C10345A78F885679C86D701A"
$delete = "36379"

$aDel = StringSplit($delete, "")
Local $add 
For $i = 1 to $aDel[0]
    $add += $aDel[$i]
    $input = StringReplace($input, $add, "#")
Next
$output = StringReplace($input, "#", "")
Msgbox(0,"", $output)

this fails with $delete = "9999" for example

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

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

Posted (edited)

Thanks for the help guys

I will have to factor in some error stuff to make sure the random numbers dont exceed the amount of chars available.

Ill keep working on it

Thx again

@ guinness After a quick look on 2645

I think it does something weird

Local $sString = '3EDF35C6C10345A78F885679C86D701A'
                     3 DF35C C10 45A7 45A7

Not quite sure where that last 45A7 came from, or the rest of the chars went

Edited by Chimaera
Posted

I already factored that in!

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted (edited)

I already factored that in too.

my >draft just ignores the random values ​​that exceed the amount of available characters

Edited by PincoPanco

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

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

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
  • Recently Browsing   0 members

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