0

I get an error unknown identifier 'start'

my code:

visit_table(table: ETABLE)
    local
        t_array: ARRAY[ARRAY[ELEMENT]]
        b_header: BOOLEAN
        row_index: INTEGER
    do
        t_array := table.t_array
        b_header := table.b_header
        table.content := "<table>"
        row_index := 0

        from t_array.start
        until t_array.off
        loop

            table.content := table.content + "<tr>"
                from t_array.item.start
                until t_array.item.off
                loop
                    if row_index = 0 and b_header = TRUE then
                        table.content := table.content + "<th>" + t_array.item.content + "</th>"
                    else
                        table.content := table.content + "<td>" + t_array.item.content + "</td>"
                    end
                end
                table.content := table.content + "</tr>"
                row_index := row_index + 1
        end

        table.content := table.content + "</table>"
    end

I just want to parse through the objects of the 2d array and wrap html tags around them.
Is there something wrong with my syntax?

vkoukou
  • 140
  • 2
  • 15

1 Answers1

1

The class ARRAY does not inherit TRAVERSABLE that provides features start, after, forth and item. Therefore, arrays cannot be traversed using these features. Instead, the traversal is usually done using item indexes that start from lower and finish at upper. So, in your code there would be two variables i and j (one for the inner loop and another for the outer loop) of type INTEGER and the loops like

from
    j := t_array.lower
until
    j > t_array.upper
loop
    ... t_array [j] ... -- Use item at index `j`.
    j := j + 1 -- Advance to the next element of the array.
end

An alternative is to use an iteration form of the loop:

across
    t_array as x
loop
    ... x.item ... -- Use current item of the array.
end

The latter approach is closer to the one used in your current code, so you may prefer it more. Note that, in this case there is no need to call start, off and forth, because this is done automatically.

Finally, it's possible to use your current code, but iterate over linear_reprentation. For that you would need 2 more local variables p and q of types LINEAR [ARRAY [ELEMENT]] and LINEAR [ELEMENT] respectively. The first one would be initialized with p := t_array.linear_representation and the outer loop would use p.start, p.after, p.item and p.forth. The second one would be initialized with q := p.item.linear_representation. This is the most cumbersome method and I'm listing it only for completeness.

Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35