in reply to Category List

Here's a cute little hack for you. You don't specify whether you want them sorting or what, but anyway:
#!/usr/bin/perl -w use strict; # sorted list my @list = sort qw(stereo tv monitor scanner keyboard mouse banana); # add extra element if odd length, so next statement doesn't break push @list, '' if @list%2; # turn array into a hash my %list = @list; # text - need 'if' to avoid empty bullet if odd No. elements for (sort keys %list) { printf("- %-20s", $_); printf("- %-20s", $list{$_}) if $list{$_}; print "\n"; } # html (snippet) print qq(\n<table>\n); for (sort keys %list) { printf("<tr><td>%s</td><td>%s</td></tr>\n", $_, $list{$_}); } print qq(</table>\n);
This sorts them left -> right, top -> bottom. If you want the precedence the other way round, you need to work on things a bit more.

hth

cLive ;-)

Replies are listed 'Best First'.
Re: Re: Category List
by Kanji (Parson) on Dec 02, 2001 at 13:31 UTC

    You can avoid using a hash if you loop through @listnelements at a time, which has the added bonus of flexibility (for when you want to change the number of columns later on).

    #!/usr/bin/perl -wT use strict; my $columns = 2; # change this as needed my @list = sort qw(stereo tv monitor scanner keyboard mouse banana) +; while ( my @column = splice( @list, 0, $columns ) ) { printf "- %-20s", $_ foreach @column; print "\n"; # print TR( td( \@column ) ), "\n"; # CGI.pm-ized :-) }

        --k.