Slater Posted June 13 Posted June 13 Hi Everyone, I guess I'm doing something stupid! I'm trying to find a character in a string, working from the RHS. Position ( "123456789" ; "7" ; 9 ; -1 ) returns the value 7. And I had thought that it would return 3, as starting from the RHS (due to the -1) 7 is the 3rd character. Position ( "123456789" ; "7" ; 1 ; 1 ) gives me 7 Position ( "123456789" ; "7" ; 1 ; -1 ) give me 0 Position ( "123456789" ; "7" ; 9 ; 1 ) gives me 0 Checking it with different numbers, Position ( "123456789" ; "2" ; 9 ; -1 ) returns 2. Again, I had thought that it would give me 7 as that's the 7th digit from the RHS Can someone tell me what the function should be so that it counts from the RHS? Many thanks
comment Posted June 13 Posted June 13 6 minutes ago, Slater said: I had thought that it would return 3, as starting from the RHS (due to the -1) No, the sign of the occurrence parameter only tells the engine in which direction from the start position to search. Once a match is found, the function returns its position in the given text - regardless of how it was found. If you want the position from right, you need to calculate it yourself using Position and Length. The exact formula would depend on how exactly you want to count, but perhaps: Length ( text ) - Position ( text ; searchString ; start ; occurrence ) - Length ( searchString ) + 2 1
Slater Posted June 14 Author Posted June 14 Comment, now that makes total sense! So it's always about the position of the character from the LHS even if you're counting from the RHS. Thank you so much, such a simple answer but one that caused me so much head scratching! 13 hours ago, comment said: No, the sign of the occurrence parameter only tells the engine in which direction from the start position to search. Once a match is found, the function returns its position in the given text - regardless of how it was found
comment Posted June 14 Posted June 14 14 minutes ago, Slater said: even if you're counting from the RHS You are not counting from right; you are only scanning from right. You always count from the beginning of the text. Not sure what your exact use case is, but you could start by reversing the text and then everything would work in the opposite direction. So if the original text were "123456789", then Position ( reversedText ; "765" ; 1 ; 1 ) would return 3. Note that the searchString is also reversed: the pattern "765" does not occur at all in the original text. 1
Slater Posted June 14 Author Posted June 14 7 minutes ago, comment said: You are not counting from right; you are only scanning from right. You always count from the beginning of the text. Yes, in my regular language counting from and scanning are more or less the same thing. Apologies, I should have said scanning from the right
comment Posted June 14 Posted June 14 No need to apologize, I just wanted to make it extra clear in case someone else is reading this. The term "scan" is actually used in the help for this function: Quote The scan begins at the start value and, if occurrence is positive, continues toward the end of the text string; if occurrence is negative, the scan continues toward the beginning of the text string. https://help.claris.com/en/pro-help/content/position.html Note that the same principle applies to the start parameter too. Both: Position ( "123456789" ; "7" ; 1 ; 1 ) and: Position ( "123456789" ; "7" ; 5 ; 1 ) return 7. Because you always count from the beginning of the text, regardless of where you started and in which direction you searched. 1 1
LaRetta Posted June 14 Posted June 14 This is a precious nugget of which I was unaware and it makes me smile! Thank you, Michael. 🤠
Ocean West Posted June 14 Posted June 14 7 hours ago, comment said: The scan begins at the start value and, if occurrence is positive, continues toward the end of the text string; if occurrence is negative, the scan continues toward the beginning of the text string. https://help.claris.com/en/pro-help/content/position.html I had no idea that's fantastic.
Søren Dyhr Posted June 16 Posted June 16 On 6/14/2024 at 5:21 PM, Ocean West said: I had no idea that's fantastic. Huh ... I seems to remember this as question in FM-certification test, which I admittedly flunked! --sd
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now