#!perl -wT # musiclist.cgi -- spits out my CD collection as HTML; 2nd attempt use strict; use CGI; # some security, as per the CGI.pm POD $CGI::POST_MAX = 1024 * 0; # no posts $CGI::DISABLE_UPLOADS = 1; # no uploads #################### my ($infile, @data, $description, $artist, $category, $year, $title, $notes, $numFields, %groupBy, $q, $key); $description = < [ $artist, $category, $year, $title, $notes ], category => [ $category, $artist, $year, $title, $notes ], year => [ $year, $artist, $category, $title, $notes ], title => [ $title, $artist, $category, $year, $notes ], ); #################### $q = new CGI; $key = $1 if $q->param('groupKey') =~ /^(\w+)$/; print $q->header, $q->start_html("My CD's"), $q->h1("My CD's"); if( $key && exists $groupBy{$key} ) { my %groups; print $q->hr, $q->p("Here is my collection of CD's, grouped by ", $key, "."); open IN, "<$infile" or die "Error opening input file: $!"; while( ) { next if /^#|^$/; # skip commented and empty lines chomp; push @data, [ split ',', $_, $numFields ]; } close IN; # change this if mapping table (%groupBy) changes for ( @data ) { my @fields = @{ $groupBy{$key} }; push @{ $groups{ @{ $_ }[ $fields[0] ] } }, [ @{ $_ }[ $fields[1] ], @{ $_ }[ $fields[2] ], @{ $_ }[ $fields[3] ], @{ $_ }[ $fields[4] ], ]; } for ( sort keys %groups ) { my @entries; for ( @{ $groups{$_} } ) { push(@entries, join(", ", @{ $_ })); } print $q->p($q->b($_)), $q->ul($q->li(\@entries)); } } else { print $q->p($description); } print $q->hr, $q->startform("GET", "musiclist.cgi", $CGI::URL_ENCODED), $q->p('Group by:'), $q->radio_group( -name => 'groupKey', -values => [ 'category', 'artist', 'year', 'title' ], -labels => { category => 'Category', artist => 'Artist, group, or composer', year => 'Copyright year', title => 'CD title', }, -linebreak => 'true' ), $q->br, $q->submit, $q->reset; $q->endform, $q->hr, $q->end_html; #### while( ) { next if /^#|^$/; chomp; my @record = split ',', $_, $numFields; push @{ $groups{ $record[ $fields[0] ] } }, [ $record[ $fields[1] ], $record[ $fields[2] ], $record[ $fields[3] ], $record[ $fields[4] ], ]; } close IN;