10-13-2016 11:03 AM
Hello LabVIEW community. Here's an easy one that's got me a little stumped. So obviously, not easy for me. I'm trying to use Scan From String to get the City, State, Zip from a string. I can build the string as I have just described with commas using Format Into String. But you can't do it the other way around and get an error 85. Yes, I have read the white paper on this and am not entirely sure how to apply their solution. Yes, I could put the bandaids on the code and remove the comma "manually" but I was wondering if the community knew how to remove the commas from the string by just using Scan From String.
Solved! Go to Solution.
10-13-2016 11:09 AM
Scan From String is 'greedy' as you have discovered. %s will basically grab anything it can, including the commas so the first %s will just grab the whole string. You need to put the brakes on it just a little bit. For simple cases like this (no commas allowed inside the individual parts) simply replace %s,%s,%s with %[^,] ,%[^,] ,%[^,] which means match anything which is not a comma followed by a comma.
10-13-2016 11:13 AM
You da man! But one more question, let's say it was City, Zip, State. And I wanted to output the zip as an I32 and not a String?
10-13-2016 11:15 AM
@DailyDose wrote:You da man! But one more question, let's say it was City, Zip, State. And I wanted to output the zip as an I32 and not a String?
Never mind. I answered my own question. %d is only grabbing numbers so " %[^,] ,%d,%[^,] " would work just fine. Thank you!
10-13-2016 11:16 AM - edited 10-13-2016 11:18 AM
Well, here's te solution just using Scan From String, which you made a precondition. Not saying it's the way I'd necessarily do it. There are also Match Pattern and Match RegEx nodes, and even Scan String for Tokens, but using the right format specifier on Scan From String Will do. Note the format specifier is
%[^,],
which says "match all chars that aren't a comma", then match a literal comma (which is discarded).
Hope this helps!
Dave
EDIT: I hate it when I'm five minutes too late and miss the good discussion.
10-13-2016 11:18 AM
@Darin.K wrote:%s will basically grab anything it can, including the commas so the first %s will just grab the whole string.
Just a slight correction. The %s in a Scan From String actually stops at a white space (ex: space, tab, etc).
10-13-2016 11:24 AM
You might want to also include the space in the format string so that it doesn't get included in the return values. So as Darin said, everything that isn't a comma followed by a comma and a space.
%[^,] , %[^,] , %d
(There is a space after the second and fourth comma.)
10-13-2016 11:29 AM
EDIT: I hate it when I'm five minutes too late and miss the good discussion.
I gave you a Kudo. 🙂
10-13-2016 12:29 PM
Also late, but I'd use the Spreadsheet String to Array, where the delimeter is the comma. Then you get an array of strings with each element being the three fields. You can then index the zip and turn it into a number if you want. Not sure which performs better.
Unofficial Forum Rules and Guidelines
Get going with G! - LabVIEW Wiki.
17 Part Blog on Automotive CAN bus. - Hooovahh - LabVIEW Overlord
10-13-2016 12:40 PM - edited 10-13-2016 12:41 PM
Quick test shows the Array to String method, with a string to number, being about 15-20% faster than the scan from string for 100,000 items.
93ms for Scan from String, and 74ms for Array to String.
Unofficial Forum Rules and Guidelines
Get going with G! - LabVIEW Wiki.
17 Part Blog on Automotive CAN bus. - Hooovahh - LabVIEW Overlord