InStr finds the wrong occurence

when searching integers, instr returns the wrong occurence

Page 1 of 1

8 Replies - 2017 Views - Last Post: 30 March 2009 - 07:16 PM Rate Topic: -----

#1 midasxl   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 212
  • Joined: 03-December 08

InStr finds the wrong occurence

Post icon  Posted 26 March 2009 - 10:32 AM

Hello and thanks for your time. I have a small visual basic script that finds the position of the first occurence of one string inside another. I'm sure you are familiar with the InStr function. It is doing something funky.

The records that are returned via database query are as follows...

rs1("VAR") = 1;7;1999;223;267
rs("ID") = 23

This keeps returning a value of 11, which makes sense because the integer 23 is in the string being searched (as part of 223). So I am assuming I need to split the string and load the individual data into an array. After doing so, how would I go about identifying the numeric position for each one? Thanks!

sameVAR = InStr(rs1("VAR"),rs("ID"))



Is This A Good Question/Topic? 0
  • +

Replies To: InStr finds the wrong occurence

#2 Raju2047   User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 55
  • Joined: 16-February 09

Re: InStr finds the wrong occurence

Posted 26 March 2009 - 11:23 AM

first of all store it in the array. then also measure the length of each array. then when the array lengths become you selected number then call the number.
Was This Post Helpful? 0
  • +
  • -

#3 midasxl   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 212
  • Joined: 03-December 08

Re: InStr finds the wrong occurence

Posted 26 March 2009 - 11:52 AM

Thanks for the info. Here's what I came up with so far...

Dim strValue, cocArray, counter

strValue = rs2("COC")

cocArray = split(strValue,";")

arrLen = Ubound(cocArray)
Response.Write(arrLen & "<br>")

for counter = 0 to UBound(cocArray) 
Response.Write(cocArray(counter) & "<br />") 

next



The first response.write shows me the length of the array as 4 (this is correct), and the second one shows me the contents of the array which display as...

1
7
1999
223
267

This is also correct. The next step I want to take is to search this array for the integer 223, and have it return the position of it within the array as an integer. In this case it would return as position 3. I have searched for some code to do this but I am confused; thanks for any help you may be able to provide!
Was This Post Helpful? 0
  • +
  • -

#4 Raju2047   User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 55
  • Joined: 16-February 09

Re: InStr finds the wrong occurence

Posted 26 March 2009 - 12:26 PM

Dim strValue, cocArray, counter, arrlen, strLen, InstrVal, ArrText, ArrayInt, cntr1
strValue = "1;7;1999;223;267"
strLen = Len(strValue)
cocArray = Split(strValue, ";")
InstrVal = InStr(rs2("COC"), rs1("VAR"))
arrlen = UBound(cocArray)
Response.Write (arrlen & "<br>")
For counter = 0 To UBound(cocArray)
Response.Write (cocArray(counter) & "<br />")
cntr1 = cntr1 + Len(cocArray(counter)) + 1
If cntr1 >= InstrVal Then
ArrText = cocArray(counter)
ArrayInt = counter
Exit For
End If
Next
Response.Write "The string is " & ArrText & ". And the Array no is " & ArrayInt



Just check this script out. Hope it will help.

This post has been edited by Raju2047: 26 March 2009 - 12:26 PM

Was This Post Helpful? 0
  • +
  • -

#5 midasxl   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 212
  • Joined: 03-December 08

Re: InStr finds the wrong occurence

Posted 26 March 2009 - 12:50 PM

I'll check this out later, thanks alot for your help!!
Was This Post Helpful? 0
  • +
  • -

#6 midasxl   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 212
  • Joined: 03-December 08

Re: InStr finds the wrong occurence

Posted 27 March 2009 - 05:45 AM

OK, I have made some progress. Funny how these things evolve. Here is what I have now...

Dim strValue, myArray, counter

strValue = ("1;7;1999;4;9")

myArray = split(strValue,";")

length = UBound(myArray)

for counter = 0 to UBound(myArray) 
Response.Write(myArray(counter) & "<br />") 
next

For x = LBound(myArray) To UBound(myArray)
If myArray(x) = "4" Then
position1=x
End If
Next

For x = LBound(myArray) To UBound(myArray)
If myArray(x) = "9" Then
position2=x
End If
Next

response.write("The position of 4 = " & position1 & "<br>")
response.write("The position of 9 = " & position2 & "<br>")

If position1 <> 0 AND position1 <= position2 Then
response.write("no")
else
response.write("yes")
end if



This works very well in the sense that I have given the script exact integers to look for within the array ("4", and "9") Of course it finds both of them and correctly returns their positions with the array (3, and 4 respectively).

The final IF statement compares them and then responds accordingly. Where i am having trouble now is substituting the hardcoded "4" and "9" with a record set variable from a database query.

Here is what I need it to be...

For x = LBound(myArray) To UBound(myArray)
If myArray(x) = rs1("ID") Then
position1=x
End If
Next

For x = LBound(myArray) To UBound(myArray)
If myArray(x) = rs2("ID") Then
position2=x
End If
Next



This returns nothing. I have verified the values of both record set ID's by setting a response.write AFTER EVERY LINE in the script. Each response.write returns correct with the exception of the two that sit inside the IF / ENDIF section of the FOR statements. Those response.writes return nothing. It's almost like the record set variable do not even exist inside this area. Position1 and Position2 return nothing. Not even a 0. Any ideas? Is my syntax wrong? Thanks again!

This post has been edited by midasxl: 27 March 2009 - 09:32 AM

Was This Post Helpful? 0
  • +
  • -

#7 thava   User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,607
  • Joined: 17-April 07

Re: InStr finds the wrong occurence

Posted 28 March 2009 - 07:02 PM

i think you deviate your thought

why don't you use the instr function

in the for loop check whether instr Returns greater than zero
Was This Post Helpful? 0
  • +
  • -

#8 midasxl   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 212
  • Joined: 03-December 08

Re: InStr finds the wrong occurence

Posted 30 March 2009 - 05:19 AM

Thanks for the tip. I was using the InStr function in the beginning, but I was getting inaccurate results. For example...

sameCOC = InStr(rs1("COC"),rs("ID"))



What I was finding was if rs("ID") returned a value of 28, and rs1("COC") = "1;7;48;228;1999" then the InStr was returning a result of 8. Which is correct when searching the string from left to right starting with 0. It was finding 28 within 228 which is obviously not correct; I was looking for a return of 0. It was obvious I had to create an array.

What I ended up doing was splitting the string, and loading it into an array, then compared the record set return with each of the individual strings separated by the ;

I also discovered that rs("ID") was representing an integer, not a string. The array is now a series of strings, so I had to use cint to convert the strings to integers, then the script was able to make the match.

Dim strValue, myArray, position1, position2, counter

	strValue = rs1("COC")
		
		myArray = split(strValue,";")

		For counter = LBound(myArray) to UBound(myArray) 

		If cint(myArray(counter)) = rs("ID") Then	
		position1 = counter

		Exit For
		else
		position1 = 0
		End If
		Next




Thanks for the assistance!
Was This Post Helpful? 0
  • +
  • -

#9 thava   User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,607
  • Joined: 17-April 07

Re: InStr finds the wrong occurence

Posted 30 March 2009 - 07:16 PM

instead of using cint try the cstr function with trim on both sides
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1