Skip to main content
Solved

Limit a list to its first entry ?

  • December 12, 2013
  • 8 replies
  • 30 views

Forum|alt.badge.img
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

 

Best answer by takashi

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 = ['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.
This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

8 replies

dustin
Influencer
Forum|alt.badge.img+31
  • Influencer
  • December 12, 2013
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.

ebygomm
Influencer
Forum|alt.badge.img+46
  • Influencer
  • December 12, 2013
You can also just use an attributecreator to rename MyList{0}

gio
Contributor
Forum|alt.badge.img+15
  • Contributor
  • December 13, 2013
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)

takashi
Celebrity
  • December 16, 2013
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

 


Forum|alt.badge.img
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.

takashi
Celebrity
  • Best Answer
  • December 17, 2013
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 = ['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.

takashi
Celebrity
  • December 18, 2013
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.

Forum|alt.badge.img
Thanks Takashi,

 

 

Adding the ".name" part did the trick.

 

 

Cheers

 

Lars I.