Skip to main content
List attributes, e.g. MyList{}, are great for handling repetative data. But I seem to be stuck in a rather basic problem.

 

 

I'm trying to trim my list attribute down to a single entry (the first, index 0). I've used ListIndexer, but I can't seem to get it right.

 

 

Is there a simple way to accomplish this ? Or do I need to resolve to TCL or Python (if this is at all possible) ?

 

 

Cheers

 

Lars I Nielsen

 

You will want to use the ListExploder.  This will explode each list element to it's own feature. You can then use a Tester (_element_count = 0), to separate the first element.
You can also just use an attributecreator to rename MyList{0}
Listindexer should be able to do the trick.

 

 

But it wil only result in a single attribute if the list consist of just 1 attribute.

 

More attributes, more merrier :)

 

 

You also may want to dump the list bit after the indexer. Else u still tag along the list. (unless u still need it at that outputjuncture)
Hi Lars,

 

 

Do you perhaps need to remove list elements except the first one?  If so, there isn't a suitable transformer to do that exactly, but this combination is available. ListIndexcer (List Index: 0) AttributeRemover (Lists to Remove: MyList{}) AttributeRenamer (Old Attribute: MyList, New Attribute: MyList{0})   Of course, both the PythonCaller and the TclCaller can be also used. Python Example: -----

 

import fmeobjects def trimMyList(feature):     myList = feature.getAttribute('MyList{}')     if myList:         for i in range(1, len(myList)):             feature.removeAttribute('MyList{%d}' % i) -----

 

  Tcl Example:

 

-----

 

proc trimMyList {} {   for {set i 1} {{FME_AttributeExists "MyList{$i}"]} {incr i} {     FME_UnsetAttributes "MyList{$i}"   } } -----

 

 

Takashi

 


Thanks Gio and Takashi,

 

 

I prefer Takashi's answer with Python, as it's neat and clean.

 

 

However, in a workspace test setup it worked perfectly, but in my production setup it fails. I use ListPopulator in the test setup, but I use AttributeAccumulator in my production setup.

 

 

Apparently "LIST{}" (aka MyList{}) isn't available when using Attribute Accumulator :-(

 

 

The attribute set after AttributeAccumulator contain multiple "list" entries, like this:

 

LIST{0}.FIRST

 

LIST{0}.SECOND

 

LIST{1}.FIRST

 

LIST{1}.SECOND

 

LIST{2}.FIRST

 

etc.

 

 

How must I change the Python script to accomodate for this difference ?

 

 

Cheers

 

Lars I.
Mechanism is the same. ----- # Example 1 # Remove list elements except the first element. def trimList(feature):     subNames = ='FIRST', 'SECOND']     for sub in subNames:         subList = feature.getAttribute('LIST{}.%s' % sub)         if subList:             for i in range(1, len(subList)):                 feature.removeAttribute('LIST{%d}.%s' % (i, sub)) -----   Another approach. This works like the ListIndexer, AttributeRemover and AttributeRenamer. If there are many sub names, this might be more efficient. ----- # Example 2 # Save the first element, remove list and recover the first element. def trimList(feature):     subNames = f'FIRST', 'SECOND']     for sub in subNames:         value = feature.getAttribute('LIST{0}.%s' % sub)         feature.removeAttribute('LIST{}.%s' % sub)         feature.setAttribute('LIST{0}.%s' % sub, value) -----   Not tested. If there were typos, please correct.
I tried FME 2014 Beta which supports Null attribute value. It seems that Null cannot be set with Python / Tcl scripting. The Example 2 may not be available if the list can contain Null values.
Thanks Takashi,

 

 

Adding the ".name" part did the trick.

 

 

Cheers

 

Lars I.

Reply