Search a Multi-Dimension String

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1720 Views - Last Post: 08 December 2011 - 08:54 PM Rate Topic: -----

#1 diek  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Search a Multi-Dimension String

Posted 06 November 2011 - 10:53 AM

Hi,
I have searched for a simple way to search a string array. MS gives a clean simple solution for a one dimesion string.

Dim StrArray() As String = {"ABCDEFG", "HIJKLMNOP"}
Dim FindThisString As String = "JKL"
For Each Str As String In StrArray
    If Str.Contains(FindThisString) Then
        MsgBox("Found " & FindThisString & " at index " & _
          Str.IndexOf(FindThisString))
    End If
Next

Just one of my two problems with array, but this one in more pressing. I need to search a multi-dimensional string array. I never dreamed it would be so hard, and while my textbook did cover arrays it did not do the job, their sample code did not even work. So trying the code above I tried the following.
        Dim StrArray() As String = Public SparkPlugsString(,) As String = {{"VBAutoPart", "Brand A", "Brand C", "Brand X"}, _
                                                                           {"PR214", "MR43T", "RBL8", "14K22"}, _
                                                                           {"PR223", "R43", "RJ6", "14K24"}}
        'At this point I am using RBL8 as a test
        Dim FindThisString As String = "RBL8"
        For Each Str As String In StrArray()
            If Str.Contains(FindThisString) Then
                MsgBox("Found " & FindThisString & " at index " & _
                  Str.IndexOf(FindThisString))
            End If
        Next


I thought it was simply a matter of adding a (,) to the declaration but nope. VB has the returns 2 errors, after I add my array:

Error 1: Expression expected.
Error 2:'StrArray' is not declared. It may be inaccessible due to its protection level.

Please note, I am looking to understand the problem, not just an answer.

Thank you,

diek

Is This A Good Question/Topic? 0
  • +

Replies To: Search a Multi-Dimension String

#2 diek  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Re: Search a Multi-Dimension String

Posted 06 November 2011 - 11:45 AM

I tried to find a way to edit my post, without luck. I am also open to other ways to search a multi-dimension string array.

diek
Was This Post Helpful? 0
  • +
  • -

#3 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1058
  • View blog
  • Posts: 4,104
  • Joined: 02-July 08

Re: Search a Multi-Dimension String

Posted 06 November 2011 - 01:41 PM

Your opening statement is quite impossible.

Quote

Dim StrArray() As String = Public SparkPlugsString(,) As String = {{"VBAutoPart", "Brand A", "Brand C", "Brand X"}, _
02	                                                                   {"PR214", "MR43T", "RBL8", "14K22"}, _
03	                                                                   {"PR223", "R43", "RJ6", "14K24"}}


Dim searchItem As String = "R43"
Dim plugs() As String = {"PR223", "R43", "RJ6", "14K24"}
For i As Integer = 0 To plugs.Count - 1
  If plugs(i).Equals(searchitem) Then
     'item found
     MessageBox.Show(String.Format("Plug {0} found at {1} index", plugs(i), i)
  End If
Next

Was This Post Helpful? 1
  • +
  • -

#4 diek  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Re: Search a Multi-Dimension String

Posted 06 November 2011 - 02:16 PM

Hawk,
Thanks for setting me straight. I should have explained the problem I am having. And please note it has been many years since I worked with arrays. So the employee has to pick one of 9 spark plugs, from one of three companies. Each company has their own code for identification of the spark plug. So essentially 27 different numbers. Then the employee has to match it to one of his own spark plugs, and so another 9 codes. So one spark plug can be identified by 4 different numbers based upon the company/dealer. So in my newbie brain I thought an array with 4 numbers would work. The problem can be found at http://www.mrharris....r/vbauto08.htm. A picture and the numbers can do a better job than I can explaining it.

I am certain I could write a pages of code with If then, but there has to be a more effective way to do this.

Thank you,
diek
Was This Post Helpful? 0
  • +
  • -

#5 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1058
  • View blog
  • Posts: 4,104
  • Joined: 02-July 08

Re: Search a Multi-Dimension String

Posted 06 November 2011 - 04:55 PM

I can't open the link, but I am mobile. What are you using for a data source or is this an exercise with arrays?
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Search a Multi-Dimension String

Posted 06 November 2011 - 05:33 PM

To me this, strongly suggests creating a couple of user-defined class. SparkPlug and PlugCode and Brand
Spoiler


Now you can easily convey the information.
Spoiler

Was This Post Helpful? 1
  • +
  • -

#7 diek  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Re: Search a Multi-Dimension String

Posted 06 November 2011 - 05:53 PM

Sorry the url got truncated.
VB Autocenter
I am starting to get the array concept. My brain is almost there.

Hawk, I now see how my initial code was impossible.

I think I got the search part, but I am having trouble getting my head wrapped around loading the info once I find indexPosted Image(an image to help my brain) So R43N, index (3,0)entered into the EnterPartNumberTextBox by the user. This corresponds to PR246, index (3,4)and loads "PR246" into VBPartTextBox.

I appreciate the help from everyone.
Was This Post Helpful? 0
  • +
  • -

#8 diek  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Re: Search a Multi-Dimension String

Posted 06 November 2011 - 06:44 PM

I am going to go and absorb this, I will be back.

diek
Was This Post Helpful? 0
  • +
  • -

#9 diek  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Re: Search a Multi-Dimension String

Posted 06 November 2011 - 09:20 PM

So part of the problem is resolved but my inexperience in showing, I need the row int and col int returned in my array.

Andrew, if I used the code above my prof would give me a look like I have lobsters coming out of my ears. That said I do appreciate the effort.

So here is the code, it finds the string but the return on the location index(#,#) is not working:
    Private SparkPlugCodes(,) As String = {{"MR43T", "RBL8", "14K22"}, {"R43", "RJ6", "14K24"}, {"R43N", "RN4", "14K30"}, {"R46N", "RN8", "14L32"},{"R46TS", "RBL17Y", "14K33"}, {"R46TX", "RBL12-6", "14K35"}, {"S46", "J11", "14K38"},{"SR46E", "XEJ8", "14K40"}, {"47L", "H12", "14K44"}}

    Private Sub LookUpButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LookUpButton.Click
        Dim FindThisString As String = "14K44"
        For Each Str As String In SparkPlugCodes
            If Str.Contains(FindThisString) Then
                MsgBox("Found " & FindThisString & " at index " & _
                  Str.IndexOf(FindThisString))
            End If
        Next
    End Sub


Any help or guidance would be appreciated.

diek
Was This Post Helpful? 0
  • +
  • -

#10 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: Search a Multi-Dimension String

Posted 06 November 2011 - 09:37 PM

The .IndexOf method finds the index of the search string in the string that you're searching. Confused? Let's have an example. If I was searching for "em" in "November" ("November".IndexOf("em")), the result would be 3 (think of a string as an array of chars, starting at 0). But enough of why it's not working. What you need is 2 loops (nested), with one handling the x dimension and one handling the y dimension.
For i = 0 To SparkPlugCodes.GetUpperBound(0) 'This handles the first dimension
    For j = 0 To SparkPlugCodes.GetUpperBound(1) 'This handles the 2nd dimension
        If SparkPlugCodes(i, j).Contains(FindThisString) Then
	    MsgBox("Found " & FindThisString & " at index " & i.ToString & "," & j.ToString
	End If
    Next
Next

This post has been edited by Tryparse: 06 November 2011 - 09:37 PM

Was This Post Helpful? 1
  • +
  • -

#11 diek  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Re: Search a Multi-Dimension String

Posted 06 November 2011 - 09:47 PM

Perfect! Thanks.
Was This Post Helpful? 0
  • +
  • -

#12 ricardosms  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 301
  • Joined: 02-April 10

Re: Search a Multi-Dimension String

Posted 06 November 2011 - 09:54 PM

This is what I do:
I have a record of about 3500 items with 9 entries each.
I make a one dimension string by concatenating the 9 elements. so I have an array of 3500 strings. Then I search each string and get all the results formated on a textbox including the item number. Then this number refers to the one of the 9 element(dimension) array.

This post has been edited by ricardosms: 06 November 2011 - 09:59 PM

Was This Post Helpful? 0
  • +
  • -

#13 diek  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Re: Search a Multi-Dimension String

Posted 06 November 2011 - 10:18 PM

R,
Thanks, that sounds like a good idea, in this case the code from Tryparse, above, I think will work. It is 1:30 Atlantic Time, so my brain is officially full. I will go and absorb what you said. And be back.

Thanks,
d
Was This Post Helpful? 0
  • +
  • -

#14 ricardosms  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 301
  • Joined: 02-April 10

Re: Search a Multi-Dimension String

Posted 07 November 2011 - 04:41 AM

If I want to narrow my search, I use 2 search substrings, usually longer than 3 characters, so I don't get too many results. If I have a partial part number and manufacturer' name or partial description, I can find just two or three records and then reformat the string or navigate to it on my program. It works good for me. Even I I have to do further authomatic processing, it facilitates it a lot.
Was This Post Helpful? 0
  • +
  • -

#15 diek  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Re: Search a Multi-Dimension String

Posted 07 November 2011 - 03:06 PM

Hey,
I really appreciate everyone's help. In the end Tryparse's code did the trick. And I was able to understand it. I ended up with a 27 out of 30. -1 for too much 'white space' on one of the four forms, -1 for two variable declarations not adhering to my naming convention, and -1 because I missed a few tool tips. I was just happy that with your help the program did exactly what it was supposed to do.

I used to use Experts-Exchange, but now that it is a pay site, I cannot afford it. This forum worked perfectly thank you,

diek

View Postricardosms, on 06 November 2011 - 09:54 PM, said:

This is what I do:
I have a record of about 3500 items with 9 entries each.
I make a one dimension string by concatenating the 9 elements. so I have an array of 3500 strings. Then I search each string and get all the results formated on a textbox including the item number. Then this number refers to the one of the 9 element(dimension) array.

I am curious about concatenating the arrays, I tried , did some research, tried again, but I was unable to do it. How do you do it?
d
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2