|
Forum Index : Microcontroller and PC projects : CMM2 FIELD$ confusion
| Author | Message | ||||
| epsilon Senior Member Joined: 30/07/2020 Location: BelgiumPosts: 255 |
I'm confused about how FIELD$ works in this example: > x$="%1%2%3" > print ">>>"+field$(x$,1,"%")+"<<<" >>><<< -> field nbr=1 gives no/empty match > print ">>>"+field$(x$,2,"%")+"<<<" >>>1<<< -> field nbr=2 matches on '1' > print ">>>"+field$(x$,3,"%")+"<<<" >>>2<<< -> field nbr=3 matches on '2' > print ">>>"+field$(x$,4,"%")+"<<<" >>>3<<< -> -> field nbr=4 matches on '3' > print ">>>"+field$(x$,5,"%")+"<<<" >>><<< That first case is most confusing because the result (empty string) is the same as if there are no matches at all in the searched string, which makes it hard to use FIELD$ to parse through a string, unless I'm missing something of course. It's also not clear to me whether the 2nd argument represents a string position from which to start searching (starting at 1), or a 'match number' (1st field, 2nd field, etc), or something else. This is on CMM2. Firmware is 5.0505. Thanks for any help. Epsilon CMM2 projects |
||||
| qwerty823 Newbie Joined: 30/07/2020 Location: United StatesPosts: 30 |
I tend to think of FIELD$ as a one's based "split" Since your x$ starts with a delimiter, when called like: x$="%1%2%3" ? field$(x$,1,"%") prints an empty string because that is what is in the first field: (space added at the beginning for clarity) 1 2 3 4 " %1%2%3" The numbers above your string would be the value of nbr for that part of the string to be extracted. Since you have an empty string before the first %, you get an empty string back for the first field. As to differentiating between an empty field and a non-existent field, I don't think that's possible with FIELD$. Hope that makes sense. |
||||
| matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 10582 |
You are looking for fields terminated by a % The first field is blank so you get nothing i.e a null field terminated by % Then it just follows on as expected. The parameter is the field number. Think of a simple CSV file. If you start with a comma then the first field is missing |
||||
| twofingers Guru Joined: 02/06/2014 Location: GermanyPosts: 1677 |
Hi Ruben, I think the correct use would be: x$="0%1%2%3" The delimiter should come after the string to be extracted. Michael EDIT Peter was faster. ![]() Edited 2020-11-04 04:17 by twofingers causality ≠ correlation ≠ coincidence |
||||
| epsilon Senior Member Joined: 30/07/2020 Location: BelgiumPosts: 255 |
Thank you very much for your excellent responses guys! So the delimiter is a field terminator and FIELD is intended for parsing pre-formatted data such as CSV. I was trying to use it as poor man's regular expression matcher on MMBasic text, but that's probably not the intent of this function. Epsilon CMM2 projects |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |