Skip to content

InstallShield scripting: Create a list of distinct items from a given list

July 17, 2017

Recently I had a situation when I needed the ability to take a list of strings and eliminate duplicates.

Now, in all modern languages, this is a built-in capability or data structure – set() in Python, Distinct() in C#,  etc.

But InstallScript is too basic for that. Since we need dynamic arrays, all we have to work with is the LIST object. So, after some trial and error, I came up with this piece of code, which is quite efficient; the only downside is that the order of items is not kept – but in my case it didn’t matter.

prototype Distinct(BYVAL LIST,BYREF LIST); // Assumption: the output list is initialized and empty

function Distinct(listOrig, listUnique) 
    NUMBER nIndex, nResult; 
    STRING szItem; 
begin 
    // traverse the original list, and for each item in index n, check if it appears again along the list starting from n+1 
    // if not, add it to the new list 
    nIndex = 0; // list pointer 
    nResult = ListSetIndex(listID, nIndex); 
    while (nResult != END_OF_LIST) 
        ListCurrentString(listID, szItem);  // Get the current item
        // move list pointer to the next item, if exists  
        nIndex += 1; 
        nResult = ListSetIndex(listID, nIndex);  
        if nResult != END_OF_LIST then 
            nResult = ListFindString(listID, szItem); // check if the current item appears in the remainder of the original list
            if nResult = END_OF_LIST then // no duplicates found, so we can add this item to the unique list (note, previous appearances of it were ignored)
                ListAddString(listUnique, szItem, AFTER); 
            endif; 
            // reset the list index to the next item of the original list, since ListFindString() moves it to the element it found
            nResult = ListSetIndex(listID, nIndex); 
        else  //  this was the last item of the original list 
            ListAddString(listUnique, szItem, AFTER); 
        endif; 
    endwhile; 
end;

 

Advertisements

From → InstallShield

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: