Skip to main content
Question

Count number of characters in multiple fields

  • April 18, 2024
  • 4 replies
  • 350 views

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

joepk
Influencer
Forum|alt.badge.img+22
  • Influencer
  • April 18, 2024

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())


nielsgerrits
VIP
Forum|alt.badge.img+66

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


jkr_wrk
Influencer
Forum|alt.badge.img+37
  • April 18, 2024

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:

ย 


hkingsbury
Celebrity
Forum|alt.badge.img+71
  • Celebrity
  • April 18, 2024

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


ย 

ย