Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

You could avoid loading the entire table into memory by doing two reads («SELECT MAX(size)» and «SELECT qty, size, price ORDER BY qty ASC, size ASC»).

my $max_size = 5; # SELECT MAX(size) print("<table>"); print("<tr>"); print("<th>"); print("<th>Sizes"); print("<tr>"); print("<th>QTY"); for my $size (1..$max_size) { print("<th>$size"); } my $last_qty; my $last_size; for ( # SELECT qty, size, price ORDER BY qty ASC, size ASC [ 100, 1, 43 ], [ 100, 2, 45 ], [ 100, 3, 50 ], [ 200, 4, 55 ], [ 250, 1, 52 ], [ 250, 2, 55 ], [ 250, 3, 56 ], [ 250, 5, 61 ], ) { my ($size, $qty, $price) = @$_; if (!defined($last_qty) || $qty != $last_qty) { if (defined($last_size)) { print("<td>") for $last_size+1 .. $max_size; $last_size = 0; } print("<tr>"); print("<th>$qty"); $last_qty = $qty; } print("<td>") for $last_size+1 .. $size-1; print("<td>\$$price"); } if (defined($last_size)) { print("<td>") for $last_size+1 .. $max_size; } print("</table>");

But it's simpler to just loading everything into memory.

use List::Util qw( max ); my %table; my $max_size = 0; for ( # SELECT qty, size, price [ 100, 1, 43 ], [ 250, 1, 52 ], [ 100, 2, 45 ], [ 250, 2, 55 ], [ 100, 3, 50 ], [ 250, 3, 56 ], [ 200, 4, 55 ], [ 250, 5, 61 ], ) { my ($qty, $size, $price) = @$_; $table{$qty}[$size] = $price; $max_size = $size if $size > $max_size; } print("<table>"); print("<tr>"); print("<th>"); print("<th>Sizes"); print("<tr>"); print("<th>QTY"); for my $size (1..$max_size) { print("<th>$size"); } for my $qty (sort { $a <=> $b } keys(%table)) { print("<tr>"); print("<th>$qty"); for my $size (1..$num_cols) { print("<td>", defined($table{$qty}[$size]) ? "\$$table{$qty}[$si +ze]" : ''); } } print("</table>");

In reply to Re: X, Y Table structure by ikegami
in thread X, Y Table structure by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found