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
polyethene



Joined: 11 Aug 2004
Posts: 5248
Location: UK

PostPosted: Sun Mar 19, 2006 12:01 am    Post subject: Reply with quote

I don't see the reason for StringSlice(). If you really need it, a few simple lines would do it:

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

_________________
GitHubScriptsIronAHK Contact by email not private message.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Laszlo



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

PostPosted: Sun Mar 19, 2006 12:06 am    Post subject: Reply with quote

PhiLho wrote:
I would use k := StrLen(String) // n
StrLen(String) // n = Floor(StrLen(String)/n) and we need Ceil here, what you could get with (StrLen(String)+n-1) // n.

PhiLho wrote:
I would have written (A_Index - 1) * n + 1, showing better the logic (IMHO).
But it is 2 chars longer, which is a horrible loss of resources. Laughing

PhiLho wrote:
And using SetEnv to avoid having two lines is a bit less readable.
It will be replaced with a same-line construct "if (cond) command", when it gets to AHK.
Back to top
View user's profile Send private message
Laszlo



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

PostPosted: Sun Mar 19, 2006 12:15 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
polyethene



Joined: 11 Aug 2004
Posts: 5248
Location: UK

PostPosted: Sun Mar 19, 2006 12:22 am    Post subject: Reply with quote

I see. Well your solution would be the best option for slicing strings then.
_________________
GitHubScriptsIronAHK 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: Sun Mar 19, 2006 7:32 am    Post subject: Reply with quote

Dear Laszlo, Smile

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

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

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



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

PostPosted: Sun Mar 19, 2006 12:02 pm    Post subject: Reply with quote

Laszlo wrote:
The main advantage of StringSplit and StringSlice is that they facilitate random access to the blocks.

Good idea, here is my take, reformatted to be included in my private collection of useful string functions:
Code:
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... Razz 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).
_________________
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
Laszlo



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

PostPosted: Sun Mar 19, 2006 2:52 pm    Post subject: Reply with quote

PhiLho wrote:
I dropped cramped styles a long time ago... And I like my function names to start with a verb
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.)

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.
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Tue Mar 28, 2006 9:20 am    Post subject: Reply with quote

Goyyah wrote:
Function LDOM() has been reposted Here...

Sorry for any inconvenience. Regards Smile
_________________
URLGet - Internet Explorer based Downloader


Last edited by SKAN on Tue Mar 28, 2006 11:34 am; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail
polyethene



Joined: 11 Aug 2004
Posts: 5248
Location: UK

PostPosted: Tue Mar 28, 2006 10:17 am    Post subject: Reply with quote

Thats more to do with time rather than strings Question
_________________
GitHubScriptsIronAHK 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: Thu Sep 14, 2006 9:55 am    Post subject: Reply with quote

Dear Titan, Smile

I have a requirement for which I wrote the following

Code:
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, Smile
_________________
URLGet - Internet Explorer based Downloader
Back to top
View user's profile Send private message Send e-mail
toralf



Joined: 31 Jan 2005
Posts: 3910
Location: Bremen, Germany

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

I liked the idea, but I couldn't resist. :) So here is a tweak (50% less lines of code ;)
Code:
SubWord(Phrase,Num,Delimiter="") {
    Loop,Parse,Phrase, %Delimiter%
        IfEqual,A_Index,%Num%, Return A_LoopField
    Return
  }

_________________
Ciao
toralf
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 10:18 am    Post subject: Reply with quote

toralf wrote:
here is a tweak (50% less lines of code Wink


Oh Yes! Very Happy Many thanks for the tweak toralf!
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, 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:21 am    Post subject: Reply with quote

This is even shorter:
Code:
subword(str, n, del = "") {
   StringSplit, w, str, %del%
   Return, w%n%   
}
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 10:28 am    Post subject: Reply with quote

Titan wrote:
This is even shorter:
Code:
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, Smile
_________________
URLGet - Internet Explorer based Downloader
Back to top
View user's profile Send private message Send e-mail
PhiLho



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

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

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... Smile
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... Smile
_________________
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
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 4 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