Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Pivoting parts of a database table into an HTML table

by Anonymous Monk
on Oct 22, 2012 at 08:14 UTC ( #1000286=note: print w/replies, xml ) Need Help??


in reply to Pivoting parts of a database table into an HTML table

I suppose I need to post my half-solution, too. (My original post was getting long.) It's supposed to replace the rather common looping code:

my $lastid = 0; while ($sth->fetch) { print $row; if ($lastid != $id) { print "</tr><tr>"; } }

with this:

alternate( sub { $sth->fetch }, sub { print $row }, sub { $id }, # which variable to monitor sub { print "</tr><tr>" } # what to do when variable changes );

Currently it has at least an off-by-one error (the words go into the wrong columns), but I can't spot the error.

sub alternate { my $iter = shift; my $loop = shift; # main loop callback my @conds; while (@_ > 0) { # sub to get value, sub to call if value changed push @conds, [ shift, shift ]; } my @memory; return unless $iter->(); # starting values @memory = map { $_->[0]->() } @conds; do { $loop->(); while (my ($idx, $cond) = each @conds) { my $new = $cond->[0]->(); next if $memory[$idx] eq $new; $cond->[1]->(); # callback $memory[$idx] = $new; # reset subsequent values $memory[$_] = $conds[$_][0]->() for ($idx + 1 .. $#conds); last; } } while ($iter->()); } my $gen = make_generator(\my ($day, $type, $word)); alternate( $gen, sub { print "$word, " }, sub { $day }, sub { print "</td></tr>\n<tr><td>$day</td><td>" }, sub { $type }, sub { print "</td><td>" } );

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2016-10-01 20:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?






    Results (7 votes). Check out past polls.