Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

How to reverse a list order?

Kornscope Komachi
Transitional human
Join date: 30 Aug 2006
Posts: 1,041
07-14-2009 19:58
Hi team.
I'm trying to reverse a list (curtain sculpt-maps)
Found on wiki:
// Returns a new string containing the reverse of source.
string StringReverse(string source)
{
string str;
integer x;
integer len = llStringLength(source);
for (x = 1; x <= len; x++)
str += llGetSubString(source, len - x, len - x);
return str;
}

string SCULPT_MAPS is CSV from a notecard
I started with keys and changed to texture-strings e.g. 1map, 2map etc

Tried:
StringReverse(SCULPT_MAPS);
list maplist = llParseString2List(SCULPT_MAPS, [","], [""]);

But it don't seem to reverse the order
Am I on the right track? .
What else can I try? Thanks
_____________________
SCOPE Homes, Bangu
-----------------------------------------------------------------
Talon Brown
Slacker Punk
Join date: 17 May 2006
Posts: 352
07-14-2009 20:34
http://lslwiki.net/lslwiki/wakka.php?wakka=llListSort

Don't have time to do anything but provide a link, I'm sure someone else will be along to provide example code if you need it.
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
this should help...
07-14-2009 20:42
LIBRARY: reverse Find, and reverse order Functions


you also might find this useful

LIBRARY: Multi-ListFindList Forward and Reverse


the ones noted as "string only" actually will work on keys too.

PS thanks for reminding me that I need to add those to the wiki.
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -
Dora Gustafson
Registered User
Join date: 13 Mar 2007
Posts: 779
Reverse list elements, no Sorting
07-15-2009 02:14
From: Kornscope Komachi
Hi team.
I'm trying to reverse a list (curtain sculpt-maps)
Found on wiki:
What else can I try? Thanks
The way I understand you is:
You want to have the list elements in reversed order?
You don't want the list sorted?
You don't want the character order reversed? (the 'StringReverse' does that)

Here is a working sample that does just that:
CODE

list L = [10,20,30,15,-8];
default
{
touch_end( integer nu )
{
integer m = llGetListLength( L )-1;
while ( --m>=0 )
{
L = llDeleteSubList( L, m, m) + llList2List( L, m, m );
}
llOwnerSay( llDumpList2String( L, ", " )); // Show result
}
}
This is just one way to do it:)
_____________________
From Studio Dora
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
07-15-2009 10:24
First, the reason you aren't seeing any change is that you cannot modify a list IN PLACE. The function you are using RETURNS a string that is the reverse of its argument, and you have to do something with that return value. So the first line in your code that uses the function should be more like:

CODE

SCULPT_MAPS = StringReverse(SCULPT_MAPS);


(Or assign to a second string if you want to keep the first one around.)

Second, you are using a function that will reverse the WHOLE STRING, character by character. That's probably not what you want. I suspect you want to reverse the order of the logical entries in the CSV (between the commas) without reversing the characters in each. So you want to parse into a list FIRST, then use a function that does with a list what your 'StringReverse' does to a string. Something like:

CODE

// Returns a new list of strings containing the reverse of source.
list StringListReverse(list source)
{
list result = [];
integer x;
integer len = llGetListLength(source);
for (x = 1; x <= len; x++)
{
result += [ llList2String(source, len - x, len - x) ];
}
return result;
}

list maplist = llParseString2List(SCULPT_MAPS, [","], [""]);
maplist = StringListReverse(maplist);
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
Recursive reversal!
07-15-2009 10:39
Free gift to the first person to explain how it works and why you don't want to do it this way.
CODE

list listReverse(list l)
{
if(llGetListLength(l) <= 1) return l;

return listReverse(llList2List(l, 1, -1)) + llList2List(l, 0, 0);
}
_____________________
Argent Stonecutter - http://globalcausalityviolation.blogspot.com/

"And now I'm going to show you something really cool."

Skyhook Station - http://xrl.us/skyhook23
Coonspiracy Store - http://xrl.us/coonstore
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
07-15-2009 10:43
Hell of a call stack for long lists. :p But a binary (half and half) recurse wouldn't be so bad. O(log N) instead of O(N). Memory use would be (almost) just as bad though.
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
07-15-2009 10:53
From: Hewee Zetkin
Hell of a call stack for long lists. :p But a binary (half and half) recurse wouldn't be so bad. O(log N) instead of O(N).
ITYM O(NlogN) and O(N^2). :D

That's only because LSL doesn't do tail recursion optimization. If it did, that function would be O(N). I kept telling em, they need to provide a Lisp front end for LSL, the byte-code is already stack based, but no....

[lisp]
(def 'listReverse (l)
(cond ((cdr l) (cons (listReverse (cdr l)) (car l)))
(t l))))
[/lisp]
_____________________
Argent Stonecutter - http://globalcausalityviolation.blogspot.com/

"And now I'm going to show you something really cool."

Skyhook Station - http://xrl.us/skyhook23
Coonspiracy Store - http://xrl.us/coonstore
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
07-15-2009 10:56
LOL. Lisp in SL. Ouch. I did indeed mean O(log N) and O(N), but I was talking about only the depth of the call stack, not the overall run time complexity.
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
07-15-2009 11:03
From: Hewee Zetkin
LOL. Lisp in SL. Ouch. I did indeed mean O(log N) and O(N), but I was talking about only the depth of the call stack, not the overall run time complexity.
I'm talking about depth of the call stack, too. Without tail recursion elimination you get N copies of a list N elements long, that's O(N^2).

And if you think Lisp is scary:

[forth]
: listReverse recurse
dup llGetListLength 1 > if pop >R listReverse R> llAppendList then ;
[/forth]
_____________________
Argent Stonecutter - http://globalcausalityviolation.blogspot.com/

"And now I'm going to show you something really cool."

Skyhook Station - http://xrl.us/skyhook23
Coonspiracy Store - http://xrl.us/coonstore
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
07-15-2009 11:12
Oh , how about "⌷ ← list[⍒⍳⍴list]"? :D :D :D
_____________________
Argent Stonecutter - http://globalcausalityviolation.blogspot.com/

"And now I'm going to show you something really cool."

Skyhook Station - http://xrl.us/skyhook23
Coonspiracy Store - http://xrl.us/coonstore
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
07-15-2009 11:33
can you say stack heap collision with a quickness... I knew you could....

lsl+recursion = "bad, very bad" (said in best rain man imitation)

ETA and yes I see that all the functions as well as their reference would all be on the stack because lsl copies the whole damn variable
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
07-15-2009 14:40
From: Argent Stonecutter
I'm talking about depth of the call stack, too. Without tail recursion elimination you get N copies of a list N elements long, that's O(N^2).

And if you think Lisp is scary:

[forth]
: listReverse recurse
dup llGetListLength 1 > if pop >R listReverse R> llAppendList then ;
[/forth]


Okay. Let me say, "number of stack frames." Maybe that will clear things up. It's what "call stack depth" usually means. In any case, I think list contents go on the heap. But we're only disagreeing over terminology in any case; I think we both have a clear understanding of the matter.
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
07-15-2009 14:52
From: Hewee Zetkin
Okay. Let me say, "number of stack frames." Maybe that will clear things up. It's what "call stack depth" usually means. In any case, I think list contents go on the heap. But we're only disagreeing over terminology in any case; I think we both have a clear understanding of the matter.
OK, so how about that APL front end to LSL?
_____________________
Argent Stonecutter - http://globalcausalityviolation.blogspot.com/

"And now I'm going to show you something really cool."

Skyhook Station - http://xrl.us/skyhook23
Coonspiracy Store - http://xrl.us/coonstore
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
07-15-2009 19:28
From: Argent Stonecutter
OK, so how about that APL front end to LSL?

I think you're out of your mind. :-P And I suppose we'd need prim touchpads or something to allow the proper character entry. Heh.
Kornscope Komachi
Transitional human
Join date: 30 Aug 2006
Posts: 1,041
07-15-2009 20:29
Thanks for the help team!
Don't know why my searches didn't find Voids' work.
Learning all the time.
llAvatar2Avatar(animate HUGS,avatar MULTI)
_____________________
SCOPE Homes, Bangu
-----------------------------------------------------------------
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
07-15-2009 21:38
yw (and I can guess why... the terms "reverse" and "order" are pretty common =)

Dora's code is actually near identical to my small memory version, but her's will run a hair faster on small lists... mine is optimized to run on medium to larger lists, although it does one more loop than it actually has to to simplify the test logic for speed of repetition (and 1 operation on single item lists).... I'm not sure if hers pushes more running space than it needs to, by calling the delete and add on the same line though... something I should have tested, because it'd actually be an improvement on mine if it doesn't.
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
07-16-2009 06:46
From: Hewee Zetkin
I think you're out of your mind. :-P
Did you read what it said on the wrapper? "Contents: ferret".
_____________________
Argent Stonecutter - http://globalcausalityviolation.blogspot.com/

"And now I'm going to show you something really cool."

Skyhook Station - http://xrl.us/skyhook23
Coonspiracy Store - http://xrl.us/coonstore
Briana Dawson
Attach to Mouth
Join date: 23 Sep 2003
Posts: 5,855
07-16-2009 07:44
From: Kornscope Komachi
Hi team.
I'm trying to reverse a list (curtain sculpt-maps)
Found on wiki:
// Returns a new string containing the reverse of source.
string StringReverse(string source)
{
string str;
integer x;
integer len = llStringLength(source);
for (x = 1; x <= len; x++)
str += llGetSubString(source, len - x, len - x);
return str;
}

string SCULPT_MAPS is CSV from a notecard
I started with keys and changed to texture-strings e.g. 1map, 2map etc

Tried:
StringReverse(SCULPT_MAPS);
list maplist = llParseString2List(SCULPT_MAPS, [","], [""]);

But it don't seem to reverse the order
Am I on the right track? .
What else can I try? Thanks



Try this:
}
}
string=re-type(list in reverse order by hand, HAHA)
}
_____________________
WooT
------------------------------

http://www.secondcitizen.net/Forum/
Kornscope Komachi
Transitional human
Join date: 30 Aug 2006
Posts: 1,041
07-16-2009 16:48
From: Briana Dawson
Try this:
}
}
string=re-type(list in reverse order by hand, HAHA)
}

string=re-type(dnah yb redro esrever ni tisl, HAHA)
Done!
_____________________
SCOPE Homes, Bangu
-----------------------------------------------------------------
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
07-16-2009 20:36
From: Kornscope Komachi
string=re-type(dnah yb redro esrever ni tisl, HAHA)
Done!

oh btw, your original solution, only suffered from counting from the wrong end. if it'd started at -1, and run to negative length or at length-1 and run to 0 it would've worked fine.
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
07-17-2009 00:10
From: Void Singer
oh btw, your original solution, only suffered from counting from the wrong end. if it'd started at -1, and run to negative length or at length-1 and run to 0 it would've worked fine.

Not quite. See my first point. Even if reversing the order of the whole string (rather than the list) was the intent, there was another problem....
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
07-17-2009 07:33
gah, half awake, wasn't even looking at the type (ok and the fact that it needs to return len, -1)

it was the right conceptual track though...
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -