Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

My script is a bit complicated so I will try to use an example that has the concept of what I'm trying to do.

I have a hash that will be containing duplicate keys but different values.

What I need to do is to print the following format in HTML so it works properly with my DHTML Ticker. For the DHTML Ticker to work the format goes as:
<div id="tick1" class="dropcontent" subject="8-15-2006"> Jerry John Jen </div> <div id="tick2" class="dropcontent" subject="8-16-2006"> Keith Ken Kendra </div>
So I have a hash that stores a date value as its key and its value as the winners that day.

So if Jerry, John, and Jen won on 8-15-2006 then the hash will be like this:
%events=("8-15-2006"=>"Jerry","8-15-2006"=>"John","8-15-2006"=>"Jen");


But on a typical day, we would like the "ticker" to display each day of the week. So the hash will multiple days with multiple winners, not just "8-15-2006"

I have enough to populate the hash, but I don't know where to go to print out the HTML Format I need. As you notice, each div block has its subject as the "8-15-2006". Since 8-15-2006 shows up three times in the %events hash, then I need a way where it will only print it once but at the same time also print all 3 values for each winners name. Notice also how also the div id name changes from tick 1, tick 2 etc. Right now I just don't know enough to start accomplish.

Any help or referrences here would be awesome...thanks for taking your time to read this.

Bobby

Replies are listed 'Best First'.
Re: Filtering out a hash to display proper HTML
by bobf (Monsignor) on Aug 17, 2006 at 06:50 UTC

    Hashes cannot have duplicate keys. The example you gave actually reduces to a single key/value pair (%events = ( '8-15-2006' => 'Jen' )). It looks like you want a hash of arrays, where the key is the date and the value is a reference to an anonymous array that contains the names of the winners:

    %events = ( "8-15-2006" => [ qw( Jerry John Jen ) ] );
    See perldsc for more information about complex data structures.

    For printing the results you may want to look at one of the templating modules that are available, which will allow you to separate the code from the presentation. Template Toolkit is a popular choice, but for something this simple HTML::Template would easily do the job.

Re: Filtering out a hash to display proper HTML
by wfsp (Abbot) on Aug 17, 2006 at 07:09 UTC
    Here's one way using HTML::Template.
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use HTML::Template; my $param = { ticks => [ { tick => 'tick1', subject => '8-15-2006', names => [ { name => 'jerry', }, { name => 'john', }, { name => 'jen', }, ], }, { tick => 'tick2', subject => '8-16-2006', names => [ { name => 'Keith' }, { name => 'Ken', }, { name => 'Kendra', }, ], }, ], }; #print Dumper $param; my $t = HTML::Template->new(filename => 'ticker.html'); $t->param($param); print $t->output;
    ticker.html
    <html> <head> <title>ticker</html> </head> <body> <TMPL_LOOP NAME = 'ticks'> <div id = "<TMPL_VAR NAME = 'tick'>" class = "dropcontent" subject = "<TMPL_VAR NAME = 'subject'>" > <TMPL_LOOP NAME = "names"> <TMPL_VAR NAME = "name"> </TMPL_LOOP> </div> </TMPL_LOOP> </body> </html>
    output (some black lines removed)
    ---------- Capture Output ---------- > "C:\Perl\bin\perl.exe" _new.pl <html> <head> <title>ticker</html> </head> <body> <div id = "tick1" class = "dropcontent" subject = "8-15-2006" > jerry john jen </div> <div id = "tick2" class = "dropcontent" subject = "8-16-2006" > Keith Ken Kendra </div> </body> </html> > Terminated with exit code 0.