Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 09:04 15 Nov 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : CMM2 FIELD$ confusion

Author Message
epsilon

Senior Member

Joined: 30/07/2020
Location: Belgium
Posts: 255
Posted: 05:51pm 03 Nov 2020
Copy link to clipboard 
Print this post

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 States
Posts: 30
Posted: 06:09pm 03 Nov 2020
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 10582
Posted: 06:11pm 03 Nov 2020
Copy link to clipboard 
Print this post

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: Germany
Posts: 1677
Posted: 06:16pm 03 Nov 2020
Copy link to clipboard 
Print this post

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: Belgium
Posts: 255
Posted: 08:21am 04 Nov 2020
Copy link to clipboard 
Print this post

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
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025