Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
This is taken from some blog. Who can beat it?
#!/usr/bin/perl -w # counts the instances of each character in the file specified. my %ascii_counts = (); my @keys = keys(%ascii_counts); my @values = values(%ascii_counts); my $total_chars; while (1) { # program's main loop &init_hash(); &fill_hash(); &clean_hash(); &print_hash(); } # Subroutines ======================================================== +== sub init_hash { # foreach ascii character add it to the hash with count = 0 foreach $i (0..255) { $ascii_counts{ord($i)} = 0; } } sub fill_hash { # get filename print "Enter 'ls' or a filename to scan: "; my $file_name = shift || ; while ($file_name =~ m/^ls/) { system("$file_name"); print "\nEnter 'ls' or a filename to scan: "; $file_name = ; } # open file open(INFILE, $file_name) or warn &fill_hash(); # iterate through lines/characters incrementing the key's value while (my $l = ) { my @line = split(//, $l); foreach $char (@line) { $ascii_counts{$char}++; } } # close file close(INFILE); } sub clean_hash { while (my ($key, $value) = each(%ascii_counts)) { # remove keys with value == 0 if ($value == 0) { delete($ascii_counts{$key}); } # remove non-alpha-numeric keys elsif ($key =~ /\W/) { delete($ascii_counts{$key}); } } # store the key/value pairs sorted for later use @keys = sort(keys(%ascii_counts)); @values = sort(values(%ascii_counts)); # determine total number of values $total_chars = 0; foreach $x (@values) { $total_chars += $x; } } sub print_hash { print "\n[key]\t[count]\t[%]\n\n"; foreach my $key (@keys) { my $value = $ascii_counts{$key}; my $percent = $value/$total_chars*100; print " $key\t$value\t$percent\n"; } print "\nTotal: $total_chars characters.\n\n"; }

My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

In reply to Golf: Improve this guy's fail . . . please! by dragonchild

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (5)
    As of 2018-06-18 23:56 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (111 votes). Check out past polls.