Jump to content

Removing letters from a string


 Share

Recommended Posts

So, i have a string, say, "HELLO", and i want it to be returned in multiple ways..

For instance, as i can see it, on the first loop, it would remove one letter and return the rest of the string, eg. it would return the following strings:

ELLO
HLLO
HELO
HELO
HELL

And i managed to do that fine using:

$Word="HELLO"
$WordRETURN=""

For $x=1 To StringLen($Word)
$WordNEW=StringReplace($Word,$x," ",1)
$WordNEW=StringStripWS($WordNEW,8)

$WordRETURN&=$WordNEW&@CRLF

Next

MsgBox(0,"",$WordRETURN)

However, i want to now do it again, but taking out 2 letters each time, so the results would look something like:

LLO
ELO
ELO
ELL
LLO
HLO
HLO
HLL
ELO
HLO
HEO
HEL
....AND SO ON

And then following that, i want to do it again, taking out three letters.

I have looked at the concept of making a 5x5 array in the case of "HELLO", so it would look something like this:

H H H H H
E E E E E
L L L L L
L L L L L
O O O O O

And then loop through so you would look at H in the first column then move on to the next column, then once that bit was done, youd return to the first column on E and then move onto the next column...maybe this bit wasnt explained to well, if you dont understand what i am saying here, say so.

So is anyone able to shed some light?

Thanks!!

Edited by furrycow
Instant Lockerz Invite - www.instantlockerzinvite.co.uk
Link to comment
Share on other sites

Maybe something like this.

$Word = "HELLOGOODBYE"
$WordRETURN = ""
$Width = 2

For $x = 1 To StringLen($Word) Step $Width
    $sleft = StringLeft($Word, $x - 1)

    $sright = StringTrimLeft($Word, $x + $Width - 1)

    $WordNEW = $sleft & $sright
    $WordRETURN &= $WordNEW & @CRLF

Next

MsgBox(0, "", $WordRETURN)
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Thanks, but yours removes letters

1 and 2,

and then

3 and 4,

and then

4 and 5...

I was hoping for a system that...

Removes letters

1 and 2,

and then

1 and 3,

and then

1 and 4,

and then

1 and 5,

and then

2 and 1,

and then

2 and 3,

and then

2 and 4,

and then

2 and 5...

Thanks for your help so far!

Instant Lockerz Invite - www.instantlockerzinvite.co.uk
Link to comment
Share on other sites

Still not clear? Removing 1 and 2 would produce the same result as removing 2 and 1. Do you really want to repeat that? And what happens after 5 and 4? Do you then start the process again by removing 1 and 2 and 3 then 1 and 2 and 4? How long does this go on for, until you have removed all letters, or what?

Link to comment
Share on other sites

Sorry about not being clear enough.

In answer to your question, yes 2 and 1 would produce the same as 1 and 2, and no i dont want to repeat, but i figured getting rid of repetition would be easy once i had the main function in place.

And youre correct, after removing all the two letters, i would then want to move onto removing three, and then four and so on, AS LONG AS, the remaining string length was >1.

After removing 5 and 4, it would then loop to go on and do, "1 and 2 and 3" as long as the remaining string length is greater than 1.

It can return each string as it goes/add them to an array/or add them onto an existing string, i dont mind.

Thanks czardas

Instant Lockerz Invite - www.instantlockerzinvite.co.uk
Link to comment
Share on other sites

Sorry about not being clear enough.

In answer to your question, yes 2 and 1 would produce the same as 1 and 2, and no i dont want to repeat, but i figured getting rid of repetition would be easy once i had the main function in place.

And youre correct, after removing all the two letters, i would then want to move onto removing three, and then four and so on, AS LONG AS, the remaining string length was >1.

After removing 5 and 4, it would then loop to go on and do, "1 and 2 and 3" as long as the remaining string length is greater than 1.

It can return each string as it goes/add them to an array/or add them onto an existing string, i dont mind.

Thanks czardas

Must the results be in the order you describe? If not then it's simple.(I think)

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

maybee something like this?

it does what you want ... just not in the order that you want it Posted Image

remove("HELLO")

Func remove($text)
    $text = StringSplit($text,"")
    Dim $arr[UBound($text)]
    back($text,1,$arr)
EndFunc

Func back($text,$k,$arr)
    If $k=$text[0]+1 Then
        Local $ret=""
        For $i=1 To $text[0]
            If Not $arr[$i] Then $ret&=$text[$i]
        Next
        ConsoleWrite($ret & @CRLF)
    Else
        For $i=0 To 1
            $arr[$k] = $i
            back($text,$k+1,$arr)
        Next
    EndIf
EndFunc
Posted Image

Only two things are infinite, the universe and human stupidity, and i'm not sure about the former -Alber EinsteinPractice makes perfect! but nobody's perfect so why practice at all?http://forum.ambrozie.ro

Link to comment
Share on other sites

YOU LEDGEND!

Thank you so much, man have i been trying to get this to work for some time!!

I was on the right trail though, i remember having a script that searched all files in all levels of directories in a specific directory, where it kept calling back on the function, so i was testing with that, but you have saved me a tonne of time!!

Thanks everyone for their help.

Instant Lockerz Invite - www.instantlockerzinvite.co.uk
Link to comment
Share on other sites

remove("HELLO")

Func remove($text)
    $text = StringSplit($text,"")
    Dim $arr[UBound($text)]
    back($text,1,$arr)
EndFunc

Func back($text,$k,$arr)
    If $k=$text[0]+1 Then
        Local $ret=""
        For $i=1 To $text[0]
            If Not $arr[$i] Then $ret&=$text[$i]
        Next
        ConsoleWrite($ret & @CRLF)
    Else
        For $i=0 To 1
            $arr[$k] = $i
            back($text,$k+1,$arr)
        Next
    EndIf
EndFunc
Posted Image

Sweet! It reminds me of ping pong. :D
Link to comment
Share on other sites

For the record, I was thinking of this

strip("HELBO")

Func Strip($text)
    $a = StringSplit($text, "")
    For $i = 0 To 2 ^ StringLen($text) - 1
    $res = ''
    For $j = 1 To StringLen($text)
    If BitAND($i, 2 ^ ($j-1)) Then $res &= $a[$j]
    Next
    ConsoleWrite($res & @CRLF)
    Next
EndFunc ;==>Strip
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

For the record, I was thinking of this

strip("HELBO")

Func Strip($text)
    $a = StringSplit($text, "")
    For $i = 0 To 2 ^ StringLen($text) - 1
    $res = ''
    For $j = 1 To StringLen($text)
    If BitAND($i, 2 ^ ($j-1)) Then $res &= $a[$j]
    Next
    ConsoleWrite($res & @CRLF)
    Next
EndFunc ;==>Strip

So glad you posted this martin. I never knew what bitwise operations were about, until I read this. How useful!
Link to comment
Share on other sites

This should also work the way you want.

;;***********************************************
;; needs patch to Array.au3 as per ticket #1438
;;***********************************************

#include <Array.au3>

Global $str = 'Hello_World!'        ; example input

Local $list = StringSplit($str, '', 2)
Local $tuples[1] = [0], $tmp
For $i = UBound($list) - 1 To 1 Step -1
    $tmp = _ArrayCombinations($list, $i, '')        ;; needs patch to Array.au3 as per ticket #1438
    _ArrayDelete($tmp, 0)
    _ArrayConcatenate($tuples, $tmp, 0)
Next
_ArrayDelete($tuples, 0)
_ArrayDisplay($tuples, "Final result")

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

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