« Notes/Domino 8 available from August 17 | Main| AppendRTItem »

Reversing the order of a list in formula language

Tags: development

Some time ago I implemented a location search for one of our customers. The search results are company names and addresses. Today I was asked to also display the product groups for every company in the search results. When I showed this to the customer he said "That's great, but can you display the product groups in a specific order?". Of course, the company documents do not contain the product groups in that order. Since the product groups are set up in a profile document I thought it might be better to let the customer set the order in the profile document and use that list to determine the order of the elements in the list I'm displaying.

So, how do you order the elements of a list accordingly to another list? @Keywords is the answer.The help says "Given two text lists, returns only those items from the second list that are found in the first list.". Well it does that and it does it in the order of the elements in the second list.

_orderedlist := "elephant" : "zebra" : "crocodile" : "ostrich" : "lion";
_unorderedlist := "ostrich" : "zebra" : "lion";
_myorderedlist := @Keywords(_unorderedlist; _orderedlist);

So _myorderedlist now contains "zebra" : "ostrich" : "lion".

But, since I am displaying the product groups as an unordered list and the list is formatted so that all elements float right I needed to reverse the order of the list's elements. I remembered the rarely used [ToKeyword] keyword of the @Name function. It can be used to reverse the order of name components so that you can create views categorized by user's name components. The rest was easy:

_forward := "foo/" + @Implode(_myorderedlist; "/");
REM {_forward = "foo/zebra/ostrich/lion"};
_reverse := @Name([ToKeyword]; _forward);
REM {_reverse := "lion\zebra\ostrich"};
_reverselist := @Explode(_reverse; "\\");

I had to implode the list into a string separated by slashes to mimic a canonical name. Also, since [ToKeyword] strips the common name part, I had to prepend "foo" so that it could be thrown away. The list elements in the string are reversed and finally exploded.

I just love what you can do with a few lines of @formula code, especially when it comes to lists.

Disclaimer: No animals or lists were harmed in the making of this blog entry.

Comments

Gravatar Image1 - Using @Name([ToKeyword) is a very clever idea, thanks !

Am I right that zebra\ostrich should be the other way round in this example?

Thanks,
Peter

Post A Comment

:-D:-o:-p:-x:-(:-):-\:angry::cool::cry::emb::grin::huh::laugh::lips::rolleyes:;-)

Search

Calendar

MiscLinks

We Use Ytria Lotus Notes Tools For Faster Notes Development and Better Domino Administration

Tags

Site Info