尝试遍历Eiffel中的数组时出错

时间:2017-11-22 14:52:35

标签: multidimensional-array syntax syntax-error traversal eiffel

我收到错误unknown identifier 'start'

我的代码:

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

我只想解析2d数组的对象并在它们周围包装html标签 我的语法有问题吗?

1 个答案:

答案 0 :(得分:1)

班级ARRAY不会继承提供功能TRAVERSABLEstartafterforth的{​​{1}}。因此,无法使用这些功能遍历数组。相反,遍历通常使用从item开始并在lower结束的项目索引来完成。因此,在您的代码中,类型upper将有两个变量ij(一个用于内循环,另一个用于外循环),循环类似

INTEGER

另一种方法是使用循环的迭代形式:

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

后一种方法更接近当前代码中使用的方法,因此您可能更喜欢它。请注意,在这种情况下,无需拨打across t_array as x loop ... x.item ... -- Use current item of the array. end startoff,因为这是自动完成的。

最后,可以使用当前代码,但迭代forth。为此,您还需要另外两个类型linear_reprentationp的局部变量qLINEAR [ARRAY [ELEMENT]]。第一个将使用LINEAR [ELEMENT]进行初始化,外部循环将使用p := t_array.linear_representationp.startp.afterp.item。第二个将使用p.forth进行初始化。这是最麻烦的方法,我只是为了完整性而列出它。