Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: OUTPUT Data to a table or horizontal bar chart

by hippo (Chancellor)
on May 17, 2018 at 12:46 UTC ( #1214735=note: print w/replies, xml ) Need Help??


in reply to OUTPUT Data to a table or horizontal bar chart

These first few points don't relate directly to your question but rather to your code. I hope you don't mind.

print start_html(-title =>'Senders', -bgcolor=>'#95B8DB');

I wouldn't do that and nor would the maintainer of CGI who says "HTML Generation functions should no longer be used". For the reasons listed there this is best avoided.

use CGI ':standard';

If you took the advice not to use the HTML Generation functions then you probably don't need this line either because all you are using the rather heavy CGI module for now is printing the header. But that's one immutable line of output so just use

print "Content-Type: text/html; charset=utf-8\n\n",

and be done with it.

system("sudo grep -E 'Passed CLEAN.* ->.*' /var/log/maillog | grep -v +root@ > /tmp/PassedCLEAN.txt"); system("sudo awk '{print \$12}' /tmp/PassedCLEAN.txt > /tmp/PassedCLEA +N_1.txt"); system('sed -e "s/<\(.*\@.*\)>/\1/g" /tmp/PassedCLEAN_1.txt > /tmp/Pas +sedCLEAN_2.txt');

I really, really wouldn't do that. You are shelling out from perl to run 2 greps, an awk and a sed: three utilities which perl supercedes in 99% of cases. Think about how you could do all of this from within perl instead.

Turning to your question, I would replace your output stanza of

for my $word (sort keys %count) { print "$word $count{$word}\n"; #print "$count{$word}\n"; print "<br />"; print " \n"; } close $fh;

with something which leverages the HTML table element. eg:

print '<table><tr><th>Word</th><th>Count</th></tr>'; for my $word (sort keys %count) { print "\n<tr><td>$word</td><td>$count{$word}</td></tr>"; } print '</table>';

Or, for a bar chart:

print '<table><tr><th>Word</th><th>Count</th></tr>'; for my $word (sort keys %count) { print "\n<tr><td>$word</td><td>" . ('#' x $count{$word}) . "</td>< +/tr>"; } print '</table>';

When you are happy with this principle you can go further and abstract it with a template if you wish, eg. Template.

Replies are listed 'Best First'.
Re^2: OUTPUT Data to a table or horizontal bar chart
by theravadamonk (Scribe) on May 18, 2018 at 10:47 UTC

    Hi Guys, Thanks for the below CODE. it works well.

    print '<table><tr><th>Word</th><th>Count</th></tr>'; for my $word (sort keys %count) { print "\n<tr><td>$word</td><td>$count{$word}</td></tr>"; } print '</table>';

    It sorts from A to Z order With word column. I want to sort the other column which is Count. I need from Largest number to lowest. e.g - email account having most emails should sort first and So on. I tried But I couldn't.

    e. g- I tried, But I did not succeed. sort($b <=> $a) ; # for numbers

    I think I will have to change this below code

    for my $word (sort keys %count) {

    Could you pls help me to resolve this issue. i searched a lot. Your codes are welcome.

        I changed below code

        for my $word (sort keys %count) {
        to
        for my $word (sort { $count{$b} <=> $count{$a} } keys %count) {
        It seems to work. Anyway, Your Ideas r welcome

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1214735]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2020-12-01 00:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?