AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

StringMod() String Manipulation - Enhanced by PhiLho / Titan
Goto page Previous  1, 2, 3, 4, 5, 6  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Thu Sep 14, 2006 10:42 am    Post subject: Reply with quote

PhiLho wrote:
That's because on a long string, the StringSplit will have to find and allocate all the variables, even if only the first is requested. While the Loop Parse stops as soon as it reached its goal, and drops intermediary results.


I agree .. Smile This was the exact thing I wanted to tell Shocked . I am too tired working with Cheetah2.dll .. Very Happy

PhiLho wrote:
Goyyah, why do you hate so much space? I like to put a space after commas and around = or :=, I find it easier on the eye, code breathes... Smile


Yes it looks very bad in BBCode .. however "not-so-bad" in my editor .. I always use space after a comma.. I do not know why I changed it! Shocked . I should return back to my habit.

Regards, Smile
_________________
URLGet - Internet Explorer based Downloader
Back to top
View user's profile Send private message Send e-mail
polyethene



Joined: 11 Aug 2004
Posts: 5248
Location: UK

PostPosted: Thu Sep 14, 2006 10:55 am    Post subject: Reply with quote

PhiLho wrote:
Conceptually, I prefer Goyyah & toralf's way than your, Titan.
I thought it was all about code size. Personally I would use toralf's method because it's the fastest.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Thu Sep 14, 2006 11:00 am    Post subject: Reply with quote

I am using toralf's code already ..

PhiLho wrote:
Conceptually, I prefer Goyyah & toralf's way than your, Titan.


but, PhiLho must have meant Loop,Parse Vs StringSplit method.

Regards, Smile
_________________
URLGet - Internet Explorer based Downloader
Back to top
View user's profile Send private message Send e-mail
polyethene



Joined: 11 Aug 2004
Posts: 5248
Location: UK

PostPosted: Thu Dec 28, 2006 10:58 pm    Post subject: Reply with quote

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:
Code:
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.
_________________
GitHub • Scripts • IronAHK • Contact by email not private message.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
PhiLho



Joined: 27 Dec 2005
Posts: 6836
Location: France (near Paris)

PostPosted: Thu Dec 28, 2006 11:01 pm    Post subject: Reply with quote

As long as the parameters are correctly escaped, if needed...
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
polyethene



Joined: 11 Aug 2004
Posts: 5248
Location: UK

PostPosted: Thu Dec 28, 2006 11:03 pm    Post subject: Reply with quote

PhiLho wrote:
As long as the parameters are correctly escaped, if needed...
Sorry did I miss something?
_________________
GitHub • Scripts • IronAHK • Contact by email not private message.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
PhiLho



Joined: 27 Dec 2005
Posts: 6836
Location: France (near Paris)

PostPosted: Fri Dec 29, 2006 9:20 am    Post subject: Reply with quote

Examples of not working parameters:
StringMod(str, "Omit", ".-,;")
StringMod(str, "Replace", "*", "o")
etc.
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Fri Dec 29, 2006 9:59 am    Post subject: Reply with quote

Nice work Titan Very Happy.
I am yet to test it, but I know it will work fine.
FYI: I have linked that post in the title post.

Regards, Smile
_________________
URLGet - Internet Explorer based Downloader
Back to top
View user's profile Send private message Send e-mail
polyethene



Joined: 11 Aug 2004
Posts: 5248
Location: UK

PostPosted: Fri Dec 29, 2006 2:34 pm    Post subject: Reply with quote

PhiLho wrote:
As long as the parameters are correctly escaped, if needed... StringMod(str, "Omit", ".-,;")
Oh yeah, param1 := RegExReplace(param1, "([\[\\\^\$\.\|\?\*\+\(\)])", "\$1") should be enough right?

Skan wrote:
I have linked that post in the title post.
The link to StringMod.ahk on my site still works btw.
_________________
GitHub • Scripts • IronAHK • Contact by email not private message.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Fri Dec 29, 2006 2:43 pm    Post subject: Reply with quote

Titan wrote:
The link to StringMod.ahk on my site still works btw.


Oh! So the old version is not available! Sad
_________________
URLGet - Internet Explorer based Downloader
Back to top
View user's profile Send private message Send e-mail
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Sat Apr 28, 2007 1:37 pm    Post subject: Reply with quote

Dear Titan, Smile

Thanks for the RegEx version. Iam using Scramble in one of my scripts.

Code:
      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


Regards, Smile
Back to top
View user's profile Send private message Send e-mail
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Sat Apr 28, 2007 2:55 pm    Post subject: Reply with quote

It looks slightly safer to use a non-printable ANSI character for delimiter:
Code:
string = qwertyu iop[
new := RegExReplace(string, "(.)", "$1" Chr(1))
Sort new, % "Random Z D" . Chr(1)
StringReplace new, new, % Chr(1),,All
MsgBox %new%
Back to top
View user's profile Send private message
silkcom



Joined: 23 Jan 2008
Posts: 162

PostPosted: Mon Jul 27, 2009 10:06 pm    Post subject: Reply with quote

Sorry to awaken this old thread, SKAN do you still have your anagrammer.zip file? That's exactly what i'm trying to make Smile.
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Tue Jul 28, 2009 5:25 am    Post subject: Reply with quote

silkcom wrote:
Sorry to awaken this old thread, SKAN do you still have your anagrammer.zip file?


You may download a copy of words.db ( 2.47 MiB ) which is the datafile for the anagram script posted in this topic.
Back to top
View user's profile Send private message Send e-mail
silkcom



Joined: 23 Jan 2008
Posts: 162

PostPosted: Tue Jul 28, 2009 2:11 pm    Post subject: Reply with quote

Hmm, I don't see the anagram script, only the code for the function
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page Previous  1, 2, 3, 4, 5, 6  Next
Page 5 of 6

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group