Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Odd and even table rows

by brian_d_foy (Abbot)
on Aug 08, 2007 at 15:35 UTC ( [id://631329]=note: print w/replies, xml ) Need Help??


in reply to Odd and even table rows

I got tired of doing this all the time, so I created Tie::Cycle. Every time I use the tied scalar, it advances itself along its possible values:

use Tie::Cycle; tie my $cycle, 'Tie::Cycle', [ qw( even odd ) ]; foreach my $row ( 0 .. 6 ) { print qq|<tr class="$cycle">...</tr>\n|; }

In the output, it flip flops between "odd" and "even".

<tr class="even">...</tr> <tr class="odd">...</tr> <tr class="even">...</tr> <tr class="odd">...</tr> <tr class="even">...</tr> <tr class="odd">...</tr> <tr class="even">...</tr>

It gets better when you want to cycle through more values. Just add them to the call to tie:

use Tie::Cycle; tie my $cycle, 'Tie::Cycle', [ qw( red white blue ) ]; foreach my $row ( 0 .. 6 ) { print qq|<tr class="$cycle">...</tr>\n|; }
--
brian d foy <brian@stonehenge.com>
Subscribe to The Perl Review

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://631329]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-23 21:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found