Jump to content

Updating a variable in a list


CampSoup1988
 Share

You are about to reply to a thread that has been inactive for 4116 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Ok, lets say I want to track the donations of three people.  I have a variable called DonationTotal which stores [0,0,0]

I have tried a number of ways to update lets say the second number in the list, but I keep getting errors.

 

Oh, and Current has the integer 2.

 

These are the different methods I have tried and their errors:

llList2Integer(DonationTotal, Current) += amount;

 but I got: The left-hand side of an assignment must be a variable, property or indexer

 

DonationTotal[current] += amount;

 and got:

Expected ; or = (cannot specify constructor arguments in declaration)
; expected
Invalid expression term '+='
; expected

 

I also tried a few other similar combinations, which got me the same results of those previous two attempts.

 

If anyone has any advice, I would be greatly appreciated!

 

Thank you,

CampSoup1988

Link to comment
Share on other sites

Hi, LSL llists look like they should behave as arrays from other languages, but they do not. :( We need to use helper functions.

We can't do DonationTotal[current] += amount;

but instead you might use --

DonationTotal = llListReplaceList(DonationTotal, [llList2Integer(DonationTotal, current) + amount], current, current);

The LSL List category page has more information and other options to juggle them.

Link to comment
Share on other sites

So I have to replace the entire list....

 

I was kind of hoping to avoid that as in reality the list would be a lot longer then just three varaible.  Actually it is open ended depending on how many staffers are added to the list

 

And yes, I had previously dug deeply into that wiki.

Link to comment
Share on other sites

Knowing the list length isn't an issue, since it is available using llGetListLength(list).  The typical way of doing this is already laid out in the wiki as an example function from the LSL_List page, though you'd have to alter it to use an index instead of 'searching' the list for a match (which isn't hard.)

 

ListItemReplace function

 

If you want to have it do something other than strings, you can change the function parameters to utilize another type, or even generalize the function by extending it to take in an integer that is the return from a llGetListEntryType() call, and use that to convert the input 'newvalue' to the correct type.

 

Link to comment
Share on other sites

As written, the function uses llListFindList to create the variable placeinlist, but if you already know where the item you want to replace is, you don't have to look for it.  Just pass the function your known value of placeinlist and then use llListReplaceList to do the update.

Link to comment
Share on other sites

This code below costs many creation of lists .. Let s cross fingers that the garbage collector works well

 

 

list ListReplaceIntegerElementByIndex(list l , integer index , integer value){    return llList2List(  l , 0, index -1 ) + (list)value +  llList2List( l ,  index +1 , -1 );}default{    state_entry()    {        // displays Object: 10, 20, 30, 40, 1000, 60, 70, 80, 90        llOwnerSay(llList2CSV( ListReplaceElementByIndex( [ 10, 20, 30, 40, 50, 60, 70, 80, 90 ], 4, 1000)));    }}

 

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 4116 days.

Please take a moment to consider if this thread is worth bumping.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...