And while it was functional, I wanted to find the inner JAPH within. So I thought for a while, and developed the following snippet :for($x=0; $x<3;$x++){ for($y=0; $y<3;$y++){ print $lines[$y][$x], " "; } }
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.map { my $x=0; map { push @{$foo[$x]}, $_; $x++; } @$_; } @lines;
#!/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 ); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: transpose AoA and HTML table AoA contents.
by salvadors (Pilgrim) on Jan 12, 2001 at 02:24 UTC | |
by jeroenes (Priest) on Jan 12, 2001 at 13:24 UTC |