String Things - Stand-alone string manipulation functions

Post your working scripts, libraries and tools for AHK v1.1 and older
Verdlin
Posts: 63
Joined: 04 Oct 2013, 08:55
Contact:

Re: String Things - Stand-alone string manipulation function

29 May 2014, 04:35

Thanks, Tidbit!

@guest3456: Yes, those first braces are unnecessary. It's there because I paste all of my examples from a scratchpad function. It's doesn't hurt to include them in posts, and it is less work for me to remove braces and indentation.

@AfterLemon: I considered that at first, too. The particular case where I find this function beneficial is outlined below. It saves a little bit of coding headache and also reduces potential for small errors.

Shown below, I use this type of on-the-fly debugging on a daily basis, and the concat function makes my life a little easier...

Code: Select all

{
	; Consider...
	Loop 5
		iVar%A_Index% := A_Index
	Msgbox % st_concat("`n", iVar1, iVar2, iVar3, iVar4, iVar5)
	; vs...
	Msgbox % iVar1 "`n" iVar2 "`n" iVar3 "`n" iVar4 "`n" iVar5
	; Note this is moot with arrays.
	ai := []
	Loop 5
		ai[A_Index] := A_Index
	Msgbox % st_concat("`n", ai*)
	; vs...
	Msgbox % st_glue(ai)
	return
}
User avatar
tidbit
Posts: 1272
Joined: 29 Sep 2013, 17:15
Location: USA

Re: String Things - Stand-alone string manipulation function

30 May 2014, 14:59

2.6 is out.
I've been slacking and not paying attention to what I've changed/added/removed.
All I can remember is I just added st_concat() and have an alternative (better, but harder to read) version of st_printArr().
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
sv270190
Posts: 45
Joined: 06 Feb 2014, 11:48

Re: String Things - Stand-alone string manipulation function

15 Aug 2014, 08:56

dear sir

what are the functions in which i can use the entire file and how could i get the output file

for eg st_insertline st_deleteline how can i use the entire file

i.e i want the 3rd of input.txt to be inserted with "new 3rd line" so that the original 3rd line is now the fourth line.

kindly guide me what are all the other functions in which the operation is conducted in the entire file
S.V. SRINIVASAN
SRIVILLIPUTTUR
TAMIL NADU
User avatar
tidbit
Posts: 1272
Joined: 29 Sep 2013, 17:15
Location: USA

Re: String Things - Stand-alone string manipulation function

15 Aug 2014, 11:09

kindly guide me
you will need these commands and steps:

1. Read the file into a variable: http://ahkscript.org/docs/commands/FileRead.htm

2. Use this function from String Things to insert stuff where you want: giraffe:=st_insertLine("Pandas cannot fly", Variable, 3). %giraffe% will contain the new text.

3. Delete the original file from step [1] using: http://ahkscript.org/docs/commands/FileDelete.htm

4. Re-create the original file with the new text stored in %giraffe% using: http://ahkscript.org/docs/commands/FileAppend.htm

Note: I color-coordinated important stuff for you to easily study/learn.
I would suggest testing on a backup version of your file(s).
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
sv270190
Posts: 45
Joined: 06 Feb 2014, 11:48

Re: String Things - Stand-alone string manipulation function

17 Aug 2014, 00:08

thank you for your immediate response/guidance
may i conclude that the subject file be read to a variable and that we can use string function / operation to the entire file

i will test and revert to you.
S.V. SRINIVASAN
SRIVILLIPUTTUR
TAMIL NADU
User avatar
tidbit
Posts: 1272
Joined: 29 Sep 2013, 17:15
Location: USA

Re: String Things - Stand-alone string manipulation function

17 Aug 2014, 10:14

Working directly with files is not easy in AHK. There is already a library for that, TF.ahk. But iirc, it needs to do the same steps as I mentioned above (read, delete, build, rewrite). And it's massive. And with TF() you can't simply copy/paste the functions you need into your code and ignore everything else. You'll need all 1300+ lines.

so with ST(), you can remove the 1300 lines and just add 4 lines to read, delete, build then rewrite. Only if you need them.
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
User avatar
Stevoisiak
Posts: 12
Joined: 24 Aug 2017, 14:19
Contact:

Re: String Things - Stand-alone string manipulation functions

01 Sep 2017, 12:44

Any particular reason to host this on DropBox and not GitHub? (My workplace blocks access to DropBox due to security concerns)
User avatar
runie
Posts: 304
Joined: 03 May 2014, 14:50
Contact:

Re: String Things - Stand-alone string manipulation functions

01 Sep 2017, 17:38

Stevoisiak wrote:Any particular reason to host this on DropBox and not GitHub? (My workplace blocks access to DropBox due to security concerns)
tidbit really likes dropbox.
User avatar
tidbit
Posts: 1272
Joined: 29 Sep 2013, 17:15
Location: USA

Re: String Things - Stand-alone string manipulation functions

01 Sep 2017, 20:26

dropbox is nice and easy. git is annoying. I'll put it on pastebin instead.
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
User avatar
Stevoisiak
Posts: 12
Joined: 24 Aug 2017, 14:19
Contact:

Re: String Things - Stand-alone string manipulation functions

06 Sep 2017, 10:14

I may be mistaken, but I didn't see any way of handling 2D arrays with st_split, such as ones found in CSV files.

Based on code by Oleg, here's a function for splitting a delimited string to a 2D array.

Code: Select all

; Split delimited string to a 2D array
; Adapted from code by Oleg (https://stackoverflow.com/a/45621807/3357935)
ad_split2D(unsplitString, separator:=",")
{
   local lines, columns, index, value  ; prevent scope warnings
   lines := StrSplit(unsplitString, "`n")
   columns := []
   for index, value in lines
      columns.Insert(StrSplit(value, separator))
   Return columns
}
Example usage

Code: Select all

jobString = 
(
Unit,Dept_ID,Name
CORP,0368,Admin
CORP,3945,Programmer
SESHAN,4596,Software Engineer
)

jobArray2D = ad_split2D(jobString, ",")
User avatar
tidbit
Posts: 1272
Joined: 29 Sep 2013, 17:15
Location: USA

Re: String Things - Stand-alone string manipulation functions

06 Sep 2017, 10:42

st_split is useless now. ahk finally has it built-in, strsplit()
then you work on it (either my function or the returned var of strsplit()) the same way you use arrays. arr[blah] arr[1] arr[5] etc

But I guess you knew that since you used it :P
yours does save a step for multi-lined inputs, though. neat.
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
joefiesta
Posts: 494
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: String Things - Stand-alone string manipulation functions

14 Jan 2018, 18:03

Great library. Thanks

Question about St_Pad:

I found StrPad() function in the old forum by Majkinetor. But, it doesn't work. The character padded is not the correct character. I have to admit that I like the fact that it does not use a loop. It was found here: https://autohotkey.com/board/topic/2418 ... ntry156593
here is Majkinetor's StrPad():

Code: Select all

StrPad(str, padchar, padlen, left=1){
               if (i := padlen-StrLen(str))
                VarSetCapacity(w, i, asc(padchar)),  NumPut(0, &w+i, "Char"),  VarSetCapacity(w, -1)
        return left ? w str : str w
}
Can anyone get this to work?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: String Things - Stand-alone string manipulation functions

14 Jan 2018, 23:08

Perhaps look at SLICE STRING / PAD STRING, here:
jeeswg's RegEx tutorial (RegExMatch, RegExReplace) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=28031

- [EDIT:] I've looked at the function, and it pads only on one side of a string, not within a string.
- It was written for AutoHotkey Basic, and works in AutoHotkey ANSI, I've rewritten the function in 2 different ways, both work with both AutoHotkey ANSI and AutoHotkey Unicode:

Code: Select all

MsgBox, % StrPad("some text ", "-", 20)
MsgBox, % StrPad2("some text ", "-", 20)

StrPad(str, padchar, padlen, left=1)
{
 	if (i := padlen-StrLen(str))
		VarSetCapacity(w, i*2+2, 1), NumPut(0, &w+(i<<!!A_IsUnicode), "UShort"), VarSetCapacity(w, -1)
	w := StrReplace(w, Chr(A_IsUnicode?257:1), padchar)
	return left ? w str : str w
}

StrPad2(str, padchar, padlen, left=1)
{
 	if (i := padlen-StrLen(str))
		w := StrReplace(Format("{:" i "}", ""), " ", padchar)
	return left ? w str : str w
}
- [EDIT:] Note: it's possible to fill binary data with consecutive bytes with the value 1, reading the data as ANSI: individual bytes are seen as Chr(1), reading the data as UTF-16: pairs of bytes are seen as Chr(257) (i.e. 0x0101 (little endian) = 1+256 = 257).
- [EDIT:] In short, VarSetCapacity is used with FillByte, or StrReplace and Format, as workarounds to repeat a string, and to avoid using Loop. I have asked for a StrRept function to be added to AutoHotkey in my Wish List 2.0.
Wish List 2.0 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 13&t=36789
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
iPhilip
Posts: 796
Joined: 02 Oct 2013, 12:21

Re: String Things - Stand-alone string manipulation functions

15 Jan 2018, 15:03

Hi tidbit,

I noticed a problem with the ST_Insert() function when pos < 0. See the example below, which includes a simplified version of the function that fixes the problem.

Code: Select all

#NoEnv

insert := "-"
input := "123456789"
for key, val in [1,2,3,4,5,6,7,8,9,10,0,-1,-2,-3,-4,-5,-6,-7,-8,-9,-10]
   MsgBox % "Position`t`t= " val "`nST_Insert Result`t= " st_insert(insert, input, val) "`nST_Insert2 Result`t= " st_insert2(insert, input, val)

ST_Insert(insert,input,pos=1)
{
	Length := StrLen(input)
	((pos > 0) ? (pos2 := pos - 1) : (((pos = 0) ? (pos2 := StrLen(input),Length := 0) : (pos2 := pos))))
	output := SubStr(input, 1, pos2) . insert . SubStr(input, pos, Length)
	If (StrLen(output) > StrLen(input) + StrLen(insert))
		((Abs(pos) <= StrLen(input)/2) ? (output := SubStr(output, 1, pos2 - 1) . SubStr(output, pos + 1, StrLen(input))) : (output := SubStr(output, 1, pos2 - StrLen(insert) - 2) . SubStr(output, pos - StrLen(insert), StrLen(input))))
	return, output
}

ST_Insert2(insert,input,pos=1) {
   if (!pos || pos > StrLen(input))
      return input insert
   else
      return RegExReplace(input, ".", insert "$0", , 1, pos < 0 ? pos+1 : pos)
}
Cheers!
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
iPhilip
Posts: 796
Joined: 02 Oct 2013, 12:21

Re: String Things - Stand-alone string manipulation functions

16 Jan 2018, 12:29

Using jeeswg's clever approach, st_pad() can be reduced to a on-liner :)

Code: Select all

st_pad(string, left="0", right="", LCount=1, RCount=1) {
   return StrReplace(Format("{:" LCount "}", ""), " ", left) string StrReplace(Format("{:" RCount "}", ""), " ", right)
}
Cheers!
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: String Things - Stand-alone string manipulation functions

16 Jan 2018, 12:54

Hi tidbit, I have looked over these functions occasionally, very intrigued. Well written and documented. I notice you use recursion, especially for the array to string functions. Nice. :salute:

I am curious why you have some parameters like indentLevel in st_printArr(array, depth=5, indentLevel="") that you say "DO NOT TOUCH." Is that to save space and lines? Rather than declaring the variable on its own line?

Like this:

Code: Select all

st_printArr(array, depth=5) ; st_printArr(array, depth=5, indentLevel="")
indentLevel:=""
{
   for k,v in Array
   {
      list.= indentLevel "[" k "]"
      if (IsObject(v) && depth>1)
         list.="`n" st_printArr(v, depth-1, indentLevel . "    ")
      Else
         list.=" => " v
      list.="`n"
   }
   return rtrim(list)
}
try it and see
...

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: Auntiejack56 and 125 guests