trying to parse string to array of floats...

Hello,

My goal is to parse a textual string into an array of floats... so, I have a loop and I go through the string, and... I get an error message. I will first post the line which was mentioned in the error message: On 1.3 for a FR630, programming in windows.

Failed invoking <symbol>
Unexpected Type Error
in parsePoints (C:\source\AppView.mc:165)

the offending line is this:
point_array= istr.substring(start, end).toFloat();
[/code]

in my resources I have this:
<property id="mystr" type="string">23.3456,-9.12942 12.1234,-3.2425 2.32532,-3.4352</property>



the entire function is pretty complex but essentially the string is in a "X.XXXX,Y,YYYY X.XXXX,Y.YYYY" format. Full points are separated by spaces, lat and lon pairs are separated by a comma. There can be no spaces before or after the body of the string... Pretty minimal error checking... If it doesn't work, it moves on.

I think next time I re-write that function I'll just forgo having two different delimiters... much headache. I am not a veteran loop programmer...

var point_array = new [2 * 4];

function parsePoints() {
var pointsAreGood = false;
var half = (point_array.size()/2).toNumber();
var istr = App.getApp().getProperty("mystr");

var start = 0;
var end;

for (var i = 0; i < half; i++) {
var sample = istr.substring(start, istr.length());
end = sample.find(",").toNumber();
point_array= istr.substring(start, end).toFloat(); // line 165 here: error...
if (!(point_arrayinstanceof Lang.Float)) {
Sys.println("error ingesting 1st-half");
break;
} else {
start = end;
sample = istr.substring(start, istr.length());
end = sample.find(" ").toNumber();
if (end == null) {
end = istr.length();
}
point_array[i+half] = istr.substring(start, end).toFloat();
if (!(point_array[i+half] instanceof Lang.Float)) {
Sys.println("error ingesting 2nd-half");
break;
}
start = end;
if (start == istr.length()){
pointsAreGood = true;
break;
}
}
}
if (!pointsAreGood) { // if unable to load points so try to clean up memory...
for (var i = 0; i < point_array.size(); i++) {
point_array= null;
}
}
}
[/code]

The purpose for all this is to allow the user to enter points into a lengthy string in the app settings, and have the program operate off of those settings. This seems to be the easiest way for what I want to do. If anyone has any recommendations on minimizing memory usage, such as having floats in the settings, I'd be happy to hear it. I am hoping to allow users to enter in these coordinates textually because that is not too difficult.. Thanks!
  • It looks to me like you're including the comma identified by end, but you actually want to omit it. Your code seems very complicated for what it needs to do. I'm not 100% sure that this does what you want, but it sure seems that it does.

    var point_array = new [2 * 3];

    function parse_points(s)
    {
    // if the user enters an invalid character in the string, this could cause
    // the application to crash. it might be a good idea to validate the data
    // while parsing it to avoid that. (the problem areas are noted below)

    // assumes point_array has been allocated to store the correct number of
    // points. we could easily read points from the string and add them to the
    // array until we run out of data to parse, but that is not what the old
    // function did.

    var i = 0, n = point_array.size();
    while (i < n) {

    // find the space
    var space = s.find(" ");
    if (space == null) {
    space = s.length() + 1;
    }

    // get the substring delimited by the space we found. this is one
    // coordinate.
    var coord = s.substring(0, space - 1);

    // find the comma in the coordinate
    var comma = coord.find(",");
    if (comma == null) {
    break;
    }

    // parse before the comma as a float
    point_array= coord.substring(0, comma - 1);
    point_array= point_array.toFloat(); // dangerous
    ++i;

    // parse after the comma as a float
    point_array= coord.substring(comma + 1, coord.length());
    point_array= point_array.toFloat(); // dangerous
    ++i;

    // cut off the stuff we've parsed
    s = s.substring(space + 1, s.length());
    }

    if (i != n) {

    for (i = 0; i < n; ++i) {
    point_array= null;
    }
    }
    }
    [/code]