Search conducted from right to left? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
FredOoo
Posts: 186
Joined: 07 May 2019, 21:58
Location: Paris

Search conducted from right to left?

Post by FredOoo » 25 May 2022, 05:54

Code: Select all

	Haystack := "The Quick Brown Fox Jumps Over the Lazy Dog"
	Needle   := "Fox"
	p1  := inStr(Haystack, Needle, false, +1) ; 17
	p2  := inStr(Haystack, Needle, false, -1) ; 27 ★ ???
	; If Occurrence is omitted, a negative StartingPos causes the search to be conducted from right to left.
	msgBox p1 "`n" p2
I get p1 and p2 = 17
Maybe something I misunderstood?
(Alan Turing) « What would be the point of saying that A = B if it was really the same thing? »
(Albert Camus) « Misnaming things is to add to the misfortunes of the world. »

User avatar
boiler
Posts: 16932
Joined: 21 Dec 2014, 02:44

Re: Search conducted from right to left?  Topic is solved

Post by boiler » 25 May 2022, 06:09

The position where "Fox" is found is supposed to be 17 in that string no matter which direction the search starts from because there is only one instance of that word. Starting the search from the end doesn’t mean it will count how many characters it is from the end. It means it will find the first match when starting from that end. You’ll see the difference if you change the needle to "the" because it will find the first instance at position 1 when starting from the beginning of the string and the last instance at position 32 when starting from the end.

User avatar
FredOoo
Posts: 186
Joined: 07 May 2019, 21:58
Location: Paris

Re: Search conducted from right to left?

Post by FredOoo » 25 May 2022, 06:38

hum… ok, I see.
Thanks
(Alan Turing) « What would be the point of saying that A = B if it was really the same thing? »
(Albert Camus) « Misnaming things is to add to the misfortunes of the world. »

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

Re: Search conducted from right to left?

Post by Helgef » 25 May 2022, 07:43

instr - Return value wrote: Regardless of the values of StartingPos or Occurrence, the return value is always relative to the first character of Haystack. For example, the position of "abc" in "123abc789" is always 4.

Post Reply

Return to “Ask for Help (v2)”