Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Keeping a part of a String from reversion



  • Please log in to reply
7 replies to this topic
Redo121
  • Members
  • 4 posts
  • Last active: Feb 27 2016 10:01 PM
  • Joined: 06 Jul 2015

Straight to the topic: I want to make a text reversion script, which can look for and omit a part of the string from reversing.

 

Say my string looks like "I found a very [shiny] coin.", and I want to keep "[shiny]" from getting reversed, unlike rest of the string.

I already have a text reversion script, I only wonder if it can be reworked in that way? And if that action can be repeated for each "[...]" found in a string?

 

To clearly explain what I'm trying to do, I want "I [found] a very [shiny] coin." to become ".nioc [shiny] yrev a [found] I".



kon
  • Members
  • 1652 posts
  • Last active:
  • Joined: 04 Mar 2013
test := "I found a very shiny coin."
MsgBox, % Reverse(test)	;".nioc [shiny] yrev a [found] I".


Reverse(s) {
	static ignore := {found: true, shiny: true}
	m := "", p := 1
	while p := RegExMatch(s, "(\b[\w']+\b)([\W]*)", m, p + StrLen(m)) {
		if (ignore[m1])
			Out := m2 . m1 . Out
		else
			Loop, Parse, m
				Out := A_LoopField . Out
	}
	return Out
}


Redo121
  • Members
  • 4 posts
  • Last active: Feb 27 2016 10:01 PM
  • Joined: 06 Jul 2015
test := "I found a very shiny coin."
MsgBox, % Reverse(test)	;".nioc [shiny] yrev a [found] I".


Reverse(s) {
	static ignore := {found: true, shiny: true}
	m := "", p := 1
	while p := RegExMatch(s, "(\b[\w']+\b)([\W]*)", m, p + StrLen(m)) {
		if (ignore[m1])
			Out := m2 . m1 . Out
		else
			Loop, Parse, m
				Out := A_LoopField . Out
	}
	return Out
}

Well, that one was just an example, and I'd love any sentence to be reversable that way, with exception of words enclosed in [...].

 

[EDIT]

By the way:

Spoiler

 

[EDIT2]

What I managed to do (probably very unoptimized but well, I don't really care that much about speed and I don't know much about more complicated aspects of AHK), now can protect one [...] from getting reversed, rest is still not protected.

Spoiler

Note: using the reversion hotkey multiple times on same sentence makes words in [...] (both those on which the protection works and the other ones) get repeated? o.O



kon
  • Members
  • 1652 posts
  • Last active:
  • Joined: 04 Mar 2013

I misunderstood the question. I thought you were using [] to denote words you would add to a list of words to skip, as opposed to ignoring every word that is literally enclosed in []. Try this:

^+Enter::
ClipboardSave := ClipboardAll
Clipboard := ""
Send, ^x
ClipWait, 1
if (!ErrorLevel) {
	reversed := Reverse(Clipboard)
	Clipboard := ""
	Clipboard := reversed
	ClipWait, 1
	if (!ErrorLevel) {
		Send, ^v
		Sleep, 200
	}
}
Clipboard := ClipboardSave
return

Reverse(s) {
	m := "", p := 1
	while p := RegExMatch(s, "(\[?)([\w']+\b)(]?)([\W^]]*|$)", m, p + StrLen(m)) {
		if (A_Index = 1)
			Out .= SubStr(s, 1, p - 1)
		if (m1 && m3) ; if word is enclosed in "[]"
			Out := m4 . m1 . m2 . m3 . Out
		else
			Loop, Parse, m
				Out := A_LoopField . Out
	}
	return Out
}


Redo121
  • Members
  • 4 posts
  • Last active: Feb 27 2016 10:01 PM
  • Joined: 06 Jul 2015

Works almost perfectly - almost, because it sometimes "eats" spaces in combinations ", [...]" and "consumes" ?, !, etc. when placed after [...] with over one word inside.

But shut up Redo, you got what you wanted, didn't you :)



kon
  • Members
  • 1652 posts
  • Last active:
  • Joined: 04 Mar 2013

Here's another version of the Reverse function that may work better for the special cases you mentioned.

Reverse(s) {
	m := "", p := 1, A := []
	while p := RegExMatch(s, "\[.+?]", m, p + StrLen(m))   ; find all the words enclosed in "[]" and inset them into an array
		A.Push(m)
	s := RegExReplace(s, "\[.+?]", ">@redlohecalp<")       ; replace all the words enclosed in "[]" with a placeholder
	Loop, Parse, s                                         ; reverse the string
		Out := A_LoopField . Out
	Loop, % A.MaxIndex()                                   ; re-insert the words from the array into the string
		Out := StrReplace(Out, "<placeholder@>", A.Pop(),, 1)
	return Out
}


Redo121
  • Members
  • 4 posts
  • Last active: Feb 27 2016 10:01 PM
  • Joined: 06 Jul 2015

So far so good, no issues occurred. In case of finding one I'll contact you(?) or more likely - I'll try to fix it myself.

Thanks! :)
 

![true] erew [globe] siht no gnivil [people] [nice] tuoba [Legends]



kon
  • Members
  • 1652 posts
  • Last active:
  • Joined: 04 Mar 2013
✓  Best Answer

So far so good, no issues occurred. In case of finding one I'll contact you(?) or more likely - I'll try to fix it myself.

Thanks! :)
 

![true] erew [globe] siht no gnivil [people] [nice] tuoba [Legends]

Glad to hear it's working :)

 

I realized there wasn't any real need for the placeholder... here's a slightly simplified version.

Reverse(s) {
	m := "", p := 1, A := []
	while p := RegExMatch(s, "\[.+?]", m, p + StrLen(m))   ; find all the words enclosed in "[]" and inset them into an array
		A.Push(m)
	Loop, Parse, s                                         ; reverse the string
		Out := A_LoopField . Out
	m := "", p := 1
	while p := RegExMatch(Out, "].+?\[", m, p + StrLen(m)) ; replace any words enclosed in "][" with the word stored in the array
		Out := StrReplace(Out, m, A.Pop(),, 1)
	return Out
}