Question

Count number of characters in multiple fields

  • 18 April 2024
  • 4 replies
  • 51 views

Badge +3

Hey.

I need to count the number of characters in all text fields of my dataset.

Any simple way to do this to avoid using multiple StringLengthCalculator ?


4 replies

Userlevel 4
Badge +17

Hi, you can use @StringLength(<string>) in e.g. AttributeManagers, AttributeCreators, ExpressionEvaluators etc. Could that be of help to you? 

You could use a BulkAttributeRenamer to create copies (Mode: Keep Original Attributes) of your attributes with a prefix, then use an AttributeManager to set all attributes with your prefix to the value “@Evaluate(@StringLength(@CurrentAttribute()))”

Or directly overwrite your attributes with their lengths using an ExpressionEvaluator with this expression: @StringLength(@CurrentAttribute())

Userlevel 6
Badge +33

Or ue the AttributeExploder to create a feature for every attribute and send them through the StringLengthCalculator.

Userlevel 4
Badge +18

If you need to know what length the fields have, because you need to create a database for the features you should use a schemascanner:

The Schema feature will contain a list of all Attributes with the max length of the attribute:

If you need to validate the length of the attributes because you need to know if it fits in the database you could use a AttributeValidator:

 

Userlevel 5
Badge +29

I like these problems, because there are so many ways it can be solved!! Yet another approach would be using a python caller with the input function being (leave the others as default)

    def input(self, feature: fmeobjects.FMEFeature):

for f in feature.getAllAttributeNames():
if f == '_count':
pass
else:
attr = feature.getAttribute(f)
if isinstance(attr, str):
feature.setAttribute(f, len(attr))

self.pyoutput(feature)

This will replace the attribute value with the length of it. You can also add logic to only do certain types, or ignore certain attributes (in this case i’m ignoring ‘count’ and only getting the length of strs). Using a couple of bulk renamers and an aggregator you can add the lengths back onto the original feature


 

 

Reply