![]() |
Sorting numerical data to index
I'm terrible with this sort of stuff - not just in GS2 but in Python aswell.
I'm basically after a function that sifts through a list file (probably not the right term for gs2, but basically what I mean) and resorts the list so the index of said list matches to the contents being sorted in a ascending or descending order. So basically, I feed it a list with the contents [11,24,55,9,17] and the variable to sort ascending, and it returns: list[0] = 9 list[1] = 11 list[2] = 17 list[3] = 24 etc, etc But not only that, I'd need the function to be able to do this and reassign two pieces of data, so each number would be linked to a string. (list[0] = "9, raeiphon" - picture a scoreboard as such) This would be easy if GS2 supported tuples, but I've been gone from the scripting scene for so long I can barely remember anything about it. I'm not asking for a script that does exactly this. I'm asking for advice on what methods I should take to properly learn how to do this - what to read up on, what to think, what to sort. A script would be nice if it was properly commented and whatnot, but I doubt anyone would be willing to spare the time to do that, if it's as hard as I think it is. There's probably a really simple way to do this too. -sigh- |
Copy a quicksort implementation or something from wikipedia, port it to gscript, replace the comparison with one that first splits the number out of that string and only orders by it?
|
Quote:
PHP Code:
PHP Code:
Hope this helps! |
That does it perfectly, Novo.
Thanks a ton. I'll spend some time dissecting that script. On a completely unrelated note, is it possible to call a string as a function - and on that train of thought, is it possible to use a string to mask an object so I can access that object's attributes? Say foo = "calculate()", and I have a function of the same value. Is there any way of using foo to execute that function? For the second one, say I had a string foo = "Command". This is in a class - and it's linked to a script with a gui element named Command. Is there some way of going foo.text (therefore linking Command.text)? |
You can use ("string")() to call a function, and use ("string").attribute to access an object.
|
Quote:
|
Quote:
|
Quote:
|
I don't think you're ready to experiment with dynamic executional programming. In the majority of the cases, you don't need dynamic execution. Using them makes debugging harder and encourages unconventional hacks rather than well-thought-out solutions.
|
Right, another question:
Does GS2 have some sort of semaphore I can use to tell a specific function to idle and wait until data from the server is received? I figured I could do this by setting a while loop which runs a 'idle' task until a onActionServerside -> onActionClientside bounce retrieves the data and updates the variable controlling the while loop, simultaneously ending the idling session and returning the function with the proper data. Pretty rudimentary stuff, but I'm terrified of trying it out in fear that there's no anti-infinite loop control on the server end, and that ****ing around with this stuff will lag the server I'm currently on to ****. Normally in Python I'd just thread major functions or aspects and plonk semaphores whereever I needed them - I'm not sure if GS2 has the functionality with threading at all |
Quote:
And the function waitfor(object, eventname, time); may be what you're looking for. The function halts the function calling it and waits for the specified event on the specified object for the specified amount of time. If the time runs out then waitfor() returns false, otherwise returns true if the event it's waiting for happens. I created a class for weapons that wraps triggerserver() and triggerclient() and allows functions to be called on the other side and wait for their return value. The script is in the Code Gallery. Quote:
|
Apparently no one appreciates my quicksort implementation (and introsort, which has an upper bound complexity of O(n log n) and average complexity of O(n log n) because it switches from quicksort to heapsort based on a certain threshold):
http://forums.graalonline.com/forums...ad.php?t=81279 Or maybe I just scared off everyone with the first function in that thread for efficient string searching. |
Yea... WhiteDragon's code is marginally better than mine because I use an insertion (which has a O(n)... Making my code O(n^2) in worst case) while WhiteDragon's model uses qsort( which doesn't insert, but swaps values. No memory displacement ).
I think mine's easier to understand though, and for someone who isn't familiar with algorithms... Might be a start. |
All times are GMT +2. The time now is 02:57 AM. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.