[stringtemplate-interest] Multivalued attributes and iteration over inner and outer loops

Ollerton, Robert M CIV SPAWARSYSCEN-PACIFIC, 71120 bob.ollerton at navy.mil
Thu Oct 29 13:13:11 PDT 2009


Greetings,

I do not understand how to formulate a group of templates that can be
used to iterate over the elements in lists of lists.  I would like to
understand how to use string templates generate the same output that
this program does. For example, the following program generates this
output when given 2 and 3 as inputs:

s0
    e0 -> s0
    e1 -> s1
    e2 -> s0
s1
    e0 -> s0
    e1 -> s1
    e2 -> s0

What would a set of templates, preferably in a single group template
file, look like?

Thanks,
Bob

/**
 * The program
 */
class S {
    final String s;
    final T[] t;

    S(String s, int n) { this.s = s; t = new T[n]; }
    public String getS() { return s; }
    public T[] getT() { return t; }
    
    public String toString(){
        StringBuilder sb = new StringBuilder();
        sb.append(s);
        for (T temp: t)
            sb.append("\n    " + temp.getE()+ " -> " + temp.getD());
        return sb.toString();
    }
}
class T {
    final String e;
    final String d;

    T(String e, String d) { this.e = e; this.d = d; }
    public String getD() { return d; }
    public String getE() { return e; }
}
public class Test {
    public static void main(String[] args) {
        final int j = Integer.parseInt(args[0]);
        final int k = Integer.parseInt(args[1]);
        S[] s = new S[j];
        for (int outer = 0; outer < j; outer++) {
            s[outer] = new S("s" + outer, k);
            for (int inner = 0; inner < k; inner++) {
                s[outer].getT()[inner] = new T("e" + inner, "s"
                        + (inner % j));
            }
        }
        
        for (S v: s)
            System.out.println(v.toString());
    }
}


More information about the stringtemplate-interest mailing list