|
Robby Lawl
Registered User
Join date: 17 Feb 2007
Posts: 7
|
06-14-2007 10:12
I'm fairly new to scripting so please forgive my ignorance.
I've been racking my brains from days how to do this.
What I want to do is create a list (strided) of people who come into the area and track how many times sensed while there. (Using a Strided List)
so basically I end up with [1, Name1, 3 Name2, 8, Name3] in the List (Global List)
also I have it making another list (not strided since I only care about the names [Name1, Name2, Name3, Name4] in a Local list so it gets re-created every sensor event.
SO here's where I get stuck....
how do I compare the 2 lists.
If I compare Global to Local list and find a name in the Local but not in Global I want to add it.
If I find the name in both I want to increment the count
If I don't find the name in Local and its in Global I want to remove it from Global.
so as an example of a sweep...
Global [8,Name1,4,Name2, 2,Name3] Local [Name1,Name3,Name4]
so I end up with
Global [9,Name1,3,Name3,1,Name4]
Hope that makes sense, any help would be appreciated...
|
|
AnnMarie Otoole
Addicted scripter
Join date: 6 Jan 2007
Posts: 162
|
06-14-2007 13:07
You first do an llListFindList on each list with the new name. If it is not in a list you will get -1, otherwise you will get the index number. You then do a series of "if" clauses on the two index results to perform the desired action.
I dislike working with strided lists (especially as the stride length changed periodically in my application requiring massive reformatting). I don't like the way it works and instead create separate lists, in your case one list for Global Names and another for Global counts. Entries in the Count list correspond 1 to 1 with index locations in the Global Name list. That way if you have to add another data parameter you don't have to change the stride length, just simply add another list. They are less prone to accidents and easier to handle. The downside is if you do a llList2CSV the data are not listed beside each other.
|
|
Jim Guyot
Tinkerer
Join date: 21 Apr 2007
Posts: 38
|
06-14-2007 13:30
Although, you can do a simple for loop to make multiple lists usable as a CSV:
for (i=0;i<llGetListLength(test1);i++) { bigList += test1; bigList += test2; bigList += test3; }
llList2CSV( bigList );
I also have to agree, strided lists are difficult to use at best. You can only access the first item of a stride with the strided functions. And making changes to the code down the road becomes a tremendous problem.
|
|
Micheal Moonlight
Registered User
Join date: 4 Sep 2005
Posts: 197
|
06-14-2007 13:46
the for loops was close to what i would of done...
for (integer i=0;i<llGetListLength(local);++i) { integer tempint = llListFindList(global,[llList2String(local,i)]); if (tempint > -1) { increase the total found in global for the match. } else { add name to global; } }
|
|
Milambus Oh
Registered User
Join date: 6 Apr 2007
Posts: 224
|
06-14-2007 13:55
Another warning about Lists in general. They are very bad on memory. I have a script that uses a list of names similar to the one you are wanting, with some other associated lists for data on that name (as is being suggested on the previous posts) and I find that I can only handle about 60 names with associated data before I run into Stack-Heap collisions, thats with optimizing my code as much as I have been able to do so far. (Everything that does not directly work on the user data is tasked out to other scripts, including llInstantMessages because the memory usage for the strings was hogging too much memory.)
So bottom line, be very careful with those lists.. 16k runs out faster than you think it will.
|
|
Robby Lawl
Registered User
Join date: 17 Feb 2007
Posts: 7
|
06-14-2007 22:40
I thought about doing 2 seperate lists for the name and the count, but couldn't figure out how to keep the lists lined up, especially when removing a name. Here is what I have so far, and its close to working except the count seems to always add to 1 name all the counts. (ie. if to agents present, the count goes up 2 for the first name and nothing for the second) // LSL203 Example Script 6 // Author: Johanna Hyacinth // Modded: Robby Lawl
list Visitors;
default {
//----------------------------------------------------------------------------------------------- // When the script begins, set up a repeating sensor, which triggers every thirty seconds, to // detect all avatars in a sphere with a radius of 40 meters. // state_entry() { llSensorRepeat( "", "", AGENT, 3.0, PI, 5.0 ); } //----------------------------------------------------------------------------------------------- // When the sensor event is triggered, scan through the list of detected avatars. If any given // avatar is not already in our list, add it. If the avatar is already in the list, increment // the detection count for that particular avatar. // sensor( integer num_detected ) { integer i; list Output; for (i = 0; i < num_detected; ++i) { string Name = llDetectedName( i ); Output += llDetectedName(i); integer pos = llListFindList( Visitors, [ Name ] ) - 1; if (pos == -2) { Visitors += [ 0, Name ]; }
integer j; list Temp; integer visitCount = llGetListLength( Visitors ); for (j = 0; j < visitCount; j +=2 ) { integer Count2 = llList2Integer( Visitors, j ); string Name2 = llList2String( Visitors, j+1 ); if(llListFindList(Output, [Name2]) != -1) { Count2 = Count2 + 1; Temp += [ (string)Count2, Name2 ]; } } Visitors = Temp; } } no_sensor() { Visitors = []; } //----------------------------------------------------------------------------------------------- // When the scripted object's owner touches it, sort the list in descending order based on how // many times each avatar was detected, and report this data to the owner. touch_start( integer num_detected ) { if (llDetectedKey( 0 ) == llGetOwner()) { integer i; integer itemCount; Visitors = llListSort( Visitors, 2, FALSE ); llOwnerSay( "Visitors:" ); itemCount = llGetListLength( Visitors ); for (i = 0; i < itemCount; i += 2) { string count = llList2String( Visitors, i ); string Name = llList2String( Visitors, i+1 ); llOwnerSay( " " + Name + " - " + count ); } } } }
I'm very open to a better way to do this....
|
|
Robby Lawl
Registered User
Join date: 17 Feb 2007
Posts: 7
|
06-15-2007 14:15
Ok I finally figured out where I was going wrong....had the end point encompassing to much for a FOR loop...
Thanks everybody for the responses....now to clean up the mess I made in the code...
Going to try to write the same thing without the strided lists as well
|