Programmer's Corner Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Programmer's Corner - Forums


VB .Net Array Search Problem/Question???

 
Post new topic   Reply to topic    Programmer's Corner Forum Index -> Visual Basic .NET
Author Message
ReneeFox
Grasshopper


Joined: 22 Oct 2004
Posts: 16
Location: Texass

PostPosted: Sat Nov 13, 2004 7:12 am    Post subject: VB .Net Array Search Problem/Question??? Reply with quote

I have a 1-dimensional array that I'm trying to search for duplicate strings. Perhaps I just don't understand how to get this to work. All elements in the array have: '_n_' within the element string, but not at the beginning of the element. The array is a global dynamic array, so I didn't use Array.CreateInstance. But, in a local subprocedure, I do declare an array with CreateInstance, copy the contents of the global array to the local array, and have tried Array.IndexOf and Array.BinarySearch methods. Neither is finding the duplicate string in subsequent elements. I started out using the methods on the global array and get the same result, i.e., -1 for .IndexOf, or -n for .BinarySearch (where 'n' looks like a counter of how many times it didn't find the string). Are those methods only used to find whole strings in the elements of the array? If so, how do I search for specific strings in the array and report them as duplicate (and obtain the duplicate element in the array)? Here's an example of the strings in my array:

myArray[0] = Beak_64_Lane
myArray[1] = Berchfield_67_Lane
myArray[2] = BISHOP_64_Lane
...

As you can see, both Beak and Bishop have '_64_' in two elements. In the loop, I'm currently looking at 'Beak', so I search at the next element to the end of the array. What I tried was:

addrDup = Array.BinarySearch(myArray, (i + 1), Len(theNumStr), theNumStr) OR
addrDup = Array.IndexOf(myArray, theNumStr, (i + 1))

where 'addrDup' and 'i' are Integers, and 'theNumStr' is a String with '_64_' in it. Will Array.IndexOf or Array.BinarySearch work for this? This is a Windows Application. Any help would be appreciated!
Back to top
aschenta
*narf*


Joined: 07 May 2003
Posts: 548
Location: Windsor Ontario Canada

PostPosted: Sat Nov 13, 2004 11:12 am    Post subject: Reply with quote

IndexOf does search a string for another string so that is the one you are looking for. i believe IndexOf returns the location of the string but i don't know what it returns when you search an entire array. also be sure to check the values of the variables to ensure that you have the data you expect.
Back to top
ReneeFox
Grasshopper


Joined: 22 Oct 2004
Posts: 16
Location: Texass

PostPosted: Sat Nov 13, 2004 7:44 pm    Post subject: Debug Used Reply with quote

Oh yes, what I'm looking for is in the variable; I have AUTO... on, and have stepped through this code lots of times. If IndexOf finds the string, a positive value is returned. The entire time I'm using it, I get -1, which means it didn't find the string ... when it's in there more than twice. I've looked at the array, all variables involved, and they are set correctly. But, I do need something to check the entire array ... not line-by-line. The duplicates could be anywhere in that array, and what a hog that will be, because I have to do this for every duplicate I find ... not just '_64_' which was just an example.

Smile
Back to top
intrest86
Samurai++


Joined: 31 Dec 1969
Posts: 64
Location: Johns Hopkins

PostPosted: Sat Nov 13, 2004 10:11 pm    Post subject: Reply with quote

Ok, you have a few options here.

First you need to understand that the IndexOf function works with objects because it works with all classes. That means that its method to test equality of two things is the overloaded Equals method of the Object class. Since you are searching for a String in an array of Strings, it will use the String classes equal method. That method only returns true if the entire string is the same, so it will not find substrings for you.

Also, about BinarySearch: BinarySearch only works for arrays sorted so that whatever you are searching for is increasing. BinarySearch is useless on unsorted arrays.

So there are two solutions:

1)Iterate through the array yourself, checking fo rthe substring. Basically the same as using IndexOf in the next option, but with more code visible in the place you are using it.

2)Override the String class with a different Equals method then makes an equality check based on substrings. Then fill both of your arrays with this new class, and use IndexOf. If you do this you can also call Sort and then use BinarySearch... but it would be pointless because if ti was sorted the duplicates would be adjacent to each other.
Back to top
ReneeFox
Grasshopper


Joined: 22 Oct 2004
Posts: 16
Location: Texass

PostPosted: Sun Nov 14, 2004 7:05 am    Post subject: Reply with quote

Thaz' what I thought. Darn! I was hoping there was a better, more efficient way. I think this is what I did with the original script that I'm turning in a VB program. What a challenge! KiXtart must've been written in Basic, C, C++ or something similar, because I see all of the same commands without the OOP part. Thank you so much for saving me a lot of time searching for a better answer; I was all over the place, and never saw anything to address this, either ... just the entire element string search. I've been programming for a looooooooooooong time; I'm just new to VB. Oh, about Binary Search ... the array is sorted, but by the 'name' part. I have to do this for both the name, and the number; I'm just choosing the number first, i.e., the array down to the 2nd 64 actually looks like this:

[0] = Abbott_6_Lane
[1] = Abbott_90_Lane
[2] = Abbott_92_Lane
[3] = Beak_64_Lane
[4] = Berchfield_67_Lane
[5] = Berryhill_6_Lane
[6] = Bishop_64_Lane
[7] = Burb_63_Lane

I was updating my KiXtart script to deal with the duplicate numbers when I took VB .NET in school and decided to convert the script instead. This is one of my smaller programs, but it's pretty big.

Very Happy
Back to top
noeldp
Grasshopper


Joined: 22 Jan 2008
Posts: 1

PostPosted: Tue Jan 22, 2008 2:54 pm    Post subject: finding dup strings in an array of strings Reply with quote

dim s as string = string.join("somechar",mystringarray) ' make 1 string
if s.indexof("somestring") <> s.lastindexof("somestring") ' test for dup
Back to top
ReneeFox
Grasshopper


Joined: 22 Oct 2004
Posts: 16
Location: Texass

PostPosted: Mon Jan 28, 2008 4:04 pm    Post subject: Reply with quote

Oh myyyyy! I can't believe you responded after all this time, noeldp! As I look at my question, I barely remember what it was about, and don't even recognize myself as the poster. Surprised

I think I bailed on the project, because it was for a game that put out a new base version, and I switched to that; making the old script obsolete. This may come in handy if I decide to create a program for the new version, but I doubt it. Thanks anyway! Laughing
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Programmer's Corner Forum Index -> Visual Basic .NET All times are GMT - 5 Hours
Page 1 of 1

 


Powered by phpBB © 2001, 2002 phpBB Group