Ugly Code
Gotta love proprietary templating languages
Here is a seriously cursed piece of code that I wrote today
if(contains(GetVar("lots"), LOT), '',
LOT)
+
if(Contains(GetVar("lots"), LOT) ,'', left$(SetVar("lots", GetVar("lots") + ' ' + LOT),1))
At first it might not look too horrible, but the more you look at it the worse it gets, I will tell you the tale of how this thing came into existence.
So I’m writing a template for some papers, and I get lot numbers, quite a lot of them (pun intended), but we don’t really want to have all the duplictes printed.
I think “nice” I know that the templating software has a Previous() function that lets me retrieve the value of a field in the previous row, I’ll just use that, and it will work.
so I wrote a nice little if, and thought myself finished
if(Previous(LOT) != LOT, LOT, '')
It was simple, it was nice, the problem is that the lots of course don’t come sorted, so we got this
lot1
lot2
lot1
So I had to come up with some way to deduplicate it, I looked so long for some kind of deduplicaton thing in the software, but the sum functions only could sum numbers, and not strings, so I had to come up with something else.
After some searching I found the function pair SetVar and GetVar, and those would set a local variable in the software for this run through of the template.
There is no collections, but since this is a templating language at least we have strings..
So to add it we concatenate our new lot with a space in front of it to separate them, and for something we will get to now. In one field of the report you are only allowed to use one statement, and the setvar function return the result of the set thing.. Which means we check if the Lot is already in our lot string, and then print the first character of it, which is a space, and since this is the last thing in our return string it just looks as if it’s empty..
I’m not looking forward to maybe having to debug this in the future.