notation: parameter that specifies delimiters/separators Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
jeeswg
Posts: 6371
Joined: 19 Dec 2016, 01:58
Location: UK

notation: parameter that specifies delimiters/separators

24 Jan 2018, 07:57

- The Sort command is a good example of what I consider a tricky issue.
- I think it uses a good solution, but it doesn't (try to) solve all of the possible common issues.
- I'll use the AHK v2 Sort function syntax to make things clearer.

Code: Select all

vOutput := Sort(vText, "D,") ;use comma as a delimiter
vOutput := Sort(vText, "Dx") ;use x as a delimiter
- This does not allow you to specify no delimiter (i.e. treat each character as an individual item). Perhaps 'XD' or 'AD' see below.
- This does not allow you to specify no delimiter (i.e. treat the string as one item, this is not useful for Sort, but could be useful for other functions). Perhaps 'AD00' (see below) (since a string won't contain nulls).
- This does not allow you to specify a multiple-character delimiter. E.g. for CRLFs, perhaps AD1310/UD13001000/WD13001000 'ASCII/ANSI delimiter 13 10'/'Unicode/wide delimiter 1300 1000', using a hex representation of the string.
- [EDIT:] Another thing to consider is that option parameters are typically parsed based on spaces/tabs.
- [EDIT:] A possible simple solution would be something like ZD|string|, where ZD would mean that the first character after ZD marks the start of the string, and when that character appears again marks the end of the string.

- Quite often in functions, I'd actually want to specify 4 delimiters: separator/line break string in input/output. (See simple example lower down.) E.g. how to interpret an input/needle/list/ini-style string, and how to present the output text.
- Anyway, the question is how to specify 4 delimiters, any string, of any length (or no delimiters, or all characters separated), in the most succinct way possible.
- A standard system that could be used with all of the functions that require delimiters/separators.
- A possible solution is to add separate delimiter parameters, but it may be better to avoid this, and have one options parameter where you specify everything (delimiters, separators, and everything else).
- Another possibility is to have an option parameter that usually expects a string, but that can accept an object if necessary.
- Or there could be a separate parameter that handles a 'delimiter object'.
- This issue repeatedly comes up when writing functions.
- Thanks for reading.

Code: Select all

;example of a simple function that needs to handle 4 delimiters/separators
;this function just performs StrReplace twice, but other functions would be more complex
MsgBox, % vText := "a:1,b:2,c:3"
MsgBox, % ChangeSep(vText, ":", ",", "`t", "`r`n")
oOpt := {Delim:":", DelimLn:",", Sep:"`t", SepLn:"`r`n"}
MsgBox, % ChangeSep2(vText, oOpt)

ChangeSep(vText, vDelim, vDelimLn, vSep, vSepLn)
{
	vText := StrReplace(vText, vDelim, vSep)
	vText := StrReplace(vText, vDelimLn, vSepLn)
	return vText
}

ChangeSep2(vText, oOpt)
{
	vText := StrReplace(vText, oOpt.Delim, oOpt.Sep)
	vText := StrReplace(vText, oOpt.DelimLn, oOpt.SepLn)
	return vText
}
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
jeeswg
Posts: 6371
Joined: 19 Dec 2016, 01:58
Location: UK

Re: notation: parameter that specifies delimiters/separators  Topic is solved

09 May 2018, 15:17

A solution, as specified here:
your personal AutoHotkey style guide - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 28#p212128
[solution: a delimiter/separator parameter could accept one string or a linear array, just like StrSplit][it's often a sensible thing to do, to have a separate delimiter/separator parameter (instead of it being one item in a comma-separated list as in the Sort command)]
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: stargate and 140 guests