Tinderbox Forum

Extract N items from the beginning of a list and retain order

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.

  1. Why do you need unique? Does your list have duplicates?

  2. How about $MyList3.first(n) ?

  3. For your second approach, I’d suggest something like this.

var:list result="";
0...10.each(n) {
     result = result + $MyList3[n];

Haven’t tried this, but it ought to be straightforward.

So the list is:

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

The last 3 items appear different to the rest. Is that meant?

Ignoring the issue above, each ‘item’ is actually a single string of #-concatenated substrings.

So if the above list is in MyList, then:

var:list vList;
$MyList.each(anItem){
   vList = anItem.split("#").at(1);
   // write to $Text just to check
   $Text+= anItem.split("#").at(1)+"\n";
};

I make vList outside the loop in case the actual use is to do something with the extracted list.

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.
1 Like