Skip to main content
Solved

How to divide number to sequenced list?


lazarlubomir
Contributor
Forum|alt.badge.img+10

Hello everyone,

I try to solve problem with numbers and values. In my project, I manage SQL select count via SQL Executor, so the result of query could be for example number 5. But what I need in second step, I have to create list with minimal value 0 and maximum value (result of query) 5. So list will contain following values 0,1,2,3,4 and 5.

Any tips please?

Thank You so much!

Lubo

Best answer by david_r

The "obvious" solution would be to use a Cloner with your query result as number of copies, followed by a ListBuilder.

You can also accomplish the same thing using Python, it is probably also more efficient, performance-wise:

import fmeobjects
def generate_list(feature):
    items_to_create = int(feature.getAttribute('count'or 0) + 1
    items = [''] * items_to_create
    feature.setAttribute('list{}', items)

You need to supply an attribute "count" and it will return a list called "list{}" with count+1 elements containing an empty string, e.g. for count = 5:

`list{0}' has value `'
`list{1}' has value `'
`list{2}' has value `'
`list{3}' has value `'
`list{4}' has value `'
`list{5}' has value `'
View original
Did this help you find an answer to your question?

6 replies

david_r
Celebrity
  • Best Answer
  • December 12, 2017

The "obvious" solution would be to use a Cloner with your query result as number of copies, followed by a ListBuilder.

You can also accomplish the same thing using Python, it is probably also more efficient, performance-wise:

import fmeobjects
def generate_list(feature):
    items_to_create = int(feature.getAttribute('count'or 0) + 1
    items = [''] * items_to_create
    feature.setAttribute('list{}', items)

You need to supply an attribute "count" and it will return a list called "list{}" with count+1 elements containing an empty string, e.g. for count = 5:

`list{0}' has value `'
`list{1}' has value `'
`list{2}' has value `'
`list{3}' has value `'
`list{4}' has value `'
`list{5}' has value `'

lazarlubomir
Contributor
Forum|alt.badge.img+10
  • Author
  • Contributor
  • December 12, 2017
david_r wrote:

The "obvious" solution would be to use a Cloner with your query result as number of copies, followed by a ListBuilder.

You can also accomplish the same thing using Python, it is probably also more efficient, performance-wise:

import fmeobjects
def generate_list(feature):
    items_to_create = int(feature.getAttribute('count'or 0) + 1
    items = [''] * items_to_create
    feature.setAttribute('list{}', items)

You need to supply an attribute "count" and it will return a list called "list{}" with count+1 elements containing an empty string, e.g. for count = 5:

`list{0}' has value `'
`list{1}' has value `'
`list{2}' has value `'
`list{3}' has value `'
`list{4}' has value `'
`list{5}' has value `'
Yes, it works :-) But how can I fill values for every element of list?

 

For example: 
  1. `list{0}' has value 0
  2. `list{1}' has value 1
  3. `list{2}' has value 2
  4. `list{3}' has value 3
  5. `list{4}' has value 4
  6. `list{5}' has value 5
Thank You so much!

 

 


david_r
Celebrity
  • December 12, 2017
lazarlubomir wrote:
Yes, it works :-) But how can I fill values for every element of list?

 

For example: 
  1. `list{0}' has value 0
  2. `list{1}' has value 1
  3. `list{2}' has value 2
  4. `list{3}' has value 3
  5. `list{4}' has value 4
  6. `list{5}' has value 5
Thank You so much!

 

 

For example:

 

import fmeobjects
def generate_list(feature):
    items_to_create = int(feature.getAttribute('count'or 0) + 1
    for n in range(items_to_create):
        feature.setAttribute('list{%s}' % n, n)

takashi
Evangelist
  • December 13, 2017

Interestingly the XMLXQueryExtractor with this XQuery expression works as well. Just for your information.

fme:set-list-attribute("list{}", (0 to xs:integer(fme:get-attribute("count"))))

# I personally call this transformer "XQueryCaller".


david_r
Celebrity
  • December 13, 2017
takashi wrote:

Interestingly the XMLXQueryExtractor with this XQuery expression works as well. Just for your information.

fme:set-list-attribute("list{}", (0 to xs:integer(fme:get-attribute("count"))))

# I personally call this transformer "XQueryCaller".

That's an interesting hack. Guess you could call that one the XQueryListPopulator

lazarlubomir
Contributor
Forum|alt.badge.img+10
  • Author
  • Contributor
  • December 15, 2017
takashi wrote:

Interestingly the XMLXQueryExtractor with this XQuery expression works as well. Just for your information.

fme:set-list-attribute("list{}", (0 to xs:integer(fme:get-attribute("count"))))

# I personally call this transformer "XQueryCaller".

Great tip @takashi! It works too and probably it is more suitable solution!

 


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings