Jump to content
Claris Engage 2025 - March 25-26 Austin Texas ×

Can't get Position to work for me in a field - counting from RHS


Recommended Posts

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

Link to comment
Share on other sites

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

 

  • Like 1
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

  • Thanks 1
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

 

  • Like 1
  • Plus1 1
Link to comment
Share on other sites

This is a precious nugget of which I was unaware and it makes me smile!  Thank you, Michael. 🤠

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.