Summary

This came about for 2 reasons.
Firstly, someone asked about dumping an array of arrays into an html table. The basics of doing that are easy enough, but I've included a few extra options for my own projects - optional borders and table headers as well as verifying each array has the same number of members. The final step came from some customer reports. They listed things by row, rather than by column. A quick search through PM didn't show anything useful, so I whomped up some code that did the trick. Aside from the fact that it only prints the array, it looks a lot like C :
for($x=0; $x<3;$x++){ for($y=0; $y<3;$y++){ print $lines[$y][$x], " "; } }
And while it was functional, I wanted to find the inner JAPH within. So I thought for a while, and developed the following snippet :

map { my $x=0; map { push @{$foo[$x]}, $_; $x++; } @$_; } @lines;
Not bad! Though, I can't help but think that the $x is really, in some secret way, horribly redundant. Plus, using braces to reference arrays hurts my head.
So, with all that in mind, I present the following program, which presents and tests make_table_from_AoA. Please note that it should die on the fourth test; this is not an error.
#!/usr/bin/perl use CGI::Pretty qw(:standard); # import CGI objects and fu +nctions use CGI::Carp qw(fatalsToBrowser); @a= ( ["Food","Price","Check to order","1/2 off one item"], ["burger","\$2.00",checkbox ("One of these"),radio_group (-name=> +"half off bonus", -value =>1)], ["chicken","\$2.50",checkbox ("and one of these, too"),radio_grou +p (-name=>"half off bonus",-value =>2)], ["tofutii","\$1.75",checkbox ("and one of them, while we're at it +."),radio_group (-name=>"half off bonus",-value =>3)], ); @b= ( ["Fruit" ,"Kiwi" , "Tangerine" , "Mango" , "Pom +elo","Carambola"], ["Origin" ,"New Zealand" , "Florida" , "Venezuela" , "Mex +ico","Arizona"], ["Color" ,"Brown" , "Orange" , "Red & Green" , "Yel +low"] ); print header(); print start_html(); &make_table_from_AoA (1,0,1,0,@a); print p,hr,p; &make_table_from_AoA (1,1,1,4,@a); print p,hr,p; &make_table_from_AoA (0,0,0,2,@b); print p,hr,p; &make_table_from_AoA (1,1,1,1,@b); #################### # # make_table_from_Aoa # # parameters # 1) $use_th : if this is true, the first line of the passed array +will be used # as an HTML header. # 2) $transpose : swap axis of array # 3) $check_array_size : if true, make sure each array has same # o +f elements # 4) $border : size of border to put around table. # 5) @l_array : holding tank for passed array. # #################### sub make_table_from_AoA { local $use_th = shift; local $transpose = shift; local $check_array_size = shift; local $border = shift; local @l_array = @_; #Make sure arrays are the same size. if not, die. if ($check_array_size){ $size =scalar(@{$l_array[0]}); map {die "funky arrays : First array is $size, found one of " +.scalar(@{$_}) if scalar(@{$_}) != $size}@l_array; } if ($transpose) { local @tary; map {my $x=0; map {push @{$tary[$x]}, $_;$x++;} @$_; } @l_array; @l_array=@tary; } print table( {-border=>$border}, $use_th?th([@{shift @l_array}]):undef, map{Tr(map{td($_)}@$_)}@l_array ); }

In reply to transpose AoA and HTML table AoA contents. by boo_radley

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.