![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Search a Two-Dimentional Array Does anyone know how to efficently search a Two-Dimentaional Array and return results from another Column (10,000's of entries) ArrayA Bob;Smith;Texas Sue;Johnes;Alberta I would like to search in Column1 then return the value in Column3 if found e.g. If I look for Sue and find her, I would like to return Alberta. Any help would be apreciated. I have always struggled with Arrays. |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Search a Two-Dimentional Array Bob Smith wrote: Quote: > Does anyone know how to efficently search a Two-Dimentaional Array and > return > results from another Column (10,000's of entries) > > ArrayA > Bob;Smith;Texas > Sue;Johnes;Alberta > > I would like to search in Column1 then return the value in Column3 if > found > > e.g. If I look for Sue and find her, I would like to return Alberta. > > Any help would be apreciated. I have always struggled with Arrays. a dictionary object, which has an Exists method. A dictionary object is an associative array of key, value pairs, but the keys must be unique. For example: =========== Set objList = CreateObject("Scripting.Dictionary") ' Make comparisons case insensitive. objList.CompareMode = vbTextCompare objList.Add "Bob", "Texas" objList.Add "Sue", "Alberta" If objList.Exists("Sue") Then Wscript.Echo objList("Sue") End If ======== The keys must be unique, so if you attempted to add another "Sue" an error would be raised. For your situation you need to enumerate the array and search. For example: ========== Dim Array3(1, 2) Array3(0, 0) = "Bob" Array3(0, 1) = "Smith" Array3(0, 2) = "Texas" Array3(1, 0) = "Sue" Array3(1, 1) = "Johnes" Array3(1, 2) = "Alberta" For k = 0 To UBound(Array3, 1) If (LCase(Array3(k, 0)) = "sue") Then Wscript.Echo Array3(k, 2) Exit For End If Next ======= Use the "Exit For" only if you expect just one "hit". There there can be more than one "Sue", remove that statement. I hope this helps. -- Richard Mueller MVP Directory Services Hilltop Lab - http://www.rlmueller.net -- |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Search a Two-Dimentional Array On Nov 20, 12:09*pm, "Richard Mueller [MVP]" <rlmueller- nos...@xxxxxx> wrote: Quote: > Bob Smith wrote: Quote: > > Does anyone know how to efficently search a Two-Dimentaional Array and > > return > > results from another Column (10,000's of entries) Quote: > > ArrayA > > Bob;Smith;Texas > > Sue;Johnes;Alberta Quote: > > I would like to search in Column1 then return the value in Column3 if > > found Quote: > > e.g. If I look for Sue and find her, I would like to return Alberta. Quote: > > Any help would be apreciated. I have always struggled with Arrays. > Your example has 3 values per row. If there were 2 I would recommend you use > a dictionary object, which has an Exists method. A dictionary object is an > associative array of key, value pairs, but the keys must be unique. For > example: > =========== > Set objList = CreateObject("Scripting.Dictionary") > ' Make comparisons case insensitive. > objList.CompareMode = vbTextCompare > > objList.Add "Bob", "Texas" > objList.Add "Sue", "Alberta" > > If objList.Exists("Sue") Then > * * Wscript.Echo objList("Sue") > End If > ======== > The keys must be unique, so if you attempted to add another "Sue" an error > would be raised. For your situation you need to enumerate the array and > search. For example: > ========== > Dim Array3(1, 2) > > Array3(0, 0) = "Bob" > Array3(0, 1) = "Smith" > Array3(0, 2) = "Texas" > Array3(1, 0) = "Sue" > Array3(1, 1) = "Johnes" > Array3(1, 2) = "Alberta" > > For k = 0 To UBound(Array3, 1) > * * If (LCase(Array3(k, 0)) = "sue") Then > * * * * Wscript.Echo Array3(k, 2) > * * * * Exit For > * * End If > Next > ======= > Use the "Exit For" only if you expect just one "hit". There there can be > more than one "Sue", remove that statement. I hope this helps. > > -- > Richard Mueller > MVP Directory Services > Hilltop Lab -http://www.rlmueller.net > -- entries associated with the index. The dictionary items can be any form of data, such as a semicolon delimited string or even an array, so that this will work fine ... Set objList = CreateObject("Scripting.Dictionary") ' Make comparisons case insensitive. objList.CompareMode = vbTextCompare objList.Add "Bob", Split("Smith;Texas", ";") objList.Add "Sue", Split("Johnes;Alberta", ";") If objList.Exists("Sue") Then Wscript.Echo "Sue", objList("Sue")(1) End If Tom Lavedas *********** http://there.is.no.more/tglbatch/ |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Search a Two-Dimentional Array > Does anyone know how to efficently search a Two-Dimentaional Array and return Quote: > results from another Column (10,000's of entries) > > ArrayA > Bob;Smith;Texas > Sue;Johnes;Alberta > > I would like to search in Column1 then return the value in Column3 if found > > e.g. If I look for Sue and find her, I would like to return Alberta. > > Any help would be apreciated. I have always struggled with Arrays. You said effectively... If so, you might consider your expected use of the program. if you are reading a LOT of lines from a txtfile in the indicated format, just searching for sue; once, you might consider filtering the input first - like this: find /I "sue;" input.txt > sue.txt yourprog sue.txt yourprog would be like (since you now will have only wanted lines): read all lines from the file in the arg for each line, split inputline, ";" writeline split_arr(2) 'counting from 0 I am sure you can do this litle program :-) If on the other hand you want to have the 10.000's of lines in the program, and will do varyous searches, you might want to sort the file first (using sort command in "dos" or in some other way). Knowing data is sorted will make you able to do a binary search. If you expect more "hits" for the "key", you will have to search a little back and forward from the key found (until you no longer find hit). Doing linear search is very time consuming as compared to binary search, you might be out of luck and have to lookup in every cell.... A binary search will complete (in a sorted array) in log2(N) where N is the number of rows. Ie. 50.000 linies could be searches in 16 lookups (at most). Price is the sorting, therefore the assumption that you would be doing many lookups. :-) Freddy |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Search a Two-Dimentional Array You can still use a dictionary object. Just make the assigned value an array as in set dic = CreateObject("Scripting.Dictionary") dic("Sue") = Array("Smith","87 Main Streen","Baltimore","222-3891") if dic.Exists("Sue") then wscript.Echo dic("Sue")(2) end if "Richard Mueller [MVP]" <rlmueller-nospam@xxxxxx> wrote in message news:ON8SWLzSJHA.3584@xxxxxx Quote: > Bob Smith wrote: > Quote: >> Does anyone know how to efficently search a Two-Dimentaional Array and >> return >> results from another Column (10,000's of entries) >> >> ArrayA >> Bob;Smith;Texas >> Sue;Johnes;Alberta >> >> I would like to search in Column1 then return the value in Column3 if >> found >> >> e.g. If I look for Sue and find her, I would like to return Alberta. >> >> Any help would be apreciated. I have always struggled with Arrays. > Your example has 3 values per row. If there were 2 I would recommend you > use a dictionary object, which has an Exists method. A dictionary object > is an associative array of key, value pairs, but the keys must be unique. > For example: > =========== > Set objList = CreateObject("Scripting.Dictionary") > ' Make comparisons case insensitive. > objList.CompareMode = vbTextCompare > > objList.Add "Bob", "Texas" > objList.Add "Sue", "Alberta" > > If objList.Exists("Sue") Then > Wscript.Echo objList("Sue") > End If > ======== > The keys must be unique, so if you attempted to add another "Sue" an error > would be raised. For your situation you need to enumerate the array and > search. For example: > ========== > Dim Array3(1, 2) > > Array3(0, 0) = "Bob" > Array3(0, 1) = "Smith" > Array3(0, 2) = "Texas" > Array3(1, 0) = "Sue" > Array3(1, 1) = "Johnes" > Array3(1, 2) = "Alberta" > > For k = 0 To UBound(Array3, 1) > If (LCase(Array3(k, 0)) = "sue") Then > Wscript.Echo Array3(k, 2) > Exit For > End If > Next > ======= > Use the "Exit For" only if you expect just one "hit". There there can be > more than one "Sue", remove that statement. I hope this helps. > > -- > Richard Mueller > MVP Directory Services > Hilltop Lab - http://www.rlmueller.net > -- > > |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Is there a simple way to search a multidimensional array? | PowerShell | |||
| Fast copy method of sub array (=array range) possible? | VB Script | |||
| Randomize a one dimentional array | VB Script | |||
| Stupid Array Tricks: Initializing an Array to a Certain Size | PowerShell | |||
| how to assign values to array and how to create array via variable | PowerShell | |||