StrSplit() from right? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Spark
Posts: 80
Joined: 04 Jan 2017, 02:22

StrSplit() from right?

11 Jul 2018, 21:15

hello all,
is there any function for StrSplit() from right?

something like this

Code: Select all

input = E:\Folder 1\Sub Folder 2\Sub Folder 3\Sub Folder 4\picture.png
output := StrSplitRight(input ,\)
msgbox, % output[2] ; result : Sub Folder4\picture.png
thanks in advance
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: StrSplit() from right?

11 Jul 2018, 22:00

It can be done with regex but it is generally slower for simple string manipulation.

Example function:

Code: Select all

input := "E:\Folder 1\Sub Folder 2\Sub Folder 3\Sub Folder 4\picture.png"
MsgBox % StrSplitRight(input,"\",2)

StrSplitRight(string,delim,count) {
    for i in Arr := StrSplit(string,delim) {
        output := Arr.pop() . (A_Index > 1 ?  delim . output : "")
    } until (A_Index = Count)
    return output
}
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: StrSplit() from right?

11 Jul 2018, 22:01

Code: Select all

input := "E:\Folder 1\Sub Folder 2\Sub Folder 3\Sub Folder 4\picture.png"
output := StrSplit(input, "\")
msgbox % output[output.Length()-1]    ; Sub Folder 4
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: StrSplit() from right?

11 Jul 2018, 22:37

- StrSplit has a MaxParts parameter (since AHK v1.1.28+). E.g.

Code: Select all

;StrSplit MaxParts (v1.1.28+)
MsgBox, % StrSplit("a,b,c,d,e", ",",, 3)[3] ;c,d,e
- You can use that to achieve what you want:

Code: Select all

q:: ;get the last n array items, joined, via StrSplit MaxParts (v1.1.28+)
vPath := "E:\Folder 1\Sub Folder 2\Sub Folder 3\Sub Folder 4\picture.png"
StrReplace(vPath, "\",, vCount)
Loop, % vCount+1
{
	vNum := A_Index
	vOutput .= vNum "`t" StrSplit(vPath, "\",, vCount+2-vNum)[vCount+2-vNum] "`r`n"
}
Clipboard := vOutput
MsgBox, % vOutput
return
- Note: with the mathematics: if there are n delimiters, then there are n+1 parts.
- Also, there's this:
file get part (SplitPath alternative/short-form/long-form/correct case) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=47709
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Spark
Posts: 80
Joined: 04 Jan 2017, 02:22

Re: StrSplit() from right?

11 Jul 2018, 23:47

Xtra wrote:It can be done with regex but it is generally slower for simple string manipulation.

Example function:

Code: Select all

input := "E:\Folder 1\Sub Folder 2\Sub Folder 3\Sub Folder 4\picture.png"
MsgBox % StrSplitRight(input,"\",2)

StrSplitRight(string,delim,count) {
    for i in Arr := StrSplit(string,delim) {
        output := Arr.pop() . (A_Index > 1 ?  delim . output : "")
    } until (A_Index = Count)
    return output
}
this is what i'm looking for...thank you very much :bravo:

@Flipeador
thank you, very simple and elegant script..I'll need it for next project

@jeeswg
wow ..your script very detail.
but my logic not that expert, maybe i'll need that in the future


thank you for your help, have a good day
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: StrSplit() from right?

12 Jul 2018, 00:37

Xtra, this will not work for any count.
For loop wrote:Existing key-value pairs may be modified during the loop, but inserting or removing keys may cause some items to be skipped or enumerated multiple times
Example,

Code: Select all

for k in a:=[1,2,3,4,5,6]
	str .= a.pop() "`n" 
msgbox % str
A simpler version,

Code: Select all

StrSplitRight(string, delim, count){
	return substr(string, instr(string, delim,, -1, count) + 1)
}
Cheers.
Last edited by Helgef on 12 Jul 2018, 03:10, edited 1 time in total.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: StrSplit() from right?

12 Jul 2018, 00:39

- Here is some code using SubStr.

Code: Select all

q:: ;use SubStr to get the last n parts of a string
vPath := "E:\Folder 1\Sub Folder 2\Sub Folder 3\Sub Folder 4\picture.png"
vNum := 2
vIsV1 := !!InStr(1,1,1,0), vPos := vIsV1 ? -1 : 0
vPos := InStr(vPath, "\", 0, vPos, vNum)
MsgBox, % SubStr(vPath, vPos+1)
return

w:: ;use SubStr to get the last n parts of a string
vPath := "E:\Folder 1\Sub Folder 2\Sub Folder 3\Sub Folder 4\picture.png"
StrReplace(vPath, "\",, vCount)
vIsV1 := !!InStr(1,1,1,0)
Loop, % vCount+1
{
	vNum := A_Index
	vPos := vIsV1 ? -1 : 0
	vPos := InStr(vPath, "\", 0, vPos, vNum)
	vOutput .= vNum "`t" SubStr(vPath, vPos+1) "`r`n"
}
Clipboard := vOutput
MsgBox, % vOutput
return
- Btw with all techniques you should be aware re. what if you ask for the last n parts when there are fewer than n parts in the string.
- [EDIT:] Oh, just noticed, this is the 2018th thread I've appeared in. 2018, the current year!
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: StrSplit() from right?  Topic is solved

12 Jul 2018, 01:39

fixed:

Code: Select all

input := "E:\Folder 1\Sub Folder 2\Sub Folder 3\Sub Folder 4\picture.png"
MsgBox % StrSplitRight(input,"\",2)

StrSplitRight(string,delim,count) {
    Arr := StrSplit(string,delim)
    Loop % Arr.length() {
        output := Arr.pop() . (A_Index > 1 ?  delim . output : "")
    } until (A_Index = Count)
    return output
}
Spark
Posts: 80
Joined: 04 Jan 2017, 02:22

Re: StrSplit() from right?

12 Jul 2018, 01:57

@Xtra
whoaa ... thanks for making it perfect
Helgef wrote: A simpler version,

Code: Select all

StrSplitRight(string, delim, count){
	return substr(string, instr(string, "\",, -1, count) + 1)
}
Cheers.
I have a little question about your simpler version,
what is this "delim" for?

if i change it to be

Code: Select all

StrSplitRight(string, count){
	return substr(string, instr(string, "\",, -1, count) + 1)
is there any difference??

best regards
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: StrSplit() from right?

12 Jul 2018, 03:11

@ Spark, I fixed it.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, Fredrik F, garry, maxkill, RandomBoy, ShatterCoder and 402 guests