I'm an idiot (maybe this should be in the documentation?)

mrangel
05 Sept 2018, 14:09

A common thing you need to do with an object list is filter it down to just the ones that have a certain attribute. It's easy enough to write a function to do that, but I always wondered why there's a FilterByAttribute, but not FilterByHasAttribute (or whatever you'd call it).
I end up writing that function every time I start a new project.

Just realised: FilterByAttribute (AllObjects(), "description", null) will find all objects that don't have a description. And the corresponding FilterByNotAttribute() call will find all objects that do have a given attribute. Because it uses Equal() to test, so a null value is the same as not having the attribute.

Maybe this should be in the documentation.
Or even, a little tweak to make the usage slightly more intuitive:

  <function name="FilterByAttribute" parameters="lst, name, value" type="objectlist">
    if (not IsDefined("value")) {
      return (FilterByNotAttribute (lst, name, null))
    }
    l = NewObjectList()
    foreach (o, lst) {
      val = GetAttribute(o, name)
      if (Equal(val, value)) list add (l, o)
    }
    return (l)
  </function>

  <function name="FilterByNotAttribute" parameters="lst, name, value" type="objectlist">
    if (not IsDefined("value")) {
      return (FilterByAttribute (lst, name, null))
    }
    l = NewObjectList()
    foreach (o, lst) {
      val = GetAttribute(o, name)
      if (not Equal(val, value)) list add (l, o)
    }
    return (l)
  </function>

↑ With that little change (if I understand the rules right), the user could call FilterByAttribute (somelist, "someattribute"), omitting the third attribute to get a list of objects which have (or don't have) an attribute regardless of value. I think that's a fairly natural thing for a programmer to assume would be possible, so it would be nice to have it Just Work.


XanMag
05 Sept 2018, 17:02

I’ve got no input here but, based on your title, I’d be glad to put that you’re an idiot in the documentation. Haha.


Forgewright
05 Sept 2018, 17:22

Burn.
They are certainly handy functions. For now it would be nice to see your new functions(these and any others) in a Function.aslx library.


jmnevil54
05 Sept 2018, 23:31

New filter by attribute functions. FilterByNotAttribute() and FilterByAttribute (AllObjects(), "description", null).
Got it!
Yes, those should be in the tutorials!