String to integer using javascript

Swiftlikeninja's picture

Okay I ran into an interesting scenario trying to sort through an xml feed. The Structure I have has a number of completes field and Im trying to get a leaderboard set up with the top completes displayed but the feed has the completes as a string and as such the structure sort sorts them alphabetically. Meaning that 10 would actually come before 2 which can cause some major issues in sorting. I found that if I use a number splitter off the complete index and rebuild the structure w/ the kineme structure maker then sorting works but the structure Im using can range to over 400 members so manually doing this is out of the question and i don't think iterator is the answer. Can anyone assist me in a javascript answer on how to change a substructure in an entire structure list from a string to an integer?

Or even simpler, tell me if Im missing something in the structure sort?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Swiftlikeninja's picture
Re: String to integer using javascript

I've attached a sample composition to further exemplify My issues. In my actual comp im sorting a substructure but this gets the point accross.

PreviewAttachmentSize
sort test.qtz12.8 KB

gtoledo3's picture
Re: String to integer using javascript

This is one way to approach it; you take the structure, iterate it while converting with one splitter, rebuild the structure, then publish the output.

...it will only work in SL though.

PreviewAttachmentSize
sort test w iteration conversion.qtz22.16 KB

Swiftlikeninja's picture
Re: String to integer using javascript

I've tried the iterator queue technique you provided gtoledo3 but i noticed the fps drops to 12 from 59. Im iterating through 300+structures... its a work around and thank you for that. Hopefully we can find some other means of doing this.

dust's picture
Re: String to integer using javascript

this is how to cast a string to an int in javascript.

result.outputNumber = parseInt(inputString);

gtoledo3's picture
Re: String to integer using javascript

The problem with that is that it does the same thing as converting with an insert splitter, effectively (probably takes more time.. I'd be curious to profile it).

It's a structure of strings, that then needs to be converted to a structure of numbers.

Hmm, sorry to hear that the published out with the iterator is so slow for you - I've had it run pretty well, but each scenario is different... the main reason I came up with that was for structure smoothing, not conversion (you could place a smooth in there and it would work fine).

You've got me curious...

usefuldesign.au's picture
Re: String to integer using javascript

RE: Javascript: In simple cases the casting gets done automatically just by setting the output type to number.

Inside an array you need to use the parseInt or some other method like Dust said.

PreviewAttachmentSize
string2number casting.qtz14.9 KB

gtoledo3's picture
Re: String to integer using javascript

Oh, that made sense ... cool. I wonder how quick it will work, speed wise.

usefuldesign.au's picture
Re: String to integer using javascript

n/a fps!

For some things I find the Javascript patch is unnoticeably fast, as long as it doesn't have to loop toooo much. Crank 'size' up to 10000 numbers and you notice it being slow though in this comp. But for sorting 1000 numbers it's pretty much instantaneous eben on my old beast.

PreviewAttachmentSize
string2number casting II.qtz24.66 KB

dust's picture
Re: String to integer using javascript

i find using the qc structure sort very fast to make a leader board.

usefuldesign.au's picture
Re: String to integer using javascript

Apple should really sort out the ordering confusion that exists with  <QC_Structures> and item numbers and "n" key numbers when generating structures with JS patch. I've been going around and around in circles trying to write JS patches that unscramble these disordered structures. It's doing my head in and is a big time PITA!

dust's picture
Re: String to integer using javascript

?

what is a qc structure really ? NSDictionary? NSAray? Structure to file saves a .plist

usefuldesign.au's picture
Re: String to integer using javascript

Not sure NSDictionary aren't they(?!). I've heard that the problem is webkit, but smokris was saying it's something he's looking to fix in next version of structure tools too. I've got no idea what the underlying data types are but it seems to have something to do with the issue where you get a list of items and the order the list in the tool tip is different to the number inside the quotes (even when you didn't use a key).

I've been trying various Javascript ways of assigning items from one structure to another. Also sorting with the sort patch which is powerful (but as said in OP it sorts by alphanumeric not Float when comparing numbers so 72 is sorted before 8).

I've been messing around and even declaring a  = new Array() gives a different structure to  a = new Object even if all the other code is identical.

Here's a comp that makes comparing a little easier. Interesting to try different ways of assigning values in JS patch that generates the structures too like backwards counting of "Char" item etc...

PreviewAttachmentSize
string2number casting IIIii.qtz41.85 KB

gtoledo3's picture
Re: String to integer using javascript

It can be an array, or a dictionary. I would try something like:

    var result = new Object();

result.Input = new Array();

... to get a correctly ordered array when writing to file . "Inside" QC, this seldom matters.