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
Dear PhiLho :) ,
I want one more feature (a bit tough for me to accomplish) to be included inside the parsing loop. Can you help me?... Please.

Regards, :)

Edit: I request you to save your code as .AHK and provide me with a link that will appear in the first post
kWo4Lk1.png

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

Well, Titan's code is short and al, but it doesn't do true Rot13, which acts only on alphabetic characters and replaces them only by alphabetic chars...

Wrong!
I updated the algorithm (see my above post).. sorry for the initial mistake.

@Goyyah, you can upload scripts for free on the AutoHotkey.net file manager.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Dear Titan, :)

you can upload scripts for free on the AutoHotkey.net file manager.


I am aware of that, and have been uploading lot of files. Thanks to you. But I thought you would like to put your version (as .AHK) on your homepage or something, and also you might want to include some note on Version number/Author etc.

Simply put, you modify your code and update your AHK and I dont have to update the download link that shall be on the first post.

Thanks again, :)
kWo4Lk1.png

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
I see your logic, clever.. okay, here's the link: https://ahknet.autoh...l/StringMod.ahk.

I'm reluctant to include this script on my page becuase it's actually your creation and I don't want to steal your due credit :) If you'd like me to promote this script however, I'd be glad to do so.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Dear Titan, :)

I'm reluctant to include this script on my page becuase it's actually your creation and I don't want to steal your due credit If you'd like me to promote this script however, I'd be glad to do so


:) , I don't want any credit for this. If PhiLho had not spotted this function (in the original post) and spent his valuable time enhancing, it would have faded away!. StringMod() has been lucky twice, and you have enhanced it again.

Do whatever you want to do with the code but I request you that the function name remains StringMod(). Please!

And I invite you and everybody to promote this function.

Regards, :)
kWo4Lk1.png

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
OK, I uploaded the file to AutoHotkey.net.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Dear PhiLho & Titan, :)

SCRAMBLED / JUMBLED Words

String = AUTOHOTKEY
Loop, 33
   Str = % Str Scramble(String) Chr(10)
Msgbox,0, % String,% Str

Scramble(String)
{
Loop, Parse,String
 ReturnString = % ReturnString A_LoopField Chr(10)
Sort,ReturnString,Random
Null:= ""
StringReplace, String, ReturnString, `n, %Null%,All
return String
}

Sample output of above code for word "AUTOHOTKEY"

OTUYTHOAEK
KYOAETTOUH
AETTOYUKHO
TEYUOTAOHK
HKTATYEOOU
OYKATHEOTU
KETOYTOHUA
AEHTOOYUTK
TTOYEKOHUA
UAOEHKTYTO
ETTHOOKAUY
HOTUAYOTEK
TOTUAOHYKE
HOKYETTUOA
OTEHOATKUY
UOTYHEKOAT
HAUTTOOYEK
HOYATEOTKU
OTKEOAYHUT
KAOYHEUTOT
UTHKEOTOYA
UTHOTKYOEA
TOYAUHETOK
TTYAOKEHOU
ETTUOHYKOA
EAOUKTTOHY
KHATYOUOTE
UOOHAETKYT
YOHEUOATKT
TOYKAUETOH
AOOHTETYKU
YHTOTKAEUO
OTAHTKEOUY
Can Scramble() be optimised & fit into StringMod() ? :roll:

like StringMod("AUTOHOTKEY","Scramble")

Regards, :)
kWo4Lk1.png

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

Can Scramble() be optimised & fit into StringMod() ? :roll:

Done (https://ahknet.autoh...l/StringMod.ahk)

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
These are the functions I am using.
CharDrop(String,Chars) {

   Loop Parse, Chars

      StringReplace String, String, %A_LoopField%,,All

   Return String

}



CharKeep(String,Chars) {

   Loop Parse, String

      IfInString Chars, %A_LoopField%

         out = %out%%A_LoopField%

   Return out

}
They are simpler, but there are no branches with parameter comparison inside, so they must be faster. Most of the time, they are sufficient. They are so short that you normally just inline them, whithout the function wrapper around. Test them with
Text = *asd,f./?x

MsgBox % CharDrop(Text,"*,?")

MsgBox % CharDrop(Text,".,?/x")

MsgBox % CharKeep(Text,"dsa")

MsgBox % CharKeep(Text,"x,?f")
Creating a monster function, which does everything, might not be a very good idea. It will be slow, and if you only need one of its functionality, why would you include a large function and inflate your code?

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

Creating a monster function, which does everything, might not be a very good idea. It will be slow, and if you only need one of its functionality, why would you include a large function and inflate your code?

StringMod() is quite efficient (at least my version anyway) as it skips code that would not be needed to perform a given action. As the name itself implies, it's a fairly large combined collection of different string manipulation features that can be useful for numerous purposes. I do agree that it's large in code size, but it's easy to chop off the bits of code you don't need.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005

StringMod() is quite efficient (at least my version anyway) ... it's easy to chop off the bits of code you don't need.

Inside the parse loop, you check at each character if the command is Rot, Flip, Replace, Only or something else. It is a bunch of comparisons, branches for each character. This way you save a few lines of code, but the function would be several times slower than necessary. Until AHK has no optimizing compiler, cutting up large functions is faster and easier to maintain. Also, deleting unnecessary functions would require the user to understand, edit and test the code.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Dear Titan, :D

Can Scramble() be optimised & fit into StringMod() ? :roll:

Done (https://ahknet.autoh...l/StringMod.ahk)


Thanks a lot! :) :)
So quick!.. You are a Wizard!

Regards, :)
kWo4Lk1.png

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Dear Laszlo, :)

Creating a monster function, which does everything, might not be a very good idea. It will be slow, and if you only need one of its functionality, why would you include a large function and inflate your code?


Well, I need this function for a word game, in which I will be using most of the functionality of StrMod(). I will be posting the word game here.

Until AHK has no optimizing compiler, cutting up large functions is faster and easier to maintain. Also, deleting unnecessary functions would require the user to understand, edit and test the code


Maybe, its a valid point. I will put on the first post the breakdown of StringMod() functionality.
Omit()
Only()
Flip()
Replace()
Scramble()


Titan may have to help me with Rot13() and Rot47() I guess.

and, let the users decide what they want to have.
IMHO, StringMod() is a good example of adding more power into one single Function. Also an example for Team work

Regards, :)
kWo4Lk1.png

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Laszlo has a point that I wanted to make myself...
I played with the code given by Goyyah (and his algorithm) because it was fun, and the functions were, at start, kind of closely related.
Introducing the Rot functions was borderline...
Pushing functionnalities like scramble seems out of the scope of the function (despite the very generic function name), outside the initial algorithm because it no longer acts on individual characters, choosing what to do on them, but it changes char order, operating at global string scope. (Same for flip, but it is simple enough to be similar to the other functions.)

Having spent quite some time in both the Unix and the Windows, I am sometime splitted between Unix philosophy - make small programs doing little things, but powerful when combined using shell scripting, pipes, redirections etc., and the Windows philosophy - see MS Office or the UltraEdit juggernaut that seems to seduce many users: combining the base text editor with an hex editor, a FTP program, a diff, replacing in files and what's not.

It is seducing indeed: install one program and get most of the tools you need at once, under a consistent interface.
But it is limiting: what if you don't need the FTP? It takes disk space and inteface real estate for nothing. What if the diff doesn't offer all the features you need? You can use another diff tool (WinMerge is good :-)) but then you have a feature that is unused again.
Using separate tools has the advantage of offering flexibility: you get the right tools for you, and if you find a better one, you can switch.
And if you want a unique install program, use NSIS or some other install program (not InstallShield, better suited for monster programs like Office than to install small freewares, creating install programs twice the size of the installed binaries...).

Back to the topic, you can combine the best of both worlds: create several small, efficient satellite functions, and if really you like/need a consistent interface, create a StringMod function that calls these little functions depending on the asked task.
This way, it is easier to pick only the functions you need without having to hack the big function.

Now, Goyyah has a point too: making this function was fun and allowed to exchange points of view on coding! :-)
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

BoBo
  • Guests
  • Last active:
  • Joined: --
Beside the fact that from a(n old fashioned) users point of view - not to waste disk space/memory (regardless that his become cheap) is something I'm still attracted to - I've swapped from the "one script fits all" fshioned scripting to a more modulized form, using #Includes as much as possible. It has decreased the time I wasted to identify a piece of code and its dependencies within a huge script. And maintenance aka an Upgrade/Update/Replacement has become definitely more easiely.

JM2€C. 8)