Jump to content

Find duplicates in a list and return the number of items between them


Ruthven Ravenhurst
 Share

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

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

Recommended Posts

I'm trying to develop this little snippet for a test for a bigger script. I can't see what's wrong with it, but it gives me the error, "Not all code paths return a value" when I try to save it. Can you point it out, or show me a better way? Thanks

//Find the last 2 instances of a duplicate item in a list and tells the distance between them. returns -1 if there is only 1 instance of it and -2 if there are no instances

list test = ["test1","test2","test3","test4","test5","test6","test7","test8","test9","test10","test11","test12","test13","test14","test15","test16","test17","test18","test19","test20","test21","test22","test1","test2","test3","test4","test5","test6","test7","test8","test9","test10","test11","test12","test13","test14","test15","test16","test17","test18","test19","test20"];

integer listdist(string check)
{
    integer idx = llListFindList(test,[check]);
    integer int;
    if(idx == -1){int = -2;return int;}
    else
    {
        integer len = llGetListLength(test)-1;
        integer first;
        integer foundfirst = FALSE;
        for(len;len > -1; --len)
        {
            if(llList2String(test,len) == check)
            {
                if(!foundfirst){first = len; foundfirst = TRUE;}
                else{return first - len;}
            }
        }
        if(foundfirst){int = -1; return int;}
    }
}
default
{
    state_entry()
    {
        llSay(0,(string)listdist("test20"));
    }

    touch_start(integer total_number)
    {
        llSay(0, "Touched.");
    }
}

 

Link to comment
Share on other sites

you need a return after the else...

add an ...integer returnVal;

and change your statements like ...

return first - len; .... to ....returnVal = first - len; ( for all your return calls)

and after the end of the ELSE... do...

return returnVal;

 

Edited by Xiija
Link to comment
Share on other sites

integer listdist(string check)
{
    integer idx = llListFindList(test, [check]);
    if(idx == -1){ return -2; } //if there is none to start with: return -2 then it will not run the rest.
    
    list temp = test; //Make a list that is the copy of the actual list
    temp = llDeleteSubList(temp, 0, idx); //Delete all the entries from start to first one found.

    idx = llListFindList(temp, [check]);
    if(idx == -1){ return -1; } //if there is no more left in the temp list after we took the first one found: return -1.
    return idx;
}

Should do what you wanted.

Link to comment
Share on other sites

12 hours ago, Necati Millet said:

integer listdist(string check)
{
    integer idx = llListFindList(test, [check]);
    if(idx == -1){ return -2; } //if there is none to start with: return -2 then it will not run the rest.
    
    list temp = test; //Make a list that is the copy of the actual list
    temp = llDeleteSubList(temp, 0, idx); //Delete all the entries from start to first one found.

    idx = llListFindList(temp, [check]);
    if(idx == -1){ return -1; } //if there is no more left in the temp list after we took the first one found: return -1.
    return idx;
}

Should do what you wanted.

Not quite, what I'm wanting is from the end of the list, not the beginning of this. The reason is because users can add themselves to the list, and I want them to be able to add themselves more than once with a threshold of a minimum number of other users between each of their entries. So what I want this to do is find the LAST 2 times (if there is more than one) and tell me how many entries are between them.

 

Now that I think about it, I guess I don't need to do all that, I just need to find their latest entry, subtract that index from the list length and if the returned amount is more than the threshold, they can be added again

Link to comment
Share on other sites

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

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

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...