I need to build a table header from a list. I just need the first N items from the list. In this context, the order of this list REALLY matters. I need to be able to exact the N number of items into a new list and have the order preserved.
I can know/can count the value of N.
Here is an example of the list:
``
Alias Manager#Assurant#4;Alias Manager#1F-Secure#0;Alias Manager#NortonLifeLock#1;Password Manager#Assurant#3;Password Manager#1F-Secure#4;Password Manager#NortonLifeLock#3;Tracker Manager#Assurant#3; Assurant;1F-Secure;NortonLifeLock.
Youāll note that the second item in each item in the list is an organization name.
Iāve tried: $MyList3=$MyList3.unique; to retrieve the unique organization names. This does not work as this returns the list in alphabetical order: 1F-Secure;Assurant;NortonLifeLock. It is essential that I retain the order of the organization names, in this case: Assurant;1F-Secure;NortonLifeLock. This list will be automatically generated. It could have as many as 15 repeating sets with 10~15 more times, and will change often, so finding a way to ensure the order is preserved is a must.
I tried using each with a counter, e.g. only cycle through .each N number of times and then stop. I canāt seem to get this idea to work, .each cycles through the entire list until it is completed with the list.
I was thinking of doing some kind of $MyList[1ā¦N] loop operation in a function, but I canāt work through the logic, e.g. run function N times to build the new list.
Or, Iāve I had this list `Assurant;1F-Secure;NortonLifeLock;Assurant;1F-Secure;NortonLifeLock;Assurant;1F-Secure;NortonLifeLock;``` how do I lop off just the first N number of items, i.e.,
Does anyone have a clue how I could do this? I suspect Iām missing something very simple.
Have the solution. @mwra privately messaged me the following.
Yes! Implement a counter:
// use only the first 4 items of the iterated list stored in $SomeList
// Assumption: $MyList is already sorted so desired items
// are #1 through #4
var :number vCount = 1;
$MyList.each(anItem){
if(vCount<5){
// anItem is one of first 4 items in list
// so add to $Text
$Text += anItem + "\n";
};
vCount+=1 // increment vCount by one
};
^^^ tested in b550. If $MyList is āant;bee;cow;dog;eelā, $Text ends up as
ant
bee
cow
dog
Note that item #5 ("eel) is not added o $Text. You can use a 0-based or 1-based count as you prefer, as long as you do your maths right. so if vCount were zero-based you could check for the vCount value being <4 or <=3, etc.
MB also reminds me that List/Set.first can be used with a number, so:
$MyList2 = $MyList.first(3);
sets $MyList2 to (just the first three items in $MyList)
MB also reminds me that List/Set.first can be used with a number, so:
My Reply:
$MyList2 = $MyList.first(3);
Argh!!! This is what I was trying to remember!
This method cuts the complexity of the code, both in terms of size and operations. Whew!!! Thanks.
Iāve added both to the demo Iāll share later.