InStrRev not working as expected - bug? 
Author Message
 InStrRev not working as expected - bug?

The relevant code snippet

Function TidyUpName(Name)
Dim strFirstName, strLastName
Dim intFirstNameMark, intLastNameMark
Dim arrNames

Name = Trim(Name)
Wscript.Echo Name
If Instr(Name, ",") Then
        intLastNameMark = Instr(Name, ",")
        strLastName = Left(Name, intLastNameMark - 1)
        intFirstNameMark = InstrRev(Name, ",")
        strFirstName = Right(Name, intFirstNameMark - 1)
        strFirstName = Trim(strFirstName)
        TidyUpName = strFirstName & " " & strLastName
Else
        arrNames = Split(Name)
        strLastName = arrNames(0)
        strFirstName = arrNames(1)
        TidyUpName = Trim(strFirstName & " " & strLastName)
End If

End Function

The string parameter Name in the data source I am parsing is of the
form DAY, ROBERT or sometimes DAY ROBERT (no comma - hence the Else).
The bug I am finding is that Instr AND InstrRev are returning the same
value on all records and this cannot be right (except on records with
equal numbers of characters either side of the comma)

I even ended up echoing the results of each line of

intLastNameMark = Instr(Name, ",")
strLastName = Left(Name, intLastNameMark - 1)
intFirstNameMark = InstrRev(Name, ",")
strFirstName = Right(Name, intFirstNameMark - 1)

to the console and this confirmed my findings above.

I'm baffled. Anyone else experienced this?

Robert

ps I finally sidestepped the issue by swapping

intFirstNameMark = InstrRev(Name, ",")

with

intFirstNameMark = (len(Name) - intLastNameMark)

pps I can post full script if necessary.



Mon, 28 Nov 2005 04:57:15 GMT  
 InStrRev not working as expected - bug?

Quote:
>The bug I am finding is that Instr AND InstrRev are returning the same
>value on all records and this cannot be right (except on records with
>equal numbers of characters either side of the comma)

Wrong. InStr and InStrRev both return the position in the string, not the
distance from starting end. So if you use both to locate the same comma, you
will get the same position.
--
Dave "Crash" Dummy - A weapon of mass destruction

http://lists.gpick.com


Mon, 28 Nov 2005 06:07:10 GMT  
 InStrRev not working as expected - bug?

Quote:

> Wrong. InStr and InStrRev both return the position in the string, not the
> distance from starting end. So if you use both to locate the same comma, you
> will get the same position.

Ah, that explains it. And if I had bothered to read my "VBScript in a
Nutshell" more closely that would have made it clear too. Nonetheless,
it strikes me as more intuitive for it to return the starting position
of it from the end of the string, as it were. I guess that's what the
Split function is for.

Thanks.

Robert



Mon, 28 Nov 2005 23:00:53 GMT  
 InStrRev not working as expected - bug?
Hi,

It's very helpful to have a quick "online" reference for questions like
those.  The free MS searchable CHM file download with basic descriptions and
syntax for both VBS and JS is here (urls will wrap):

http://www.microsoft.com/downloads/details.aspx?FamilyID=01592c48-207...
8a76-1c4099d7bbb9&DisplayLang=en

Joe Earnest


Quote:
> And if I had bothered to read my "Vbscript in a
> Nutshell" more closely that would have made it clear too.



Tue, 29 Nov 2005 00:20:52 GMT  
 InStrRev not working as expected - bug?
Thanks Joe

I have got the MS docs but have to confess to preferring hard copy ref
sources (need to take my eyes off the screen sometimes!!). Just
looking at the InStrRev definition from the MS source gives

"Returns the position of an occurrence of one string within another,
from the end of string."

which to me still looks as though it should mean that InStrRev("Robert
Day", "b") should return 8 rather than 3.

The Vbscript in a nutshell definition is clearer but I know yout point
wasn't to say one was better than the other anyway.

Thanks again.

Robert

Quote:

> The free MS searchable CHM file download with basic descriptions and
> syntax for both VBS and JS is here (urls will wrap):

> http://www.microsoft.com/downloads/details.aspx?FamilyID=01592c48-207...
> 8a76-1c4099d7bbb9&DisplayLang=en

> Joe Earnest



Wed, 30 Nov 2005 05:39:21 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. passing arguments to sub - script not working as expected

2. passing arguments to sub - script not working as expected

3. Range.Collapse not working as expected

4. SetFocus Property on TextBox Does Not Work as Expected (Q237863)

5. Recorded Macro Does Not Work As Expected

6. Recorded Macro Does Not Work as Expected

7. ItemCheck event on Listviews not working as expected

8. Object Serialization not working as expected

9. Button and hotkey (do not work as expected)

10. Will Move command not working as expected

11. RegEnumValue API not working as I'd expect

12. Combo box and mousepointer not working as expected

 

 
Powered by phpBB® Forum Software