String to array?:)

Former Member
Former Member
How do i get a string to array?

Strings.xml:

<string id="test">1,2,3,4,5</string>

Document.mc:
var test = Ui.loadResource(Rez.Strings.test);
var array = "["+test+"]";

It returns as a string ("[1,2,3,4,5]"), but I have read that I can use JSON.parse, like

var array = JSON.parse("[" + string + "]");

But it dont work, and is there a way to get it to work? :D
  • Former Member
    Former Member over 9 years ago
    There's no such thing as JSON.parse in connect iq.
    What you can do I this (I'm writing this in the browser, so it's not tested code):

    function toArray(string, splitter) {
    var array = new [5]; //Use maximum expected length
    var index = 0;
    var location;
    do
    {
    location = string.find(splitter);
    if (location != null) {
    array[index] = string.substring(0, location);
    string = string.substring(location + 1, string.length());
    index++;
    }
    }
    while (location != null);
    array[index] = string;

    var result = new [index];
    for (var i = 0; i < index; i++) {
    result= array;
    }
    return result;
    }
    [/CODE]
  • This code doesn't need to know how many elements is in string.
    function toArray(text, delimiter)
    {
    var dict = {};
    var len = text.length();
    var delLen = delimiter.length();
    var idx = 0;

    while (len > 0)
    {
    var iend = text.find(delimiter);
    iend = iend != null? iend: len;
    dict.put(idx++, text.substring(0, iend).toNumber());
    text = text.substring(iend + delLen, len);
    len -= iend + delLen;
    }
    return dict.values();
    }
  • Former Member
    Former Member over 9 years ago
    True, but a dictionary is far less memory efficient as an array.
  • Yes but in this case, in fact, it does not matter because this memory will be freed very soon.

    Peter
  • Former Member
    Former Member over 9 years ago
    Yes but in this case, in fact, it does not matter because this memory will be freed very soon.


    I would expect that the dictionary uses significantly more compute power; I would expect that Garmin probably implemented the dictionary by using the hash of the string value of the key as the indexing. That is, the key is converted to a string (surprisingly expensive for numbers), then hashed, then dropped into the storage structure.

    I suggest coding up a linked list class for these sorts of tasks.
  • I suggest coding up a linked list class for these sorts of tasks.


    I'd stick with array, as long as you had a reasonable limit to work with. Building a linked list for this would impact not only available memory, but available objects (there's a limit on those too), and if there is no limit for the data and using a linked list, the app could crash when there is still free memory, but "max objects" has been hit.
  • I would expect that the dictionary uses significantly more compute power


    "Power" means CPU time. So make some tests and show as results.

    I would expect that Garmin probably implemented the dictionary by using the hash of the string value of the key as the indexing.


    I do not think so. Every object has its own 'hashCode' function and can be a key. So if the number is a key than its hashcode it is just that number.

    Peter
  • Former Member
    Former Member over 9 years ago
    thx! :)

    Thx for help :)