[ic] Loop with embedded Perl

Ethan Rowe ethan at endpoint.com
Mon Aug 1 10:41:24 EDT 2005


Jeff Boes wrote:

>I'm stuck on this:
>
>        <select name="year">
>        <option value="">Select one</option>
>        [loop list=`1981..1900+(localtime())[5]`
>         ] <option value="[loop-code]">[loop-code]</option>
>        [/loop]
>        <option value="prior 1981">1980 or older</option>
>        </select>
>
>What I'm attempting to generate is a drop-down list with a series of
>year values, 1981 to the current year.
>
>If I can get this working, what I'd really like is ..
>
>list=`reverse 1981..1900+(localtime())[5]`
>
>but at present neither of these works; I get an empty list and only the
>last option.
>  
>
Jeff,

This worked fine for me:
<html>
<body>
[loop
    list=` join ',', reverse 1981..1900+(localtime())[5]`
]
[loop-code]<br />
[/loop]
</body>
</html>

Without the "join ','," call, it treated the year list as a single 
string and reversed the characters thereof.

When you're passing stuff into a tag argument via the backtick operator, 
it of course interpolates the embedded Perl right away.  However, Perl 
that returns a list won't necessarily work the way you expect.  If you 
want to build a complex list for [loop] to use, you actually need your 
Perl to return an arrayref, in which the first element is an arrayref of 
records (each record can be an arrayref or hashref), the second element 
is a hashref of field names to array indexes (telling the structure of 
the "records" passed in the first argument), and the third element must 
be an arrayref of field names in the order they appear within the 
"records".  I may have the second and third elements switched, I'm not 
absolutely positive.

For a simple loop with one "column" per "record", it suffices to pass 
the list in a scalar with a consistent delimiter.  You could produce 
more complicated lists with embedded perl if each "record" was 
terminated with a newline, and the "columns" were separated by a 
different delimiter (like tab).  For this kind of structure, you need to 
pass the loop tag an argument to tell it the field name order within 
each record.

- Ethan

-- 
Ethan Rowe
End Point Corporation
ethan at endpoint.com



More information about the interchange-users mailing list