04-13-2015 03:46 PM
This is probably a simple thing most of you can do in your sleepk, but I am still a newbie at LV. I have a text file that I am try to find data within. A sample file might look like:
Roses are red
Violets are blue
blah blah
other swill
17 items
I am after the "17 items" portion. It is on the next to the last line (the last line is a blank line immediately after "17 items". Sometimes it might be a single digit number like "4 items". Sometimes the file will have more lines in it than others, so I do not know which line will have my target info on it.
I was looking for the "items" and trying to get the value of "17". I can search and find "items" but from there I can't get a vi to do what I want. Please do not write it for me. I like to write. I just need help with the vi palette.
How can I find out how many lines are in my text file? They have a CR/LF at the end.
Is there a way to read the last line in the file?
Does LabView have Right / Left / Mid commands for looking at sections of strings?
Actually I did find a vi for Mid and Left command: the String Subset Function. And String Length will tell me how many characters long a string is. So is there something like a Right?
Thanks,
Steve Feeser
04-13-2015 03:52 PM
Well, since you said "don't write it for me", let's try a hint. You can right click on the "read from text file" and tell it to "read lines". It will return a 1d array that you can easily search, scan through, etc.
Also, try posting what you have done so far so we can tell you why it does NOT work. Sometimes that is the more important lesson...
04-13-2015 05:41 PM
My initial thought was to do the read all lines (have the Read Text File read by lines) and then you just run the lines through a FOR loop that contains a Scan From String with a "%d Items" as the format string. Stop the FOR loop when you do NOT get an error (set conditional to Run While True).
04-14-2015 07:17 AM
Here is the vi I have so far. I am looking at one line at a time searching for the Desired String. I increment Numeric if no match to look at the next line (I don't think that will work right the way I have it). How can I end it when it finds the matching string?
04-14-2015 08:08 AM - edited 04-14-2015 08:10 AM
04-14-2015 08:27 AM
I kicked it around some more and came up with this. It seems to work. Any suggestions?
I still would like to get the number off the front of the text string; that is what I am after. I can do that in TestStand but it would be nice to do it all in LV.
Thanks,
Steve
04-14-2015 10:40 AM
Here is my final solution. It has a limit of 200 lines to search for the desired string. If it does not find it in 200 it stops. I can't picture this file ever reaching 100 lines long, so 200 limit is safe. It gives an output of the number in front of the deisred string.
Comments?
Thanks,
Steve
04-14-2015 01:19 PM
@SteveFeeser wrote:
Here is my final solution. It has a limit of 200 lines to search for the desired string. If it does not find it in 200 it stops. I can't picture this file ever reaching 100 lines long, so 200 limit is safe. It gives an output of the number in front of the deisred string.
Comments?
Thanks,
Steve
Are you still going with the "Don't write it for me, help me"?
Here are my comments:
1. Every loop iteration you read that file again. So, if you scan 200 lines, you read the file 200 times!
2. You never close your file. You should get in the habit of paying attention to closing references.
3. You ignored two different people telling you to right click on your "read from text file" and tell it to "read lines". Wire a -1 in to the count, and it will return an array, with each element a line. You can then search it inside of a for loop with a conditional stop. Like crossrulz suggested.
04-14-2015 02:19 PM
After reading the help sections for the first two vi's used here:
Read from Text File Function
Pick Line Function
I don't think it works like that. I am thinking it works like this (maybe I am wrong):
The first vi (Read from Text File Function) says it opens, reads all the characters (I assume into memory) and then closes the file.
The second vi (Pick Line Function) then goes thru line by line presenting strings to the Match Pattern Function vi.
The Read from Text File Function does close the file so that is not a concern.
The Read from TextFile Function does have an option to read lines but it does not read one line at a time, like I want to search it. (I want to search line by line to find the number at the beginning of the line.) That vi reads however many lines you specify, always starting at the beginning of the file. So, I did not see how that would help me...
Now if it really does open the file each time I increment the Pick Line Function vi then I can see that reading them into an array would be faster.
Wouldn't it do the Read from Text File, close the file and then do the Pick Line Function?
Thanks,
Steve
04-14-2015 02:21 PM
OOOhhhhh but the Read From Text File is inside my While loop. So it is doing it every time.....