AutoTrim, Off string =example text sliced into bits Loop, Parse, string If !Mod(A_Index, 3) { sub = %sub%%A_LoopField% MsgBox [%sub%] ; your code for the substring goes here sub := "" } Else sub =%sub%%A_LoopField%

StringMod() String Manipulation - Enhanced by PhiLho / Titan
Started by
SKAN
, Mar 03 2006 07:04 AM
77 replies to this topic
I don't see the reason for StringSlice(). If you really need it, a few simple lines would do it:
#46
-
Posted 19 March 2006 - 12:01 AM

autohotkey.com/net Site Manager
Contact me by email (polyethene at autohotkey.net) or message tidbit
StrLen(String) // n = Floor(StrLen(String)/n) and we need Ceil here, what you could get with (StrLen(String)+n-1) // n.I would use k := StrLen(String) // n
But it is 2 chars longer, which is a horrible loss of resources. :lol:I would have written (A_Index - 1) * n + 1, showing better the logic (IMHO).
It will be replaced with a same-line construct "if (cond) command", when it gets to AHK.And using SetEnv to avoid having two lines is a bit less readable.
#47
-
Posted 19 March 2006 - 12:06 AM

Titan: your code does not handle the slack, the last block of chars, if it is shorter than n.
Also, you assume that the blocks are processed sequentially. The main advantage of StringSplit and StringSlice is that they facilitate random access to the blocks.
Also, you assume that the blocks are processed sequentially. The main advantage of StringSplit and StringSlice is that they facilitate random access to the blocks.
#48
-
Posted 19 March 2006 - 12:15 AM

Dear Laszlo, 
I Thank you very much for the Optimised Version. (also for fixing the bugs). I require StringSlice() for many more tasks.
Thanks to PhiLho & Titan for throwing more light on the subject.
Regards,

I Thank you very much for the Optimised Version. (also for fixing the bugs). I require StringSlice() for many more tasks.

Thanks to PhiLho & Titan for throwing more light on the subject.
Regards,

#50
-
Posted 19 March 2006 - 07:32 AM

Good idea, here is my take, reformatted to be included in my private collection of useful string functions:The main advantage of StringSplit and StringSlice is that they facilitate random access to the blocks.
SliceString(_string, _sliceSize, _arrayName="") { local k ; Makes created array global If (_arrayName = "") { _arrayName = Array } k := Ceil(StrLen(_string) / _sliceSize) Loop %k% { StringMid %_arrayName%%A_Index%, _string, (A_Index - 1) * _sliceSize + 1, _sliceSize } %arrayName%0 := k Return k } ; Thin wrapper, just avoids to make the maths each time GetStringSlice(_string, _sliceSize, _sliceIndex) { local slice StringMid slice, _string, (_sliceIndex - 1) * _sliceSize + 1, _sliceSize Return slice } x := SliceString("Dictionary", 3) ListVars MsgBox %x% x := SliceString("Dictionar", 3, "var") ListVars MsgBox %x% slice := GetStringSlice("Dictionary", 3, 2) MsgBox %slice% slice := GetStringSlice("Dictionary", 3, 4) MsgBox %slice%Of course, you were right for the Ceil vs. Floor, my proposal was stupid.
I make horrible "waste of resources", adding lines and spaces, and even unnecessary braces... :-P But that's my choice, I dropped cramped styles a long time ago... And I like my function names to start with a verb (a not so uncommon choice, even official in Java).
#51
-
Posted 19 March 2006 - 12:02 PM


For short functions the style does not really matter. For complex programs I often need to separate blocks, sub-blocks, sub-sub-blocks… You quickly run out of options: Indentations, empty lines, block comments, separator lines. If I define functional blocks as separate functions, I often have to turn pages and find my way back to the original place after several levels of indirections. This is why I write short elementary tasks as compact as possible and use separators only between them. (I dropped this style, when – long ago, for a short time – my performance was measured by the number of uncommented lines of code. My employer abandoned this nonsense when I showed that my 20-line function was twice faster and much easier to maintain than the 500-line module it replaced.)I dropped cramped styles a long time ago... And I like my function names to start with a verb
Of course, this is personal preference, like naming. The company coding standard required the use of informative, hierarchical variable names, like MeasurementSubsystem_SignalQuality_DataAnalysis_FastHartleyTransform_TimeVariable. I used "t" instead, and a header explaining its meaning, and the function was called, outrageously, FHT.
If you build a library, you sort function names for finding easier, what you need. It is more convenient to have all the string functions next to each other, like StringSlice, StringSort, StringSplit… than having all the GetSomething, SortSomething functions together. Especially, since String, Int, Num, Bin, List are more or less standardized, but Get, Read, Acquire, Peek, Extract could be synonymous, and you have to browse the list at all these places.
#52
-
Posted 19 March 2006 - 02:52 PM

#53
-
Posted 28 March 2006 - 09:20 AM

Dear Titan, 
I have a requirement for which I wrote the following
Thought this was interesting enough to be included something like:
StringMod(String, "Subword", 6, A_Space)
Regards,

I have a requirement for which I wrote the following
Phrase:="The Quick Brown Fox Jumps Over The Lazy Dog" MsgBox, % SubWord(Phrase,6,A_Space) ; Returns "Over" Return SubWord(Phrase,Num,Delimiter="") { Loop,Parse,Phrase, %Delimiter% { RWord:=A_LoopField Index:=A_Index IfEqual,A_Index,%Num%,Break } IfLess,Index,%Num%, Return, "" Return RWord }
Thought this was interesting enough to be included something like:
StringMod(String, "Subword", 6, A_Space)
Regards,

#55
-
Posted 14 September 2006 - 09:55 AM

I liked the idea, but I couldn't resist. :) So here is a tweak (50% less lines of code ;)
SubWord(Phrase,Num,Delimiter="") { Loop,Parse,Phrase, %Delimiter% IfEqual,A_Index,%Num%, Return A_LoopField Return }
#56
-
Posted 14 September 2006 - 10:13 AM

Ciao
toralf
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.
toralf
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.
here is a tweak (50% less lines of code
Oh Yes!

The function was only 5 minutes old when I posted it.. Guess I am very tired with sleepless nights..
.. And I love this forum .. getting such instant optimisations!
Regards,

#57
-
Posted 14 September 2006 - 10:18 AM

This is even shorter:
subword(str, n, del = "") { StringSplit, w, str, %del% Return, w%n% }
#58
-
Posted 14 September 2006 - 10:21 AM

This is even shorter:
subword(str, n, del = "") { StringSplit, w, str, %del% Return, w%n% }
Yes! I thought of it .. but wanted to avoid it because:
This function is meant for a dictionary where the meaning can run up to 2k ...
and I am concerned only with the first 9 words ..
To make it clear .. I am creating a English Dictionary DB where the user can subsearch any of the first 9 words in the meaning!
Regards,

#59
-
Posted 14 September 2006 - 10:28 AM

Conceptually, I prefer Goyyah & toralf's way than your, Titan. 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.
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... :-)
Just a thought, coding rules are very subjective, of course.
[EDIT] For the record, I just saw Goyyah's post above. He thinks like me... :-)
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... :-)
Just a thought, coding rules are very subjective, of course.
[EDIT] For the record, I just saw Goyyah's post above. He thinks like me... :-)
#60
-
Posted 14 September 2006 - 10:33 AM

