At() and Rat() return the same value on a character string

At() and Rat() return the same value on a character string

Postby Rick Lipkin » Wed May 15, 2024 8:51 pm

To All ( enrico )

I an using fivewin 2404 and xHarbour build 20240413 .. This code returns the same value

Code: Select all  Expand view  RUN

xValue := "Amy Halady"

cName := alltrim( xValue )
msginfo( cName )        // Amy Halady

nLen := len( cName )
msginfo( nLen )             //10

nAt := at(" ", cName )
msginfo( nAT )              // 4

nRat := rat( " ",cName )
msginfo( nRat )             // 4


 


Rat() is supposed to count backwards and the above rat() function returns 4 but should return 6 .. Hopefully someone can compile this simple code and verify my results

Thanks
Rick Lipkin
User avatar
Rick Lipkin
 
Posts: 2666
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: At() and Rat() return the same value on a character string

Postby Enrico Maria Giordano » Wed May 15, 2024 9:10 pm

No, 4 is the correct result. The difference between AT() and RAT() is that RAT() search from right to left but the position is always counted from left. Anyway, Harbour shows the same result as xHarbour.
User avatar
Enrico Maria Giordano
 
Posts: 8725
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: At() and Rat() return the same value on a character string

Postby Rick Lipkin » Thu May 16, 2024 2:29 pm

Enrico

cName :- "Amy Halady"

What I expect nAt := at( " ", cName ) scans forward from left to right so starting with "Amy " so space is the 4th character
nRat := rat( " ", cName ) scans backwards from right to left starting with " Hadley" so the space is the 6th character
nLen := len( cName ) = 10

What am I missing ... ?? What I am trying to do is split the name above into First Name and Last name .. using the space(s) as the delimiter between the first name and the last name but sometimes the space may not be always be just one space, it may be two spaces like "Amy Hadley" so now I need to know counting backwards where the end of the space may be .. So in order to get the correct results I need to know where the space starts and where the space ends, hence the use of the At() and Rat() function ...

Is there a better way ... of knowing where the space starts and where the space ends in a string ??

Thanks
Rick
User avatar
Rick Lipkin
 
Posts: 2666
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: At() and Rat() return the same value on a character string

Postby Enrico Maria Giordano » Thu May 16, 2024 2:36 pm

Rick Lipkin wrote:Enrico

cName :- "Amy Halady"

What I expect nAt := at( " ", cName ) scans forward from left to right so starting with "Amy " so space is the 4th character
nRat := rat( " ", cName ) scans backwards from right to left starting with " Hadley" so the space is the 6th character


There is only one space in the string "Amy Halady", so you get the same result from AT() and RAT(). Try with the string "Enrico Maria Giordano" and you will get 7 with AT() and 13 with RAT().
User avatar
Enrico Maria Giordano
 
Posts: 8725
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: At() and Rat() return the same value on a character string

Postby cmsoft » Thu May 16, 2024 2:50 pm

Rick
Podrias hacer
Code: Select all  Expand view  RUN

nPos := Len(cName)- rAt( " ",cName)
 

De esta manera obtienes cuantos caracateres hay por detras del caracter buscado
User avatar
cmsoft
 
Posts: 1293
Joined: Wed Nov 16, 2005 9:14 pm
Location: Mercedes - Bs As. Argentina


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 88 guests

cron