Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Anyone use "xor" in conditionals?

by tachyon (Chancellor)
on Jul 14, 2003 at 06:07 UTC ( [id://273885]=note: print w/replies, xml ) Need Help??


in reply to Anyone use "xor" in conditionals?

It is one way to write a flip_flop, like in this snippet which uses a xor flip flop to alternate row colours in a 'pretty' HTML table...

sub pretty_table { my ($ary, $width, $no_center) = @_; my $header = shift @$ary; $width = $width ? qq!width="$width"! : ''; my $html = qq!<table border="0" cellpadding="2" cellspacing="1" $w +idth>\n!; my $cols = scalar @$header; $html .= get_row( $header, $cols, 'header' ); my $flip_flop = 1; for my $row_ref ( @$ary ) { my $class = $flip_flop ? 'light' : 'dark'; $html .= get_row( $row_ref, $cols, $class ); $flip_flop ^= 1; } $html .= qq! <tr>\n <td colspan="$cols" class="header">&nbsp; +</td>\n </tr>\n!; $html .= "</table>\n"; $html = qq!<div align="center">\n<center>\n$html\n</center>\n</div +>! unless $no_center; return $html } sub get_row { my ( $row_ref, $cols, $class ) = @_; my $html = qq! <tr>\n!; for my $td ( 0.. $cols-1 ) { my $data = $row_ref->[$td] || '&nbsp;'; $html .= qq! <td class="$class">$data</td>\n!; } $html .= " </tr>\n"; return $html; }

Of course you can get the same effect using $flip++ % 2 == 0 amongst others.

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: Anyone use "xor" in conditionals?
by kal (Hermit) on Jul 14, 2003 at 09:33 UTC
    Hehe, whenever I use flipflops I confuse the cow orkers. However, I've always written it differently:
    my $flipflop = 1; for my $row_ref ( @$ary ) { ... $flip_flop = 1 - $flipflop; }
    I'm not sure that this is potentially more efficient in Perl (probably less efficent?), but is an idiom I remember from my Amiga asm days. Personally, I also think it's easier to see what's going on too :)

      I'm a bit confused by these flipflops being used, as well as the ternary thing, to alternate colours.

      I long ago whittled it down to just

      $bgcolor = ($bgcolor eq 'white'?'black':'white');
      which flips itself.

      “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
      M-J D
        Check out Tie::Toggle:
        use Tie::Toggle; tie my $toggle, 'Tie::Toggle'; my @color = qw(black white); print "$color[$toggle]\n" for 1..10;
        Using more than one color? Tie::Cycle
        use Tie::Cycle; tie my $color, 'Tie::Cycle', [qw(red white blue)]; print "$color\n" for 1..10;
        Combine that with Color::Spectrum:
        use Tie::Cycle; use Color::Spectrum qw(generate); tie my $color, 'Tie::Cycle', [generate(4,'#ff00ff','#00ff00')]; print "$color\n" for 1..10;

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
Re^2: Anyone use "xor" in conditionals?
by Aristotle (Chancellor) on Jul 15, 2003 at 09:02 UTC
    I prefer a data driven approach.
    my @cycle = ('#fff', '#fff', '#fff', '#ccc', '#ccc'); my $i = 0; # ... $foo .= $cycle[$i++ % @cycle]; # ...
    That way it is a no-brainer to use any pattern you wish. "Capture similarities in code, disparities in data."

    Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-03-19 04:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found