Need to replace every Nth occurrence in a list [SOLVED]

Get help with using AutoHotkey and its commands and hotkeys
vahju
Posts: 27
Joined: 30 Sep 2013, 16:09

Need to replace every Nth occurrence in a list [SOLVED]

12 Apr 2014, 06:48

Solved see this post below.

============

So I have tried the code here but its not working.

Code: Select all

Haystack := clipboard
Haystack := RegExReplaceInc(Haystack,"`r`n",",")  :this works
MsgBox % RegExReplaceInc(Haystack,",","`r`n`r`n`r`n",1,5)  ;add 3 lines to break into groups - not working
return
I have a list of emails addresses which can get very long. I need to break them up into groups of 250. I can do the StringReplace changing `r`n to ", " but I am having problems after that breaking into groups. Each email has to be separated by a comma then a space.

Our email system can't handle that many email addresses on one line so it has to be broken out into groups of 250.

Any help would be appreciated. I am not sure that older code even works and I am not good at regex.
Last edited by vahju on 15 Apr 2014, 20:42, edited 1 time in total.
Guest

Re: Need to replace every Nth occurrence in a list

12 Apr 2014, 07:10

I don't think RegExReplaceInc() is suitable for this task as it seems to be useful for inserting number in sequence not split strings. What is on the clipboard? one long line of 1000s of addresses in CSV format? Why not simply parse the string

Haystack := "1,2,3,4,5,6,7,8,9,0"

fakeArray:=""
counter:=1
loop, parse, Haystack, CSV
{
If mod(a_index,3) = 0 ; change 3 to 250
counter++
fakeArray%counter% .= A_LoopField ","
}

loop % counter
MsgBox % trim(fakeArray%A_Index%,",")
vahju
Posts: 27
Joined: 30 Sep 2013, 16:09

Re: Need to replace every Nth occurrence in a list

12 Apr 2014, 16:27

smörgåsbord that worked great. I had to adjust the code to trim off the dangling comma and space.

Code: Select all


loop, parse, Clipboard, `r`n
{
  list .= A_loopfield . ", "
  if ( mod(A_index, 5) ==  0 )
  {
    StringTrimRight, list, list, 2
	list := list . "`n"
  }
}
StringTrimRight, list, list, 2
;MsgBox % list
Clipboard := list
User avatar
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

Re: Need to replace every Nth occurrence in a list

12 Apr 2014, 20:56

That would be 43$
4 for me and 3 for guest.
:mrgreen: :mrgreen:
John ... you working ?
vahju
Posts: 27
Joined: 30 Sep 2013, 16:09

Re: Need to replace every Nth occurrence in a list

13 Apr 2014, 08:19

Seems I spoke too soon.
This works great from the example and from copying email from a text file.
But if I copy directly from excel (which will happen a majority of the time) a extra character gets added and throws off the grouping.

Example of output with grouping of 5:
User avatar
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

Re: Need to replace every Nth occurrence in a list

13 Apr 2014, 08:53

Code: Select all


stringsplit, email, clipboard, `n`r
loop % email0
{
if ( email%A_index% != "" )
list .= email%A_index% . ", "
}

list := RegExReplace(list, ", \Z|((?:, .*?){2}),[ ]", "$1`n") ; courtesy @sluggr #regex Freenode
MsgBox % list
exitapp
change 2 to 5 or 250
John ... you working ?
User avatar
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

Re: Need to replace every Nth occurrence in a list

13 Apr 2014, 12:00

see above post
John ... you working ?
vahju
Posts: 27
Joined: 30 Sep 2013, 16:09

Re: Need to replace every Nth occurrence in a list

15 Apr 2014, 20:39

Smörgåsbord and guest thanks for the help/examples.

I could not get the regex example code to work properly and it kept grouping items 1 less then the number I provided. So I took your examples and came up with something much longer but it works and I can understand it to fix it.

That mod function helped me group the items and that was the hardest part in figuring this out.
Below is the code that works with copying emails from a column from excel or from copying emails to clipboard.

Code: Select all

cnt := 0
list := 

;cleans up list and removes trailing null value ("") from excel
loop, parse, clipboard, `r`n
{
	If (a_loopfield != "")
	{
		list .= a_loopfield . ","
		cnt += 1
	}
}

clipboard := list
sleep, 200
list := 

;formats list and breaks up into groups
;change number in mod line below to adjust group size
loop, parse, clipboard, `,
{
	if (cnt <> a_index)  ;check for last line in the list
		list .= A_loopfield . ", "
	else ;hit last line so skip adding comma space and stop loop
	{
		list .= a_loopfield
		break
	}
	if ( mod(A_index, 250) ==  0 )  ;change 250 to specified group size
	{
		StringTrimRight, list, list, 2  ;removes extra comma space at end of each group
		list := list . "`n"
	}   
}
Clipboard := list
sleep, 200
cnt :=
list :=
User avatar
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

Re: Need to replace every Nth occurrence in a list [SOLVED]

15 Apr 2014, 21:56

.mm.. that means less money for us.
anyways that would be 13$
1 for me and 3 for guest.

btw: you could have used 251 instead, it is great that you solved some part on your own. solving yourself gives immense pleasure.

for instant help join
IRC
#ahkscript
freenode. ( irc.freenode.net) ; server
port : 6667

any further details/amendments in case need be.
John ... you working ?

Return to “Ask For Help”

Who is online

Users browsing this forum: bautai, Bing [Bot], punchin, scriptor2016, sobuj53, TAC109, Xeo786 and 32 guests