LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Search string in array and retrieve index

I am trying to retrieve the index of the matched string in an array.  For example a 1D array may have 10 names of which 3 are unique.  I would like to search this array for a name and it return the index at which there is a hit.  If there is multiple hits, I would like for it to return an array with the index values.  Also, I would like the search to be case insensitive if it wouldn't be too difficult.
 
Example Array: [Aaron Colunga, Pat Kerr, Cliff Smith, Pat Kerr, Pat Kerr, Cliff Smith, Aaron Colunga, Aaron Colunga,  Aaron Colunga, Cliff Smith]
Search String: "Col"
Returned Array: [0, 6, 7, 8]
 
Any help/advise is appreciated.
0 Kudos
Message 1 of 8
(16,909 Views)
What you could do is feed the string array into a for loop and have it auto index the array and compare it to the string you are trying to match.  Then if there is a match, add the loop count to an array and the output will give you all the indexes of the strings.

I attached a picture to illustrate what i mean


Kenny

Message 2 of 8
(16,896 Views)

This is really fairly basic - The Search 1D Array function will do what you want, but it only returns one hit, so you have to run it in a while loop with a shift register and keep incrementing its offset until you don't find any more results and then stop the loop and remove the last result. To ignore the case, you can simply use the To Lowercase function (string palette) to switch both the array and the string to lowercase. I will leave the actual coding up to you.

To learn more about LabVIEW, I suggest you try searching this site and google for LabVIEW tutorials. Here, here, here, here and here are a few you can start with and here are some tutorial videos. You can also contact your local NI office and join one of their courses.
In addition, I suggest you read the LabVIEW style guide and the LabVIEW user manual (Help>>Search the LabVIEW Bookshelf).


___________________
Try to take over the world!
0 Kudos
Message 3 of 8
(16,893 Views)
Thank for the timely response from both of you.  Both solutions seems to produce the same result (returning the index on an exact match).  I was able to do this before, but I am more interested in partial matches as well.  For example searching for "est" would hit if the actual entry is "test."  Sorry for not having made that clear before.
 
I realize due to my lack of experience it may seem I am being lazy and asking others to solve my "really fairly basic" problems but the truth of the matter is that I am quite new to labview.  I have been experimenting and doing tutorials for a little over a week now and spend alot of time here reading your responses to others' problems.  I just couldn't find one that was exactly like mine although I found many close ones.  My boss doesn't like the idea of me spending a large amount of time training myself, instead he prefers I slowly learn as I go.  I will however try and do the tutorials you linked in your message.
 
In any event, thanks again for your help and good luck joining M. altenback/Dennis in becoming a Proven Zealous Veteran.
Message 4 of 8
(16,886 Views)
Hi acolunga07,

This is possible by packaging the Match Pattern primitive in a while loop.

Four years ago (!), I posted a little sample vi of one way to accomplish this... it's still there 🙂  Make sure you take the second llb that I posted, and make sure to read the other comments in the thread on how to make it better!

Check out this link

Joe Z.
Message 5 of 8
(16,870 Views)
Thanks Joe.  I will run your code and possibly modify it to suit my needs (if thats ok).  I think I may have this problem figured out, but perhaps I will give yall a better overview of what is going on.  I am trying to write a VI that will search for certain information in each row of a tab delimited text file saved with the extension .xls.  In this file there is mixed data (text, dbl f numbers, dates, etc).  From what I gather it is easiest to do this by modifying the read from spreadsheet VI to handle text strings and then split this 2D array into several 1D arrays.  I can then run a loop checking each array for a match pattern with its corresponding search field and when all the searches hit, retrieve the index.  As for a simple case, I enclosed a sample text file with my current solution.  Right now I only have it lighting up the indicator if it hits.
 
On a side note, efficiency (memory or processing) is not a major issue.  The text file might accumulate a couple hundred entries over the span of several years.  This VI is only a subfeature of a much larger VI.
Download All
0 Kudos
Message 6 of 8
(16,861 Views)
The Match Pattern seems to only work sometimes for me.  When I try and match pattern "tps" in "TPS001," "TPS002," and "TPS003" it finds it, but if I search for "tps0," if fails.  Also, putting anything in the Search Test Conductor field causes the match to fail.
0 Kudos
Message 7 of 8
(16,837 Views)
Nevermind, the Indicator was just returning the result from the last iteration.  I put a delay in the loop to see what was happening for each iteration and the search does behave as expected, so everything works fine.
0 Kudos
Message 8 of 8
(16,826 Views)