1

I want to use array_foreach functionality. But it does not seems to work when used in an aggregate function.

This intermediate test ( array predefined, instead of array generated by another aggregate), is as follows:

array_to_string(
    array_foreach(array(208,209),
    ' ' || @element || ': '|| 
        aggregate(layer:= @z_PanneauxBase,
                aggregate:='concatenate',
                expression:=to_string("pa_uid"), 
                filter:= "Refpiquet" = @element,
                concatenator:='-')
) 
)

The response is ' 208: 1384-1385, 209: 1384-1385' ! strange: the same result for both elements of the array. The @element exported as text in the concatenation seems the correct one. And by the way the answer is wrong for both.

What did I do wrong, is @element supposed to work within an aggregate process?

For info, the syntax is shown as correct by the expression string builder.

Replacing the predefined array, by an aggregate function giving a similar array, gives the same error result.

QGIS, version LTR 3.28.4, using shapefiles.

11
  • 1
    We need the data structure you use to be able to see what goes wrong and the desired result. Can you provide sample data?
    – Babel
    Commented Nov 8, 2023 at 13:20
  • yes of course, but how to send you the shapefiles? for infon Refpiquet is a dynamic field
    – ClaudeVer
    Commented Nov 8, 2023 at 13:33
  • 1
    You provided a project with many layers. No idea what you want to do with the expression, where you try to apply it or what you want to achieve. You should tell us as clear as possible what you try to do. You should also provide a minimal reproducilble example, a simplified version of your project that only contains the minimal data to be able to understand the problem. Everything elso just adds to the confusion.
    – Babel
    Commented Nov 8, 2023 at 19:32
  • 3
    No, the file does not help. There are many layers missing in the project. Try to create a sample project as easy and small as possible to demonstrate only the problem, nothing else. If you have too many layers that are unnecessary, one doesn't know where to start.
    – Babel
    Commented Dec 7, 2023 at 15:07
  • 1
    I was able to reproduce the behavior with a single layer and two different "id" fields. it produces the same issue. no luck yet though
    – Kalak
    Commented Dec 8, 2023 at 7:22

1 Answer 1

2

There is a known bug with array_foreach and aggregate. As a workaround, using nested array_foreach may work, as in this question: QGIS functions: aggregate and array_foreach not working for vertices

I mocked up some panel data similar to yours:

Panel:

enter image description here

Start by building two arrays - the panels (@panel) and stakes (@stake).

Then loop through each of these checking if the stake_id matches. There is a step to store the outer @element variable in a different variable (@stake_element)

In addition, there are extra functions to reduce the arrays to strings (array_to_string), and to filter out non-NULL values (array_remove_all) in order to get the final output, but these can be removed if you are just trying to understand the core nested loop.

with_variable('panel',
    aggregate('Panel',
    aggregate:='array_agg',
    expression:=@feature
),
with_variable('stake',array(208,209),
array_to_string(
    array_foreach(@stake,
        with_variable('stake_element',@element,
            @stake_element ||':'|| 
                array_to_string(
                    array_remove_all(
                        array_foreach(@panel,
                            if(attribute(@element,'stake_id')=@stake_element,
                            attribute(@element,'panel_id'),
                            NULL)
                        ),value:=NULL
                    ),'-'
                ))))))

Result is:

'208:1194-1195,209:1196-1197'

Not the answer you're looking for? Browse other questions tagged or ask your own question.