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

Hi Monks, I have the data in hash of hash and that needs to be mailed to support group. i wanna print the contents of this hash in the body of the mail. Can you please help me with this. I'm using MIME to send mails.
my $ac_date = convert($last); my $total = int (($to_date - $ac_date) /86400); if ($total > 10){ my ($detail,$usr,$ho,$no); $hoh {$detail} {'user_name'} = $user; $hoh {$detail} {'last_used'} = $last; $hoh {$detail} {'host_name'} = $host[1]; }

Replies are listed 'Best First'.
Re: printing the hash contents in mail
by Corion (Patriarch) on Jan 08, 2008 at 08:40 UTC

    The simplest approach is to use Data::Dumper:

    use Data::Dumper; my %hoh; my $info = Dumper \%hoh; ...
      Hi
      Thanks for the reply. but the code u gave prints the contents as below. VAR1 = { '' => { 'last_used' => '2007/12/20', 'user_name' => 'xyz', 'host_name' => 'nett1' } }; But I need to print the contents as the below code does. foreach (sort keys %hoh){ print "$_ "; my $tmp_hash = $hoh{$_}; foreach (sort keys %$tmp_hash){ print $$tmp_hash{$_}; print " "; } print "\n"; }

        Then why didn't you say so in the first place?

        Instead of printing, just collect all your data into the string:

        my $info; foreach (sort keys %hoh){ my $data = "$_ "; my $tmp_hash = $hoh{$_}; foreach (sort keys %$tmp_hash){ $data .= $$tmp_hash{$_} . " "; } $info .= $data . "\n"; } print "$info"; # here you would append that to your mail body
Re: printing the hash contents in mail
by f00li5h (Chaplain) on Jan 08, 2008 at 08:43 UTC

    you could just use Data::Dumper and print Dumper( \%structure ) or YAML, which will produce a text representation of the structure... YAML will be somewhat more pretty, but Data::Dumper is far more in-core

    you could also write a recursive sub to dump it ...

    later YAML also provides a method called Dumper so it will play nice with code that's used to using Data::Dumper

    @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;