Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Custom HOA printing

by sulfericacid (Deacon)
on Jan 28, 2006 at 17:18 UTC ( [id://526186]=perlquestion: print w/replies, xml ) Need Help??

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

Still playing with HOAs and they're definitely a beast. This print out category headers for each new number in the hash. It then prints out everything within that "category".

I need to alter the output just a little bit. The items still need to be comma delimited but now I need to be able to put an html link code around each printed word (value).

I'm still not sure how this is printing the way it is, much less know how to add more HTML to it.

my %hash; push @{$hash{"3"}}, "apple"; push @{$hash{"2"}}, "pear"; push @{$hash{"5"}}, "orange"; push @{$hash{"3"}}, "grape"; push @{$hash{"2"}}, "icky pineapple"; # for a lack of a better way to show my hash # we'll create our own the hard way for my $key (reverse sort keys %hash) { print "<b>$key characters</b><br>", join(", ", @{$hash{$key}}), "<b +r><br>"; }
to yield
5 chars <a href="link.pl?=orange">orange</a> 3 chars <a href="link.pl?=apple">apple</a>, <a href="link.pl?=grape">grape</a>


"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

Replies are listed 'Best First'.
Re: Custom HOA printing
by atcroft (Abbot) on Jan 28, 2006 at 17:40 UTC

    From *very* limited testing, the following seems like it *might* work:

    print "<b>$key characters</b><br/>", join(", ", map{ '<a href="link.pl?=' . $_ . '">' . $_ . '</a>' } @{$hash{$key}} ), "<br/>" x 2;

    (Translation: map() processes each element in @{$hash{$key}}, returning to the join() for each a value set to <a href="link.pl?=$_">$_</a> .)

    Hope that helps.

Re: Custom HOA printing
by wfsp (Abbot) on Jan 28, 2006 at 17:53 UTC
    A bit late but here's a way with HTML::Template
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use HTML::Template; my %hash = ( 2 => ['pear', 'icky pineapple',], 3 => ['apple', 'grape',], 5 => ['orange',], ); my $char_loop; for my $key (reverse sort keys %hash) { my $fruit_loop; my @fruits = @{$hash{$key}}; for my $fruit (@fruits){ push @{$fruit_loop}, { fruit => $fruit, }; } push @{$char_loop}, { chars => $key, fruit_loop => $fruit_loop, } } #print Dumper $char_loop; my $t = HTML::Template->new( filename => q|sulferic.html|, ) or die $!; $t->param( char_loop => $char_loop, ); print $t->output(); __DATA__ ---------- Capture Output ---------- > "C:\Perl\bin\perl.exe" _new.pl <html> <head> <title>HoA</title> </head> <body> <b>5</b><br> <a href="link.pl?=orange">orange</a> <b>3</b><br> <a href="link.pl?=apple">apple</a> <a href="link.pl?=grape">grape</a> <b>2</b><br> <a href="link.pl?=pear">pear</a> <a href="link.pl?=icky pineapple">icky pineapple</a> </body> </html> > Terminated with exit code 0.
    The template:
    <html> <head> <title>HoA</title> </head> <body> <TMPL_LOOP NAME='char_loop'> <b><TMPL_VAR NAME='chars'></b><br> <TMPL_LOOP NAME = 'fruit_loop'> <a href="link.pl?=<TMPL_VAR NAME='fruit'>"> <TMPL_VAR NAME='fruit'> </a> </TMPL_LOOP> </TMPL_LOOP> </body> </html>

    tested ;-)

Re: Custom HOA printing
by serf (Chaplain) on Jan 28, 2006 at 18:45 UTC
    As you're just new with hashes of arrays (and presumably other complex data structures) you should find Data::Dumper helps you to A) define data structures in one hit and B) clearly see what your data structure contains:
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hash; push @{$hash{"3"}}, "apple"; push @{$hash{"2"}}, "pear"; push @{$hash{"5"}}, "orange"; push @{$hash{"3"}}, "grape"; push @{$hash{"2"}}, "icky pineapple"; print Dumper(\%hash), $/;
    (note the backslash in front of the % in the print statement) which would give you:
    $VAR1 = { '3' => [ 'apple', 'grape' ], '2' => [ 'pear', 'icky pineapple' ], '5' => [ 'orange' ] };
    Just change the $VAR1 to %hash and the {} to () and it's done for you :o)
    my %hash = ( '3' => [ 'apple', 'grape' ], '2' => [ 'pear', 'icky pineapple' ], '5' => [ 'orange' ] );
      Even easier still if you use Data::Dumper::Simple.

      No need to reference the hash, and the data comes out exactly the same way as it would be defined.

      eg:

      print Dumper(%hash);
      gives:
      %hash = ( '3' => [ 'apple', 'grape' ], '2' => [ 'pear', 'icky pineapple' ], '5' => [ 'orange' ] );
      Cheers,
      Darren :)
Re: Custom HOA printing
by McDarren (Abbot) on Jan 28, 2006 at 17:34 UTC
    The following does what gives the output you want:
    for my $key (reverse sort keys %hash) { print "\n<b>$key chars</b><br>\n"; foreach my $element (@{$hash{$key}}) { print "<a href=\"link.pl?=$element\">$element</a>"; } print "<br><br>\n"; }
    Although I'm not sure if this is what you were after (I got rid of the join)

    Update: The solution given by atcroft below is probably the more "Perl-ish" way to do it.
    Update 2:Fixed code so that <br><br> is printed after each set of array elements (thanks ysth).

    Cheers,
    Darren

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://526186]
Approved by McDarren
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-19 10:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found