Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

StringMod() String Manipulation - Enhanced by PhiLho / Titan


  • Please log in to reply
77 replies to this topic
SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Oh! It is lost in my pile of backup DVD's, I guess.

You may try this version which also uses my Words.DB : Script for Anagrams, Word manipulation, Scrabble posted by peterm

The links seems to be broken on that Topic and you may follow the following instead.
https://ahknet.autoh...Anagram-V6j.ahk

ghee22
  • Members
  • 36 posts
  • Last active: Nov 12 2009 04:44 PM
  • Joined: 04 Jul 2009

Following a recent discussion I remembered this thread and how regular expressions can be used to simplify most things. Here's a new version with comments:

StringMod(string, method, param1 = "", param2 = "") {
	If method = Only ; remove all characters not in param1 from string:
		new := RegExReplace(string, "i)[^" . param1 . "]")
	Else If method = Omit ; remove characters in param1 from string:
		new := RegExReplace(string, "i)[" . param1 . "]")
	Else If method = Flip ; flip string backwards:
		Loop, % length := StrLen(string) ; loop for the length of the string
			new .= SubStr(string, length - A_Index, 1) ; get character from the end going backwards
	Else If method = Replace ; replace param1 with param2
		new := RegExReplace(string, "i)" . param1, param2)
	Else If RegExMatch(method, "Rot(?:ate)?(\d+)", rotN) ; for Rot13 and Rot47:
	{
		Loop, Parse, string ; for each character in the string:
			new .= Chr((chr := Asc(A_LoopField)) + rotN1) - 94 * (chr > 126)) ; apply rotation transformation
				; this increases the ASCII value of the character by the specified rotation amount
				; if the value exceeds 126 (highest character) subtract by 94 (character range)
	}
	Else If method = Scramble ; rearrange characters in random order
	{
		new := RegExReplace(string, "(.)", "$1÷") ; delimit each character with '÷'
		; this high-ASCII character is unlikely to exist in string, if it does it will be removed
		Sort, new, Random Z D÷ ; randomly sort
		new := RegExReplace(new, "÷") ; remove delimiter
	}
	Else If method = Bloat ; pad each character with param1 and param2:
		new := RegExReplace(string, "(.)", param1 . "$1" . param2)
	Else If method = Pattern ; return the number of times each character appears in the string:
	{
		unique := RegExReplace(string, "(.)", "$1÷") ; delimit each character with '÷' (like in 'Scramble') 
		Sort, unique, U Z D÷ ; remove duplicates
		unique := RegExReplace(unique, "÷") ; remove delimiter
		Loop, Parse, unique ; for each unique character in the string:
		{
			StringReplace, string, string, %A_LoopField%, , UseErrorLevel ; get the number of replacements
			new .= A_LoopField . ErrorLevel ; store this alongside the character itself
		}
	}
	Return, new ; return the modified string
}
Untested but I'm sure it works.


Hello Titan, the latest AHK version (1.0.48.05) gives this error upon compiling:
StringMod.ahk (14) : ==> Missing "("
     Specifically: )
>Exit code: 2    Time: 0.221

Anyone have a fix?

MasterFocus
  • Moderators
  • 4323 posts
  • Last active: Jan 28 2016 01:38 AM
  • Joined: 08 Apr 2009

StringMod.ahk ([color=red]14[/color]) : ==> Missing "("

Line 14 is:
new .= Chr[color=red]([/color][color=darkred]([/color]chr := Asc[color=orange]([/color]A_LoopField[color=orange])[/color][color=darkred])[/color] + rotN1[color=red])[/color] - 94 * [color=green]([/color]chr > 126[color=green])[/color])

new .= Chr((...(...))...)...(...))


-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Antonio França -- git.io -- github.com -- ahk4.net -- sites.google.com -- ahkscript.org

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.