As the title says... Chat on...

User avatar
By NickLD
#11335 Hi, Im having some trouble doing string matching for NEMA GPS codes. I am hitting the limit on string captures for this code.

the error: "too many captures"
Here is the code:
Code: Select allstr = "$GPRMC,005009.00,A,556.33250,N74137.05197,W,0.542,,050315,,,A*69"
time, status, latitude, ns_indicator, longitude,
        ew_indicator, speed, course, date, variation,
        ew_variation, checksum = string.match(str, "^%$GPRMC,([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^%*]*)(.*)$")
        print(longitude)

I think I can split it somehow, but im unsure if i can do it or how to do it.
I really only need the longitude and the latitude information but its nice to have the rest aswell.
If anyone knows how to solve my problem, please clue me in on how to fix it.

-Nick
User avatar
By dpwhittaker
#11511 Use gmatch on ",([^,]*)" to loop through the matches, or split the match in two:

Code: Select allstr = "$GPRMC,005009.00,A,556.33250,N74137.05197,W,0.542,,050315,,,A*69"
time, status, latitude, ns_indicator, longitude,
        ew_indicator, rest = string.match(str, "^%$GPRMC,([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),(.*)$")
speed, course, date, variation, ew_variation, checksum = string.match(rest, "^([^,]*),([^,]*),([^,]*),([^,]*),([^%*]*)(.*)$")
        print(longitude)