Len of array doesn't include NULL values

An odd one; I thought I’d make a quick and dirty NEMA parser for my Adafruit GPS. I’m capturing the NEMA strings just fine. Alas, things fall apart after that.

Consider this example:

$GPRMC,200023.086,0,0,M,M,*45

This should parse out to:

//0 $GPRMC, type //1 200123.086, UTC time //2 V, V (void) or A (active) //3 , lat (missing if void) //4 , lat dir (missing if void) //5 , lon (missing if void) //6 , lon dir (missing if void) //7 0.00, speed (knots) //8 0.00, compass direction //9 190115, date (ddmmyy) //10 , magnetic variation (missing if void) //11 , magnetic variation direction (missing if void) //12 N*4D checksum

But if I use:

local nemaArray = split(gpsLine,",");

…I get a len() of seven instead of the expected thirteen; when I iterate through, lat, lon, etc. are completely missing.

I tried searching the documentation, but I’m probably using the wrong terminology. Sorry!

In short: how do I split a csv array to preserve null values?

Unfortunately, you have to do this manually right now. The squirrel builtin doesn’t do what you want; we have a task in for a better split but have to preserve the current split functionality because some code may be reliant on it.

I’m pretty sure split() just skips through fields where there is nothing between the delimiters. I’ve seen the same when decoding urls:
ie

“/a/b/c” returns the same as “//a//////b/////c”

Best that you use split then check if nemarray[2]==“V”. If so, you will have missing fields.

At least I know it wasn’t a stupid mistake on my part. Thanks for the explanation.

A small update: I just found
PROCESSING NMEA 0183 GPS STRINGS in the forum, and snitched the split replacement proposed by Hugo. With my small change, it’s now:

//replacement for funky string split function function NMEASplit(a, b) { local ret = []; local field = ""; foreach(c in a) { //server.log(c); if (c.tochar() == b) { // found separator, push field ret.push(field); field=""; } else { // append to field field+=c.tochar(); } } // Push the last field ret.push(field); return ret; }

…and seems to be working just fine. We shall see.

Thanks again!